Segmentation error on return

H

Hottejonas

I have written a program in which I declare a double array as follows
double *uMatrix;

uMatrix = new double[Nnodes*(Ntimesteps+1)];

And I make sure to free it in the last part of the main program

delete[] uMatrix;

This is the second last statement of the program, and no error occures if I
terminate with exit(0) just after this line. But if I terminate the program
with

return 0;

instead of the exit(0) statement, I get a segmentation error.



It really puzzles me. Hope you can give me a good answer



Sincerely Jonas Dahl
 
T

Thomas J. Gritzan

Hottejonas said:
I have written a program in which I declare a double array as follows
double *uMatrix;

uMatrix = new double[Nnodes*(Ntimesteps+1)];

And I make sure to free it in the last part of the main program

delete[] uMatrix;

Prefer std::vector. It is easier to use and harder to forget a delete[].
This is the second last statement of the program, and no error occures if I
terminate with exit(0) just after this line. But if I terminate the program
with

return 0;

instead of the exit(0) statement, I get a segmentation error.

You did something wrong on line 42.
Perhaps you wrote out of array bounds.
It really puzzles me. Hope you can give me a good answer

Read here:
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8
 
J

Jens Theisen

Hottejonas said:
instead of the exit(0) statement, I get a segmentation error.

It has probably nothing to do with your allocation. You will have to
provide a complete example that one can compile, there is not much one
can do to help you otherwise.

Also mention what platform and compiler you're using!

Regards,

Jens
 
J

Jacek Dziedzic

Hottejonas said:
I have written a program in which I declare a double array as follows
double *uMatrix;

uMatrix = new double[Nnodes*(Ntimesteps+1)];

And I make sure to free it in the last part of the main program

delete[] uMatrix;

This is the second last statement of the program, and no error occures if I
terminate with exit(0) just after this line. But if I terminate the program
with

return 0;

instead of the exit(0) statement, I get a segmentation error.



It really puzzles me. Hope you can give me a good answer

Do you have destructors in your code? If so, they are executed
upon "return 0", but not upon "exit(0)" (I think). Therefore
destructors of global and static variables would be the first
suspect to investigate.

If you don't have them, then I'd blame an out-of-bounds
error somewhere -- use a tool like efence or valgrind to
track it, or better still, follow Thomas's advice and
prefer std::vector to dummy arrays.

HTH,
- J.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top