Thread: Rediculous stack error (core dumped) I dont understand

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    93

    Rediculous stack error (core dumped) I dont understand

    I am getting a rediculous console error I dont understand on the following code

    I have a structure that looks like this

    Code:
    struct courses{
    	int courseNumber;
    	char courseCode[5];
    	double rating_interest;
    	double rating_teaching;
    	double rating_evaluation;
    	double rating_birdFactor;
    	int voteCast_Count;
    	double rating_total_interest;
    	double rating_total_teaching;
    	double rating_total_evaluation;
    	double rating_total_birdfactor;
    }courses[__COURSE__AMOUNT__];
    Where __COURSE__AMOUNT__ is a macro.

    I have a function with the code

    Code:
    courses[i].voteCast_Count += 1;
    This will produce the core dump message
    Code:
    *** glibc detected *** ./srate: free(): invalid pointer: 0x0804b008 ***
    ======= Backtrace: =========
    /lib/tls/i686/cmov/libc.so.6[0xb7ed17cd]
    /lib/tls/i686/cmov/libc.so.6(cfree+0x90)[0xb7ed4e30]
    /lib/tls/i686/cmov/libc.so.6(fclose+0x134)[0xb7ec0844]
    ./srate[0x8048d71]
    ./srate[0x8049984]
    /lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xdc)[0xb7e7febc]
    ./srate[0x8048b31]
    ======= Memory map: ========
    08048000-0804a000 r-xp 00000000 08:01 8390941    /home/aaron/C/
    0804a000-0804b000 rw-p 00001000 08:01 8390941    /home/aaron/C/
    0804b000-0806c000 rw-p 0804b000 00:00 0          [heap]
    b7d00000-b7d21000 rw-p b7d00000 00:00 0 
    b7d21000-b7e00000 ---p b7d21000 00:00 0 
    b7e5d000-b7e68000 r-xp 00000000 08:01 8339520    /lib/libgcc_s.so.1
    b7e68000-b7e69000 rw-p 0000a000 08:01 8339520    /lib/libgcc_s.so.1
    b7e69000-b7e6a000 rw-p b7e69000 00:00 0 
    b7e6a000-b7fa5000 r-xp 00000000 08:01 8342756    /lib/tls/i686/cmov/libc-2.5.so
    b7fa5000-b7fa6000 r--p 0013b000 08:01 8342756    /lib/tls/i686/cmov/libc-2.5.so
    b7fa6000-b7fa8000 rw-p 0013c000 08:01 8342756    /lib/tls/i686/cmov/libc-2.5.so
    b7fa8000-b7fab000 rw-p b7fa8000 00:00 0 
    b7fb8000-b7fba000 rw-p b7fb8000 00:00 0 
    b7fba000-b7fd3000 r-xp 00000000 08:01 8339477    /lib/ld-2.5.so
    b7fd3000-b7fd5000 rw-p 00019000 08:01 8339477    /lib/ld-2.5.so
    bfa79000-bfa8f000 rw-p bfa79000 00:00 0          [stack]
    ffffe000-fffff000 r-xp 00000000 00:00 0          [vdso]
    Aborted (core dumped)
    Whereas the following code after the code above in the same function works

    Code:
    courses[i].rating_total_interest = courses[i].rating_total_interest + (double)atoi(token);
    		courses[i].rating_interest = courses[i].rating_total_interest / courses[i].voteCast_Count;
    has no crash and performs as expected.

    Why is this and how can I resolve it?

  2. #2
    Registered User
    Join Date
    Nov 2004
    Posts
    93

    Question

    Quote Originally Posted by INFERNO2K View Post
    I am getting a rediculous console error I dont understand on the following code

    I have a structure that looks like this

    Code:
    struct courses{
    	int courseNumber;
    	char courseCode[5];
    	double rating_interest;
    	double rating_teaching;
    	double rating_evaluation;
    	double rating_birdFactor;
    	int voteCast_Count;
    	double rating_total_interest;
    	double rating_total_teaching;
    	double rating_total_evaluation;
    	double rating_total_birdfactor;
    }courses[__COURSE__AMOUNT__];
    Where __COURSE__AMOUNT__ is a macro.

    I have a function with the code

    Code:
    courses[i].voteCast_Count += 1;
    This will produce the core dump message
    Code:
    *** glibc detected *** ./srate: free(): invalid pointer: 0x0804b008 ***
    ======= Backtrace: =========
    /lib/tls/i686/cmov/libc.so.6[0xb7ed17cd]
    /lib/tls/i686/cmov/libc.so.6(cfree+0x90)[0xb7ed4e30]
    /lib/tls/i686/cmov/libc.so.6(fclose+0x134)[0xb7ec0844]
    ./srate[0x8048d71]
    ./srate[0x8049984]
    /lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xdc)[0xb7e7febc]
    ./srate[0x8048b31]
    ======= Memory map: ========
    08048000-0804a000 r-xp 00000000 08:01 8390941    /home/aaron/C/
    0804a000-0804b000 rw-p 00001000 08:01 8390941    /home/aaron/C/
    0804b000-0806c000 rw-p 0804b000 00:00 0          [heap]
    b7d00000-b7d21000 rw-p b7d00000 00:00 0 
    b7d21000-b7e00000 ---p b7d21000 00:00 0 
    b7e5d000-b7e68000 r-xp 00000000 08:01 8339520    /lib/libgcc_s.so.1
    b7e68000-b7e69000 rw-p 0000a000 08:01 8339520    /lib/libgcc_s.so.1
    b7e69000-b7e6a000 rw-p b7e69000 00:00 0 
    b7e6a000-b7fa5000 r-xp 00000000 08:01 8342756    /lib/tls/i686/cmov/libc-2.5.so
    b7fa5000-b7fa6000 r--p 0013b000 08:01 8342756    /lib/tls/i686/cmov/libc-2.5.so
    b7fa6000-b7fa8000 rw-p 0013c000 08:01 8342756    /lib/tls/i686/cmov/libc-2.5.so
    b7fa8000-b7fab000 rw-p b7fa8000 00:00 0 
    b7fb8000-b7fba000 rw-p b7fb8000 00:00 0 
    b7fba000-b7fd3000 r-xp 00000000 08:01 8339477    /lib/ld-2.5.so
    b7fd3000-b7fd5000 rw-p 00019000 08:01 8339477    /lib/ld-2.5.so
    bfa79000-bfa8f000 rw-p bfa79000 00:00 0          [stack]
    ffffe000-fffff000 r-xp 00000000 00:00 0          [vdso]
    Aborted (core dumped)
    Whereas the following code after the code above in the same function works

    Code:
    courses[i].rating_total_interest = courses[i].rating_total_interest + (double)atoi(token);
    		courses[i].rating_interest = courses[i].rating_total_interest / courses[i].voteCast_Count;
    has no crash and performs as expected.

    Why is this and how can I resolve it?
    ** Solved **

    Made the votecast a double. Why would this make a difference, I am curious

  3. #3
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    ** Solved **

    Made the votecast a double. Why would this make a difference, I am curious
    This doesn't make any sense at all. Personnaly, i don't understand the error message. Maybe your compiler is crazy, or there are some semantics errors in your program, but what i know is that
    Code:
    courses[i].voteCast_Count += 1;
    is a valid instruction for every base type of C (even if i is out of bond).

    Maybe someone else has the answer.

  4. #4
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    The program seg faulted. Here it seems like he was trying to free a pointer which didn't point to allocated memory anymore. This could essentially have happened anywhere, and he may have only seen the consequence at that statement. Case in point, I doubt that what he did actually fixed the problem. It only hid the consequence. However, based on the code supplied, my guess is as good as yours as to what the real problem is.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    It's a sure bet that whatever he did, it didn't really fix the problem, it just pushed it under the carpet again. It will be back.

    /lib/tls/i686/cmov/libc.so.6(fclose+0x134)[0xb7ec0844]
    ./srate[0x8048d71]
    ./srate[0x8049984]
    /lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xdc)[0xb7e7febc]

    At least compile with debug symbols so that the stack trace has meaningful names in your code as well.

    My reading of this is
    0x8049984 is going to be main(), because that's what libc_start_main will call.
    0x8048d71 is one of your functions, and from the look of things, it has just called fclose().

    I didn't see an fclose() in any of your analysis, so I'm doubtful as to what you've fixed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Quote Originally Posted by Salem View Post
    It's a sure bet that whatever he did, it didn't really fix the problem, it just pushed it under the carpet again. It will be back.
    Funny... I was thinking the same thing.... somehow I don't think casting at random would solve this problem.

  7. #7
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Quote Originally Posted by INFERNO2K View Post
    ** Solved **

    Made the votecast a double. Why would this make a difference, I am curious
    Here's the kicker...

    Hint: IF you dunno why something works, but it does, chances are you just got lucky.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Otherwise know as the acupuncture cure.
    "My leg is broken, but the pain goes away if I stick this needle in my arm".

    Maybe so, but all you've done is mask the symptom, you haven't addressed the real problem.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My little stack library - what do you think?
    By hauzer in forum C Programming
    Replies: 6
    Last Post: 10-17-2008, 09:01 AM
  2. Stack Implementation and Exceptions
    By audinue in forum C Programming
    Replies: 4
    Last Post: 06-22-2008, 09:32 AM
  3. Stack problem - I've hit a wall!
    By miniwhip in forum C Programming
    Replies: 7
    Last Post: 11-14-2007, 03:05 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. inputting line of text vs. integers in STACK
    By sballew in forum C Programming
    Replies: 17
    Last Post: 11-27-2001, 11:23 PM