This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

segmentation fault when allocating memory with new or malloc


Hi,

allocating memory with one of these lines:

pl=(list*)malloc((v)*sizeof(list));
or
list*pl; pl=new list[v];

causes the program to be terminated by a segmentation fault.
v is an integer that equals e.g. 7 in my test runs (thus far from throwing a bad_alloc)


and list is trivially:
 struct list{
 	     int x;
	     int y;
	  double d;
	    };

Replacing the variable v by a constant value does not improve the situation.
Also:
cout << "A\n";
try {pl=new list[v];}
catch(bad_alloc) {cerr<< "not enough memory\n";}
cout << "B\n";
results in the output:
A
Segmentation fault


Additionally weird is that omitting the memory allocation
makes the program run further. Yet it crashes with a seg fault.
when trying to set up an outfile stream:
bmpofstream image("floor.bmp",xmax,ymax);
later in the program. The rest of the program contains datastructures for which I allcoate some 100kB memory (with new).


I use g++, gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)

Regards,
ToK


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]