943,932 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 22562
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 2nd, 2005
0

segmentation fault

Expand Post »
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?
Similar Threads
Reputation Points: 11
Solved Threads: 0
Light Poster
kloony is offline Offline
33 posts
since Jan 2005
Mar 2nd, 2005
0

Re: segmentation fault

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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Mar 2nd, 2005
0

Re: segmentation fault

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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 2nd, 2005
0

Re: segmentation fault

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?
Reputation Points: 11
Solved Threads: 0
Light Poster
kloony is offline Offline
33 posts
since Jan 2005
Mar 2nd, 2005
0

Re: segmentation fault

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 3rd, 2005
0

Re: segmentation fault

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.
Reputation Points: 113
Solved Threads: 3
Posting Whiz
Asif_NSU is offline Offline
353 posts
since Apr 2004
Mar 3rd, 2005
0

Re: segmentation fault

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"?
Reputation Points: 11
Solved Threads: 0
Light Poster
kloony is offline Offline
33 posts
since Jan 2005
Mar 3rd, 2005
0

Re: segmentation fault

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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 3rd, 2005
0

Re: segmentation fault

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
Team Colleague
Reputation Points: 121
Solved Threads: 57
Posting Virtuoso
kc0arf is offline Offline
1,629 posts
since Mar 2004
Sep 13th, 2010
0
Re: segmentation fault
Hi all,
when i run this piece of code in C it works properly, but in unix it shows segmentation fault. Help me resolve this.
#include<stdio.h>
#include<stdlib.h>
void main()
{
char *acName="hello daniweb";
*acName='H';
printf("%s",acName);
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gurushankar is offline Offline
9 posts
since Sep 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: gets
Next Thread in C Forum Timeline: Socket and pthread conflict (and Makefile)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC