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?

Dani AI

Generated

This is a classic case of undefined behavior: the same C source can appear to “work” under one toolchain and crash under another. As and pointed out, the problem is almost always a bug in the code rather than an OS quirk. and demonstrated a common example (attempting to alter immutable storage), which is useful to bear in mind.

A practical, repeatable workflow that narrows the fault quickly:

gcc -g -O0 -Wall -Wextra -Wformat -fsanitize=address,undefined -o prog prog.c
./prog
valgrind --leak-check=full ./prog
ulimit -c unlimited
./prog            # if it crashes, a core file is produced
gdb ./prog core   # in gdb: use `bt`, `frame 0`, `print <var>` to inspect

Subtle causes that often explain why one platform tolerates the bug while another fails: mismatched function prototypes or varargs/format mismatches that corrupt the stack; use-after-free or double-free; very large automatic arrays or deep recursion (stack overflow); incorrect casts or pointer truncation across 32/64-bit builds; mixed compilation flags / ABI differences. Static tools and sanitizers catch many of these: try cppcheck/clang-tidy, AddressSanitizer, and UBSan.

Short checklist to move from “it crashes” to “fixed”: enable strict compiler warnings and debug info, run sanitizers and valgrind, inspect a backtrace from gdb or a sanitizer report to find the offending function, check allocations/returns and bounds where that function operates, and reduce to a minimal reproducer. As suggested, a small reproducible example is the fastest way to a precise diagnosis.

Recommended Answers

All 12 Replies

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.

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.

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?

>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.

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.

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"?

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.

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

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);
}

Because you declare acName as a character pointer. That is, you cannot change individual letters. All you can do is have it point to another string. So you could write

#include<stdio.h>
#include<stdlib.h>

int main(void)
{
char *acName="hello daniweb";
*acName="H";
printf("%s\n",acName);
}

and the output would be H .

In order to capitalize the first letter, you have to declare acName as a character array. You do this by declaring it as an array of characters, but a null character must be included in the characters count. That is, "hello daniweb" has 13 characters. You add the null character and declare an array of 14 characters. The version below does what you want.

#include<stdio.h>
#include<stdlib.h>
void main()
{
char acName[14] = "hello daniweb";
*acName='H';
printf("%s\n",acName);
}
commented: Nicely explained with example code +4

just to elaborate on the above reply,

char *acName="hello daniweb";

acName points to an address of a string . that string is a seperate entity
and the "hello daniweb" is stored in a Read Only Memory
So when you try to change the memory location by doing

*acName='H';

, you are trying to change a Read Only Memory which is causing Segmentation fault.

and in second case

char acName[14] = "hello daniweb";

you have a normal variabel acName which you can play around with . [Obviously you can change the value of a variable] and it wont show any errors.

One more thing , this is a very old thread. Please put any more questions you have in a new thread.

One more thing , this is a very old thread. Please put any more questions you have in a new thread.

I actually think this is an awesome example of using the search feature :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.