473,395 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

GCC gives SEGFAULT... but GDB runs

Hi everybody,
While i'm compiling the following program in GCC, it gives
"segmentation fault"

But GDB doesn't give any "segmentation fault"

What's the reason for this??

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
main()
{

char name1[4];
int age1;
struct emp
{
char name[4];
int age;
};

struct emp *e1=malloc(sizeof(struct emp));

printf("enter name and age\n");
scanf("%s%d",name1,&age1);
printf("name1=%sage=%d\n",name1,age1);
e1->age=age1;
printf("\n%d",e1->age);
}

Thanks in advance,
Sethu

Mar 2 '07 #1
6 2964
On Mar 2, 2:20 pm, "seth...@gmail.com" <seth...@gmail.comwrote:
Hi everybody,
While i'm compiling the following program in GCC, it gives
"segmentation fault"

But GDB doesn't give any "segmentation fault"

What's the reason for this??
(snip)
char name1[4];
(snip)
scanf("%s%d",name1,&age1);
(snip)
Thanks in advance,
Sethu
You're using %s, with no maximum length limit, on scanf, so if the
user enters any more than 3 characters (most names are longer than
that) then your program will start behaving in an undefined manner;
segfaulting and appearing to execute normally are two possibilities,
and it seems you've encountered both. (If you search the comp.lang.c
archives, you'll see some more surprising possibilities as to what has
happened in similar circumstances; according to the C standards,
anything could happen). When scanf'ing in strings, you absolutely must
put a length limit (as in, "%3s") on the read, or you have no method
of preventing undefined behaviour. (You should probably make the
string somewhat larger than 3 chars + NUL, though).
--
ais523

Mar 2 '07 #2
"se*****@gmail.com" wrote:
>
While i'm compiling the following program in GCC, it gives
"segmentation fault"
If the compiler stops with a segmentation fault, it hasn't
generated an output file, and you have discovered a compiler bug.
I suspect your description is inadequate.
>
But GDB doesn't give any "segmentation fault"
GDB isn't a compiler. I would expect it to refuse to run the
program.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Mar 2 '07 #3
In article <45***************@yahoo.com>,
CBFalconer <cb********@maineline.netwrote:
>"se*****@gmail.com" wrote:
>>
While i'm compiling the following program in GCC, it gives
"segmentation fault"

If the compiler stops with a segmentation fault, it hasn't
generated an output file, and you have discovered a compiler bug.
I suspect your description is inadequate.
>>
But GDB doesn't give any "segmentation fault"

