Segmentation fault when calling “new” in c++?

Assuming it really is new generating the segfault, the most common cause would be a corrupted heap, typically a result of overwriting memory you don't own and/or a double delete.

Up vote 3 down vote favorite 2 share g+ share fb share tw.

I got segfault when trying to call "new" to create a pointer and push it into a vector. The code that I push the element in the vector is: queue->push_back(new Box(gen_id, Interval(x_mid, x_end), Interval(y_mid-y_halfwidth, y_mid+y_halfwidth))); Basically Box is a Class and the constructor just take 3 arguments, generation_id, and 2 Intervals. I printed out the content in vector before and after this "push", before: -0.30908203125, -0.3087158203125 , -0.951416015625, -0.9510498046875 -0.3087158203125, -0.308349609375 , -0.951416015625, -0.9510498046875 -0.30908203125, -0.3087158203125 , -0.9510498046875, -0.95068359375 -0.3087158203125, -0.308349609375 , -0.9510498046875, -0.95068359375 after: -0.30908203125, -0.3087158203125 , -0.951416015625, -0.9510498046875 -0.3087158203125, -0.308349609375 , -0.951416015625, -0.9510498046875 8.9039208750109844342e-243, 6.7903818933216500424e-173 , -0.9510498046875, -0.95068359375 -0.3087158203125, -0.308349609375 , -0.9510498046875, -0.95068359375 -0.3087158203125, -0.308349609375 , -0.95123291015625, -0.95086669921875 I have no clue why does this happen, but apparently, there's one element got corrupted.

There's no other codes between these two sets of output except that "push", and I used gdb to confirm that. Also, I checked those 2 Intervals variables, both give me a result that make sense. My questions is: in what situation does "new" get segfault?

Or is my problem caused because of other stuff? Thanks. C++ segmentation-fault new allocation link|improve this question edited Sep 6 '11 at 23:18Oli Charlesworth80.3k892184 asked Sep 6 '11 at 23:17da_zhuang31717 100% accept rate.

Please show us the code for Box. – Oli Charlesworth Sep 6 '11 at 23:18 1 Why are you pushing pointers? – Leandro F.

Vaz Sep 6 '11 at 23:20 I don't think new can normally segfualt. I would look for stack corruption, starting with Box(int, Interval, Interval), and Interval(int, int) and Intervals copy/move constructor/operator – Mooing Duck Sep 6 '11 at 23:21 1 @Moo, new can conceivably segfault if there is heap corruption. – Henning Makholm Sep 6 '11 at 23:23 @Henning Makholm: anything can happen if there is heap corruption.

Stackoverflow.com/questions/2766731/…, so yes. Still: probably array bounds or pointer errors. – Mooing Duck Sep 6 '11 at 23:24.

Assuming it really is new generating the segfault, the most common cause would be a corrupted heap, typically a result of overwriting memory you don't own and/or a double delete. Valgrind will be your friend if you can run on a Linux system.

Sep 7 '11 at 1:14 +1 for valgrind from me too! – c0da Sep 7 '11 at 7:18.

I doubt that new itself is giving you the segfault; the problem is probably in one of the constructors. Try splitting up that giant line, and put in some print statements to see exactly where the problem is. Printf("Creating the first interval...\n"); Interval a(x_mid, x_end); printf("Creating the second interval...\n"); Interval b(y_mid-y_halfwidth, y_mid + y_halfwidth); printf("Creating the box...\n"); Box* box_to_enqueue = new Box(gen_id, a, b); printf("Enqueueing the box...\n"); // Do you really want to enqueue a pointer instead of a Box?

Queue->push_back(box_to_enqueue).

And don't forget to put an fflush() in this sequence, or else error messages might end up in an unwritten buffer. Even better, use a debugger. – thiton Sep 17 '11 at 18:45.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions