From: Greg C. <chi...@mi...> - 2001-05-06 05:21:07
|
Franco Bez wrote: > > Am Sonntag, 6. Mai 2001 03:40 schrieb Greg Chicares: > > Michael Kristofik wrote: > > This program segfaults even if I comment out the delete line: > > No wonder - you reserve only 2Byte of storage in a static array. Yes. The program was designed to illustrate undefined behavior. > Also You MUST NOT delete static arrays. [snip] > > int main() > > { > > char a[2]; > > a[1000] = 'x'; // <-- segfault > > // delete[] a; > > } There was a defect in the comment. Attempting to delete an auto array was not the undefined behavior I wanted to show. Here's a corrected version with no unintended defect: // Demonstration that a program that manipulates char arrays // created on the freestore can crash with a segfault, even // when no such array is deleted. Warning--this program // deliberately contains one intentional defect. // int main() { char* a = new char[2]; a[100000] = 'x'; // <-- segfault here (intentional defect)... delete[] a; // <-- ...so removing this line doesn't help } Here's an error log produced by a proprietary compiler package: Error 00001. 0x130610 (Thread 0xFFE5062F): Access overrun: Attempt to access 1 byte(s) at 0x00B82FDC+100000, that is at offset 0+100000 in heap block 0x00B82FDC which is only 2 bytes long. | crash.cpp line 4: | { | char* a = new char[2]; |> a[100000] = 'x'; // <-- segfault | delete[] a; | } Call Tree: 0x0040112D(=CRASH.EXE:0x01:00012D) crash.cpp#4 0x004385E9(=CW3230.DLL:0x01:0275E9) The object array (0x00B82FDC) [size: 2 bytes] was created with new[] | crash.cpp line 3: | int main() | { |> char* a = new char[2]; | a[100000] = 'x'; // <-- segfault | delete[] a; Call Tree: 0x0040111F(=CRASH.EXE:0x01:00011F) crash.cpp#3 0x004385E9(=CW3230.DLL:0x01:0275E9) ------------------------------------------ Error 00002. 0x400000 (r) (Thread 0xFFE5062F): Exception 0xC0000005: Access violation at 0xB9B67C. | crash.cpp line 4: | { | char* a = new char[2]; |> a[100000] = 'x'; // <-- segfault | delete[] a; | } Call Tree: 0x0040112D(=CRASH.EXE:0x01:00012D) crash.cpp#4 0x004385E9(=CW3230.DLL:0x01:0275E9) I know of no tool that does all that for mingw, but the original poster might use gdb to find his problem, which I suspect is not in the tools. For the program above, for instance, gdb gives: (gdb) bt Error: #0 0x401235 in main () at crash.cpp:4 #1 0x4011c1 in __mingw_CRTStartup () #2 0x4011eb in mainCRTStartup () #3 0xbff88e93 in _size_of_stack_reserve__ () #4 0xbff88d41 in _size_of_stack_reserve__ () #5 0xbff87759 in _size_of_stack_reserve__ () #6 0x6 in ?? () #7 0x650de100 in ?? () Cannot access memory at address 0x16007004 |