Connecting Tech Pros Worldwide Forums | Help | Site Map

Segmentation error on return

Hottejonas
Guest
 
Posts: n/a
#1: Sep 29 '06
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



Thomas J. Gritzan
Guest
 
Posts: n/a
#2: Sep 29 '06

re: Segmentation error on return


Hottejonas schrieb:
Quote:
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[].
Quote:
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.
Quote:
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
Jens Theisen
Guest
 
Posts: n/a
#3: Sep 29 '06

re: Segmentation error on return


"Hottejonas" <Hottejonas@gmail.comwrites:
Quote:
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
Jacek Dziedzic
Guest
 
Posts: n/a
#4: Sep 30 '06

re: Segmentation error on return


Hottejonas wrote:
Quote:
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.
Closed Thread