• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 353
  • Last Modified:

Segmentation Faults

I read in a file, do manipulations or calculations and then read in another file and do the same manipulations or calculation. I do this for a lot of files. Sometime after the file has been read in, and it starts to do the manipulations and calculations I get a segmenation fault. What could cause this? It doesn't happen on may files. Could it be the was  the file was set up (it is a binary file)? Or is it something in my code? Any suggestions would be appreciated?
Thanks
0
llagreca
Asked:
llagreca
1 Solution
 
fremsleyCommented:
It's definitly something wrong in your code. A segmentation violation means that your program tries to access memory that is not allocated for it. This usually is caused by an uninitialized pointer or when you try to read/write to memory that has already been freed.

The best way to find out is to run the program in a debugger with a file that causes the segfault. There are several good graphical front-ends for the system's debugger available -- I assume you code under Unix --, my favourite: ddd
0
 
llagrecaAuthor Commented:
But why doesn't this happen when I do the manipulations to every file?
0
 
fremsleyCommented:
> But why doesn't this happen when I do the manipulations to every file?

That depends on your code; is there any special case handling depending on the data read from the file? Such parts of the code would not always execute.

If you are not familiar with debugging under Unix -- can be a quite complex task -- try this to track hanging pointers:

  - always initialize pointers to NULL
  - when you free() memory, set the pointer to NULL afterwards
  - #include <assert.h>
  - preceede every use of pointers with an assert statement like assert(my_pointer != NULL);

note: assert() is a macro, you can leave it inside your source files and simply #define NODEBUG when you're compiling for a release version.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
ntdragonCommented:
what about your code send it to EE
we will try to help you
0
 
meerakCommented:
If you give the stack trace of the core dump we should be able to
help you. Which version of OS are you using? Do you have
a debbugger installed on your machine? If you have one use it
to get the stack trace and let us know
0
 
amorousdonjuanCommented:
a segmentation fault occurs when a program accesses an area of memory (overwriting the memory) to which it does not have access.


free all the pointers that are used in one operation after u feel that they wont be used any more.use free(ptr) function.


and then only initialise new pointers.

the problem with pointers is that they exist even after you finish executing the program.so sometimes you might have to hardboot the system in order to free the memory.

if you are using turbo c++ trying working around with new() and delete() functions.

or one more method calculate the exact amount of space your pointers will be using and use "far calloc or far malloc " functions
0
 
fremsleyCommented:
amorousdonjuan: I have never seen a 'Segmentation fault, core dumped' message on a DOS/Windows system -- these are the typical famous last words of an Unix program.

> a segmentation fault occurs when a program accesses an area of memory (overwriting the memory) to which it does not have access.

Right, it does not have access to that address, thus it cannot overwrite anything.

> the problem with pointers is that they exist even after you finish executing the program.so sometimes you might have to hardboot the system in order to free the memory.

Under Unix all resources used by a process are freed when it terminates; no need to reboot. Remember: Unix is an operating system :-)

0
 
amorousdonjuanCommented:
i know my friend who used to invoke a General Protection Fault while running a C++ Program in DOS/Windows.

This used to happen every second run.
0
 
fremsleyCommented:
amorousdonjuan: Right, 'General Protection Fault' is the translation of 'Segmentation fault' in the DOS/Windows world. I just wanted to point out that llagreca does not have to worry burning the whole system with a buggy program if he/she is working under Unix, because the OS will not allow it to corrupt system memory or leave orphined memory after termination.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now