segmentation fault

Reply

Join Date: Jan 2005
Posts: 33
Reputation: kloony is an unknown quantity at this point 
Solved Threads: 0
kloony kloony is offline Offline
Light Poster

segmentation fault

 
0
  #1
Mar 2nd, 2005
Can anyone please explain to me the following strange phenomenon?

I have a code in C, and when I compile it is Windows Platform compiler (Borland 5.5), they compiling is fine and when I execute the program, everything turns out ok.

However, when I use the same code and compile it in UNIX, the compiling is ok but when I execute the program I get a "Segmentation fault" message?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,315
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 229
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: segmentation fault

 
0
  #2
Mar 2nd, 2005
There is a bug in the code. You are fortunate that the UNIX platform at least shows you this; in Windows it is just "lucky" to be running.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: segmentation fault

 
0
  #3
Mar 2nd, 2005
How is that strange? On Windows you didn't touch memory that would cause a critical error, on Unix you did. It means your code is broken. Look for uninitialized pointers and out of bound array accesses. Those are the prime culprits for a seg fault.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 33
Reputation: kloony is an unknown quantity at this point 
Solved Threads: 0
kloony kloony is offline Offline
Light Poster

Re: segmentation fault

 
0
  #4
Mar 2nd, 2005
if there is a bug in the code, why doesn't it show when I compile?
how can i rectify the problem?
I thought segmentation fault has got to do with memory errors...
or is this a portability issue?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: segmentation fault

 
0
  #5
Mar 2nd, 2005
>if there is a bug in the code, why doesn't it show when I compile?
*stunned silence*

>how can i rectify the problem?
Take my suggestions and look for off by one errors when you index an array or places you might have an uninitialized pointer.

>I thought segmentation fault has got to do with memory errors...
Yes. If you access memory outside of your address space, you get a segmentation fault.

>or is this a portability issue?
No, it's a programmer issue. Fix your broken code.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 353
Reputation: Asif_NSU is on a distinguished road 
Solved Threads: 2
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

Re: segmentation fault

 
0
  #6
Mar 3rd, 2005
Yes, segmentation fault has chased me long enough when i was learning pointers. Most of the time it gotta do something with the pointers or array. It occurs when...

1. U de-reference a NULL pointer.
2. U de-reference an uninitialized pointer --which results in accessing memory outside of ur memory space.
3. Indexing an array where the index is out of the arrays boundary---- which also means accessing not permitted memory space.
That are the three reasons i can rem for now.

As far as i know the program should have crashed in Windows pc if it were the first case. Since it works with windows itz perhaps either the second or the third case. Look for array bounds, especially in Loops.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 33
Reputation: kloony is an unknown quantity at this point 
Solved Threads: 0
kloony kloony is offline Offline
Light Poster

Re: segmentation fault

 
0
  #7
Mar 3rd, 2005
I have been trying to find out where the bug is but so far I have no luck.
Is it possible to have segmentation fault when the system "run out of memory"?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: segmentation fault

 
0
  #8
Mar 3rd, 2005
It depends on what you mean by "run out of memory". If malloc fails then it returns a null pointer, which would cause a seg fault if you don't test for failure. However, malloc failure is extremely unlikely these days, so the problem is still probably something you did wrong. If running out of "stack" space for automatic variables caused a segmentation fault, I would still be suspicious of your code because Unix is very good about growing the "stack" if needed. So if you're actually running out of memory (meaning fast memory and virtual memory), there's a lot more wrong with your code than a little error.

Unfortunately, memory errors are very difficult to find, and without actual code to look at, we're just making educated guesses.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 1,620
Reputation: kc0arf is a jewel in the rough kc0arf is a jewel in the rough kc0arf is a jewel in the rough 
Solved Threads: 50
Team Colleague
kc0arf kc0arf is offline Offline
Posting Virtuoso

Re: segmentation fault

 
0
  #9
Mar 3rd, 2005
Hello,

Please post your code. Let's take a look at it. Please use CODE tags when you do it, so that it is nice and clean to read.

Christian
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC