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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
12-06-2006, 11:15 PM
|
#1
|
|
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:
|
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?
|
|
|
|
12-06-2006, 11:21 PM
|
#2
|
|
Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,861
Rep: 
|
Code:
#include <stdio.h>
main()
{
int age;
printf("How old are you?\n");
scanf(" %d", &age);
printf("You are %d years old.", age);
}
|
|
|
|
12-06-2006, 11:28 PM
|
#3
|
|
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:
|
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.
|
|
|
|
12-06-2006, 11:42 PM
|
#4
|
|
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530
Rep:
|
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. 
|
|
|
|
12-06-2006, 11:58 PM
|
#5
|
|
Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,861
Rep: 
|
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
|
|
|
|
12-07-2006, 12:42 AM
|
#6
|
|
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:
|
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!
|
|
|
|
12-07-2006, 01:03 AM
|
#7
|
|
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530
Rep:
|
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:
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 09:00 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|