Solved

Segmentation Faults

Posted on 2000-04-17
9
342 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
Comment Utility
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
Comment Utility
But why doesn't this happen when I do the manipulations to every file?
0
 
LVL 2

Expert Comment

by:fremsley
Comment Utility
> 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
Comment Utility
what about your code send it to EE
we will try to help you
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 1

Expert Comment

by:meerak
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

762 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

8 Experts available now in Live!

Get 1:1 Help Now