GDB isn't a compiler. I would expect it to refuse to run the
program.
In fact, as you point out earlier, "the program" would not exist, since
GCC segfaulted (per the OP's description) and, presumably, did not
create any output file.

Mar 2 '07 #4
On 2 Mar, 14:20, "seth...@gmail.com" <seth...@gmail.comwrote:
Hi everybody,
While i'm compiling the following program in GCC, it gives
"segmentation fault"
I presume you mean that you can compile (and link) the program with
(the) GCC (tool chain) but it fails with "segmentation fault" when you
run it. You should express yourself more clearly. It would also be
good to tell us what data you entered...
But GDB doesn't give any "segmentation fault"
By this I presume you mean that if you then try to run the program
with the GDB debugger, it runs successfully.
What's the reason for this??
I think ais523 has identified where the segmentation fault probably
comes from.

The reason that it runs without failure under GDB's control isn't
really a C question but
<Offtopic>
I expect that the way that data is laid out in memory when GDB runs
the program is different in such a way that the buffer overrun doesn't
immediately cause failure.
</Offtopic>

Mar 2 '07 #5
se*****@gmail.com wrote:
Hi everybody,
While i'm compiling the following program in GCC, it gives
"segmentation fault"

But GDB doesn't give any "segmentation fault"

What's the reason for this??

#include<stdio.h>
#include<string.h>
You're not using any function from string.h
#include<stdlib.h>
main()
Write this as int main(void)
{

char name1[4];
Isn't this rather small for holding a name? If an array write
overflows, anything can happen.
int age1;
struct emp
{
char name[4];
Similarly, if you must use a static array, use a more sane size like
32 or thereabouts.
int age;
};

struct emp *e1=malloc(sizeof(struct emp));

printf("enter name and age\n");
scanf("%s%d",name1,&age1);
The %s specifier tells scanf to scan an arbitrarily long sequence of
characters into the corresponding array. If more characters than the
array can hold are scanned, scanf will keep writing past the end of
the array, thus corrupting memory and invoking undefined behaviour.

If you must at all use scanf for reading strings, use a length
specifier to tell it to stop at a certain point. For example you
could've said:

scanf("%3s", name1);

A better way to input a line is to use fgets. scanf is not needed if
no parsing and conversion of input is required. In your example, you
could've done:

fgets(name1, sizeof(name1), stdin);

Do man 3 fgets or read your standard library's documentation for
properly using fgets.
printf("name1=%sage=%d\n",name1,age1);
Use a tab or a newline between name1 and age1.
e1->age=age1;
printf("\n%d",e1->age);
And terminate printf's output with a newline to force a write to the
screen. Otherwise output may appear delayed.
}
Mar 2 '07 #6
On Mar 2, 9:20 am, "seth...@gmail.com" <seth...@gmail.comwrote:
Hi everybody,
While i'm compiling the following program in GCC, it gives
"segmentation fault"

But GDB doesn't give any "segmentation fault"

What's the reason for this??

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
main()
{

char name1[4];
int age1;
struct emp
{
char name[4];
int age;
};

struct emp *e1=malloc(sizeof(struct emp));

printf("enter name and age\n");
scanf("%s%d",name1,&age1);
printf("name1=%sage=%d\n",name1,age1);
e1->age=age1;
printf("\n%d",e1->age);

}

Thanks in advance,
Sethu


As pointed out the reason for the undefined behaviour
(sefault when run outside of GDB, works correctly
inside of GDB (undefined behaviour means anything
can happen, including what you expect))
is that you have allocated insufficient storage
for name1. But you also need to make sure that
whatever limit you put on the size of name1 you
do not try to put more characters in. In the
words of the Great Prophet Henry Spencer:

Thou shalt check the array bounds of all strings (indeed, all
arrays),
for surely where thou typest``foo'' someone someday shall type
``supercalifragilisticexpialidocious''

Why the difference between the behaviour inside and
outside GDB? Who knows? Possibly:

GDB put some extra space after data1,
space it might need to use at some
point (or maybe not). When you ran outside of GDB
there was no extra space, so your program tried
to write to memory it did not own, thus causing
your operating system to segfault ( a GOOD THING,
there are much worse things than a segfault.)

This type of behaviour is frequently seen. Often when
a program is compiled in debug mode everything is fine,
but compile in optimized mode (in general less forgiving
of buffer overruns) and the program segfaults.

Usually the senario is something like this: "Well after
one year, the 10 member team has finished the product.
We'll just switch to optimized mode for the shipping version
..... ARGGHHHH!!!!"

- William Hughes

Mar 2 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Nathaniel Echols | last post by:
I've written a function in C to perform protein sequence alignment. This works fine in a standalone C program. I've added the necessary packaging to use it in Python; it returns three strings and...
6
by: Stefan Behnel | last post by:
Hi! In Python 2.4b3, the deque is causing a segfault on two different machines I tested on. With deque, my program runs fine for a while (at least some tens of seconds up to minutes) and then...
0
by: dale | last post by:
Python newbie disclaimer on I am running an app with Tkinter screen in one thread and command-line input in another thread using raw_input(). First question - is this legal, should it run...
5
by: Vedran Vyroubal | last post by:
Hi all! I have a problem with STL string. My program segfaults after a period of time, it can run perfectly for days and than die after a week, and when it segfaults it doesn't have to be at the...
11
by: H.A. Sujith | last post by:
The following code is causing a segfault at the first if statement. Am I doing something wrong or is it a compiler bug? //---------- #include <stdio.h> int main(int argc, char *argv) { int...
6
by: Code Raptor | last post by:
Folks, I am hitting a segfault while free()ing allocated memory - to make it short, I have a linked list, which I try to free node-by-node. While free()ing the 28th node (of total 40), I hit a...
3
by: kj | last post by:
I am trying to diagnose a bug in my code, but I can't understand what's going on. I've narrowed things down to this: I have a function, say foo, whose signature looks something like: int foo(...
1
by: Miles Lubin | last post by:
I am using PyArg_ParseTuple to parse the arguments (ignoring the keyword arguments) to my initproc for a type I define. It seems that something goes wrong inside PyArg_ParseTuple when it gets the...
18
by: Prasad | last post by:
Hi folks, I am trying to debug the following program. Debugging the core file revealed segfault at possibleFlows=0 when values of states=36, labels=40. It works fine for lesser values. What...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.