473,406 Members | 2,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Segmentation error on return

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
3 1800
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Vishal Grover | last post by:
Hello Everyone, I am seeing a certain behaviour which I find strange, and am curious to get an explanation to it. I have the following program. #include <iostream> #include <cstdlib> using...
3
by: VB | last post by:
Hi, here File.cpp and File.h: File.cpp: ---------------------- #pragma warning (disable: 4786)
3
by: Anks | last post by:
i am unable to find why following code is giving segmentation fault.... way to produce seg fault: run the program... give input 12345678....enter any key except 'x'.... again give 12345678 as...
6
by: damian birchler | last post by:
If I run the following I get a segmentation fault: #define NAMELEN 15 #define NPERS 10 typedef struct pers { char name; int money; } pers_t;
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
14
by: cnixuser | last post by:
I am new to C programming and am still at an early level in java and C#. I am posting regarding a segmentation fault error code I get when I try to run a program that I am developing. I am coding on...
6
by: DanielJohnson | last post by:
int main() { printf("\n Hello World"); main; return 0; } This program terminate just after one loop while the second program goes on infinitely untill segmentation fault (core dumped) on...
0
by: ollii | last post by:
Hello evryboody, i created client and srever program that they can both communicate together by TCP and UDP, but when i want to send message to server from client i get error on the server i get...
0
by: wmihelpme | last post by:
Hi All, I have written a WMI Program which changes the network settings like ip address, subnet mask, gateway and dns addresses for given adapter number. when I call that function for adapter...
61
by: arnuld | last post by:
I have created a program which creates and renames files. I have described everything in comments. All I have is the cod-duplication. function like fopen, sprint and fwrite are being called again...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.