By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,421 Members | 1,053 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,421 IT Pros & Developers. It's quick & easy.

Segmentation error on return

P: n/a
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
Sep 29 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hottejonas schrieb:
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-lit...t.html#faq-5.8

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Sep 29 '06 #2

P: n/a
"Hottejonas" <Ho********@gmail.comwrites:
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
Sep 29 '06 #3

P: n/a
Hottejonas wrote:
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.
Sep 30 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.