LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Segmentation fault (https://www.linuxquestions.org/questions/programming-9/segmentation-fault-333699/)

tejas15_10 06-15-2005 12:06 AM

Segmentation fault
 
hi to all
i have some problem with my code..when i compile my code its not giving me any error but when i run the application it gives the error "segmentation fault"...i checked the code and still i m not assigning a heavy memory to the code..but dont knw wht is the problem...
plz reply me abt the segmentation error..how to remove it from the code??or any tactics for that...its very urgent for me...
thanx for reading my thread and possibly then reply....
-Tejas

zulfilee 06-15-2005 12:44 AM

Hi,
your query was not very clear
Anyway if its a C code you are talking about do the following
Compile the code

Then in your terminal type
ulimit -c unlimited

Now run the executable and it will generate a core dump for the fault

Now since the core has got generated run a gdb on the core

This you can run by typing

gdb exe -c core

In gdb session type
bt

And this will give you the place of probable error.

Note: Compile your code with the -g option

Cheers
Z

professional1983 06-15-2005 12:54 AM

Re: Segmentation fault
 
Quote:

Originally posted by tejas15_10
hi to all
i have some problem with my code..when i compile my code its not giving me any error but when i run the application it gives the error "segmentation fault"...i checked the code and still i m not assigning a heavy memory to the code..but dont knw wht is the problem...
plz reply me abt the segmentation error..how to remove it from the code??or any tactics for that...its very urgent for me...
thanx for reading my thread and possibly then reply....
-Tejas

i just hate to see this term "segmentation error". I always get it when i run my c programs:scratch:

possible errors
-----------------
1) you tried to use a pointer that was not allocated using malloc or calloc
2) you are trying to access a pointer that has been freed
3) you are trying to access an array element that does not exist
4) you use a fileopen function but it fails to open the file and then you try to write/read from the file thinking the open function was successful

these are a few but not all the causes. The best way is to use gdb and backtrace

tejas15_10 06-15-2005 06:37 AM

i download the gdb but i dont knw how to compile code before that..i8 m newbie for linux..i compiled so many file and generated *.o file..my project contain 400+ files..so to compile it at the same time...wht is the syntax..code did not give the error at compile tile....but it gives an error at run time...and my code contain c and cpp files...and the size of an exe on Linux..size is 2.7MB+..in window..
reply me soon..i m waiting..

zulfilee 06-15-2005 07:05 AM

Your second posting is still more confusing than the first

First tell us what distribution of Linux u are using

And are you trying to compile GDB in linux or windows?

Cheers
Z

tejas15_10 06-15-2005 07:28 AM

hi,
i m compile GDB in Linux...and i m using red Hat 9.0...
i collect so many data and articles regarding to segmentation fault but i coudnt find any one which is suite for my application....
i have to complete this thing befor 2 days..still i m not able to solve it..reply me soon..waiting for any solution....
if possible then give me basic steps also...
Thanx in advance

zulfilee 06-15-2005 08:09 AM

Ok let me be clear

RedHat 9 CD had gdb rpm in it.

First check if your redhat has gdb installed in it

Type
rpm -q gdb
This will tell you if gdb is installed

If it has not been installed and if you have redhat CD

Loginto GUI

From the K Menu select Systemtools->AddRemovePackage
from it

Here select Development tools and click on update

It will ask you for the redhat CDs and Viola GDB is installed on your system

Cheers
Z

tejas15_10 06-17-2005 12:12 AM

zulfilee,thanx for reply me..
i checked my code in one software which gives memory leak problem and i found some of memory leakage points...so how much probability for linux to handle this memory leak problem..in window this code is worked very weill..
I have GDB
And also give me command to run gdb and debug my code..
If any one having any idea regarding to my problem then reply me back
Best regards,Tejas

tejas15_10 06-20-2005 01:36 AM

Hi to all
I solve the segmentation fault problem..but why it happen i dont knw.....Actually my application's folder level is like this:
R2Vector
||
========================
|| || || || ||
Image Jasper PDF GDFont Yahoo

i made make file for my application....now i give parameter to run my binary file..
yahoo folder contai all images on that i want to do something...
now when i give the input file path like "yahoo/yahoo.bmp" or "./yahoo/yahoo.bmp" it gives segmentation fault....
now if i put that file outside the yahoo..means in R2Vector folder then fault is remove..This is happening y that i want to knw...coz i coudnt put all file outside the yahoo....
Plz reply me back...
its very neede for me....
Thanks and Best regards
-Tejas

rsheridan6 06-20-2005 09:12 AM

You're probably going to actually have to understand what's happening to fix this and avoid similar mistakes. Somewhere in your program, you're accessing memory in an area that you're not supposed to, and that's when you get a segfault. Professional1983 already explained the mistakes that allow this to happen. If all you did was move a file to a different folder to fix it, it's not really fixed, it's just a latent bug. To really fix the bug, you have to find the point in the program where you're forgetting to allocate memory (or whatever the mistake is), which may not be anywhere near the point where it fails (memory management bugs are nasty). Or you could just use a language with automatic memory management.

I never really understood pointers, malloc, and free until I learned assembly language. Tools are nice, but they don't always replace real understanding.


All times are GMT -5. The time now is 05:53 PM.