Solved

Segmentation Faults

Posted on 2000-04-17
9
343 Views
Last Modified: 2010-04-15
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
Comment
Question by:llagreca
9 Comments
 
LVL 2

Expert Comment

by:fremsley
ID: 2722491
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
 

Author Comment

by:llagreca
ID: 2722583
But why doesn't this happen when I do the manipulations to every file?
0
 
LVL 2

Expert Comment

by:fremsley
ID: 2722762
> 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
 
LVL 1

Expert Comment

by:ntdragon
ID: 2722770
what about your code send it to EE
we will try to help you
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 1

Expert Comment

by:meerak
ID: 2724238
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
 

Accepted Solution

by:
amorousdonjuan earned 75 total points
ID: 2755457
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
 
LVL 2

Expert Comment

by:fremsley
ID: 2758501
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
 

Expert Comment

by:amorousdonjuan
ID: 2758793
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
 
LVL 2

Expert Comment

by:fremsley
ID: 2758943
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Picking random number 8 175
Describe order of operation while copying text 11 114
Coverting 24 hour time to 12 hour in C++ 15 168
how to understand recursion 12 218
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now