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.
Reference:
http://www.linuxquestions.org/questions/programming-9/segmentation-fault-core-dumped-what-508083/
http://stupefydeveloper.blogspot.ca/2008/10/gdb-examining-core-dumps.html
http://www.cprogramming.com/debugging/segfaults.html
http://stackoverflow.com/questions/1518711/c-programming-how-does-free-know-how-much-to-free
http://stackoverflow.com/questions/1963745/how-does-free-know-how-much-memory-to-deallocate
http://stackoverflow.com/questions/851958/where-do-malloc-free-store-allocated-sizes-and-addresses
http://stackoverflow.com/questions/1119134/how-do-malloc-and-free-work
http://stackoverflow.com/questions/3923784/how-does-the-free-function-work-in-c
http://www.memorymanagement.org/
Tuesday, June 4, 2013
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment