LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 12-06-2006, 11:15 PM   #1
Micro420
Senior Member
 
Registered: Aug 2003
Location: Berkeley, CA
Distribution: Mac OS X Leopard 10.6.2, Windows 2003 Server/Vista/7/XP/2000/NT/98, Ubuntux64, CentOS4.8/5.4
Posts: 2,986

Rep: Reputation: 45
segmentation fault (core dumped) - what???


I am learning the C language so I wrote a simple little program that would spit back my age.
Code:
#include <stdio.h>
main()

{
    int age;
    printf("How old are you?\n");
    scanf(" %d", age);
    printf("You are %d years old.", age);
}
I then run
Code:
cc filename.c -o age
I execute the file and then I get the segmentation fault (core dumped). What does this mean?
 
Old 12-06-2006, 11:21 PM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Code:
#include <stdio.h>
main()
{
    int age;
    printf("How old are you?\n");
    scanf(" %d", &age);
    printf("You are %d years old.", age);
}
 
Old 12-06-2006, 11:28 PM   #3
Micro420
Senior Member
 
Registered: Aug 2003
Location: Berkeley, CA
Distribution: Mac OS X Leopard 10.6.2, Windows 2003 Server/Vista/7/XP/2000/NT/98, Ubuntux64, CentOS4.8/5.4
Posts: 2,986

Original Poster
Rep: Reputation: 45
Whoops! That fixed the problem! Thanks!

Now I must expand my code to only accept numbers or else it will loop again until the user inputs an age. That's my next challenge

Last edited by Micro420; 12-06-2006 at 11:30 PM.
 
Old 12-06-2006, 11:42 PM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 62
Quote:
Originally Posted by Micro420
I execute the file and then I get the segmentation fault (core dumped). What does this mean?
A segmentation fault occurs when a program tried to access memory it has not been told it can use by the OS. Memory is split into segments. If a program tries to read or write a memory address from a segment it has not been allocated, the OS sends a signal (SIGSEGV) to the process, telling it "naughty boy!", and by default the process falls over with this error message.

"core dumped" means the state of the program is written to a file called "core". This is helpful for debuggers which can read the core file and work out where the program crashed, the values in the variables, registers, what was on the stack and so on.

When you use scanf, you have to pass the memory address into which the input will be written by the scanf function. You passed the value of the integer "age". age is probably 0 or some random number at the point scanf gets it (it hasn't been assigned to, so officially it's value is undefined). This random value is almost certainly not a memory address in a segment which has been allocated to the program, hence the segmentation fault. The correction paulsm4 provided shows the syntax specifying the address of the integer "age".

Addresses and pointers to variables is a tricky subject to start with. Don't worry - you'll get a lot of core dumps before you think you understand it, and then a whole lot more before you actually understand it.
 
Old 12-06-2006, 11:58 PM   #5
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

matthewg42 gave a very good explanation for both "What is a 'segmentation violation'?" and "Why do I need to use the funny '&' operator in scanf?". Please feel free to ask any follow-on questions you might have.

This post is in reply to your "how do I expand my code" question.

Obviously, we can't do your homework for you. But the code in red suggests one possible solution to the problem. Run the code, read the "man" page for scanf ... and see if it leads you where you want to go.

The code in blue is "just good to have" - I would suggest using the full signature for "main()" and always passing a return value whether or not you actually need to. It's just, IMHO, good form.

Code:
#include <stdio.h>

int
main(int argc, char *argv[])
{
    int age;
    int result;

    printf("How old are you?\n");
    result = scanf(" %d", &age);
    printf ("result was %d\n", result);
    printf("You are %d years old.", age);

    return 0;
}
Hope that helps .. PSM
 
Old 12-07-2006, 12:42 AM   #6
Micro420
Senior Member
 
Registered: Aug 2003
Location: Berkeley, CA
Distribution: Mac OS X Leopard 10.6.2, Windows 2003 Server/Vista/7/XP/2000/NT/98, Ubuntux64, CentOS4.8/5.4
Posts: 2,986

Original Poster
Rep: Reputation: 45
Thanks for your detailed explanation, Matthewg42. I don't know all the little details, but what you're trying to say is that the input was being stored improperly, correct? Just out of curiosity, where is this core dump file so that I can debug? I'm just curious to see what is in this file. I've read a little bit about arrays and I understand the concepts, but I'm not sure if I am ready to apply them into something practical. Will keep trying different things.

paulsm4, thanks for the suggestion, but I haven't gotten to the result = yet. I'm still learning the if-else, while, case, and conditional loops. Supposedly I should be be able to only accept numbers and reject characters and symbols. I'll have to think about this logically for awhile. I'm trying to do this without referencing my C programming book. By the way, I am reading this book called Absolute Beginners Guide to Programming in C. Best C programming book I have read so far!
 
Old 12-07-2006, 01:03 AM   #7
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 62
Quote:
Originally Posted by Micro420
Thanks for your detailed explanation, Matthewg42. I don't know all the little details, but what you're trying to say is that the input was being stored improperly, correct?
More or less

Quote:
Originally Posted by Micro420
Just out of curiosity, where is this core dump file so that I can debug? I'm just curious to see what is in this file.
The file will be in the directory in which you executed the program, although there is a shell setting which might prevent it actually appearing. If you type:
Code:
ulimit -c
and get a 0 back, it means the core fill won't be written. You can change this by using the command
Code:
ulimit -c unlimited
This setting only affects the shell you are in until that shell terminates. Once this is set, re-run the program and the core file should be there when you have a core dump happen.

To use the core dump, you need to use the debugger. This is usually gdb. It looks ugly if you're used to a graphical debugger interface, but it's actually quite flexible. There are plenty of graphical front-ends to it, but it pays dividends to learn gdb directly.

To use the debugger, you need to make sure that you compiled your program using the -g option to gcc. This includes debugging symbols in the executable, and allows gdb to know what line of sourcecode is being executed for a given part of the binary.

So, in summary:
Code:
gcc -g yourprogram.c -o yourprogram
ulimit -c unlimited
./yourprogram
(core dump happens, core file is created)
gdb yourprogram core
At this point you can enter gdb commands like "bt" to print a backtrace of the functions which were being executed when the error occurred.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Segmentation fault (core dumped) eytan *BSD 3 04-27-2005 08:38 PM
Segmentation Fault (core dumped) newuser455 Linux - Software 3 08-28-2004 02:39 PM
Segmentation fault (core dumped) plisken Linux - General 8 09-17-2003 03:32 AM
Segmentation fault (core dumped) ooops tarballed Linux - General 3 07-25-2002 10:59 AM
Segmentation fault (core dumped) hasanaydin Linux - General 0 03-27-2002 07:47 AM


All times are GMT -5. The time now is 11:31 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration