#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    1
    Rep Power
    0

    I need some help to solved this error


    i got this following error when run my code.I'm write the code on Linux

    *** glibc detected *** ./program: munmap_chunk(): invalid pointer: 0xbfabaa0c ***
    ======= Backtrace: =========
    /lib/i386-linux-gnu/i686/cmov/libc.so.6(+0x70f01)[0xb7691f01]
    /lib/i386-linux-gnu/i686/cmov/libc.so.6(+0x7217e)[0xb769317e]
    ./program[0x8048528]
    /lib/i386-linux-gnu/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0xb7637e46]
    ./program[0x80483b1]
    ======= Memory map: ========
    08048000-08049000 r-xp 00000000 08:01 3026791 /root/LUNDU/Lundu/coding/Cprogram/program
    08049000-0804a000 rw-p 00000000 08:01 3026791 /root/LUNDU/Lundu/coding/Cprogram/program
    09736000-09757000 rw-p 00000000 00:00 0 [heap]
    b75e7000-b7603000 r-xp 00000000 08:01 2228998 /lib/i386-linux-gnu/libgcc_s.so.1
    b7603000-b7604000 rw-p 0001b000 08:01 2228998 /lib/i386-linux-gnu/libgcc_s.so.1
    b7620000-b7621000 rw-p 00000000 00:00 0
    b7621000-b777d000 r-xp 00000000 08:01 2228914 /lib/i386-linux-gnu/i686/cmov/libc-2.13.so
    b777d000-b777e000 ---p 0015c000 08:01 2228914 /lib/i386-linux-gnu/i686/cmov/libc-2.13.so
    b777e000-b7780000 r--p 0015c000 08:01 2228914 /lib/i386-linux-gnu/i686/cmov/libc-2.13.so
    b7780000-b7781000 rw-p 0015e000 08:01 2228914 /lib/i386-linux-gnu/i686/cmov/libc-2.13.so
    b7781000-b7784000 rw-p 00000000 00:00 0
    b779f000-b77a2000 rw-p 00000000 00:00 0
    b77a2000-b77a3000 r-xp 00000000 00:00 0 [vdso]
    b77a3000-b77bf000 r-xp 00000000 08:01 2228950 /lib/i386-linux-gnu/ld-2.13.so
    b77bf000-b77c0000 r--p 0001b000 08:01 2228950 /lib/i386-linux-gnu/ld-2.13.so
    b77c0000-b77c1000 rw-p 0001c000 08:01 2228950 /lib/i386-linux-gnu/ld-2.13.so
    bfa9b000-bfabc000 rw-p 00000000 00:00 0 [stack]
    Aborted

    here is the code:

    Code:
    #include <stdio.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    struct BOOK_INFO{
    	char *title;
    	char *author;
    	char *publisher;
    	long *year;
    }BI;
    
    int main (void) {
    	long yr=2006;
    	
    	
    	
    	BI.title="programing using c language";
    	BI.author="Lundu Harianja";
    	BI.publisher="INFORMATIKA Bandung";
    	BI.year=(long *) malloc(sizeof(long));
    	BI.year=&yr;
    	
    	printf("title \t\t:%s\n",BI.title);
    	printf("author \t:%s\n",BI.author);
    	printf("publisher \t:%s\n",BI.publisher);
    	printf("year \t\t:%d\n",*(BI.year));
    	
    	free(BI.year);
    }
    please help me to solved such error and what's wrong with my code?
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    236
    Rep Power
    28
    Hi!

    You'd better learn to search for such errors yourself. For example you can insert dummy prints or returns, or comment out fragments of code and see, whether problem exists.

    Here if you comment the last line (free(BI.year)) - you'll see that here are no error.

    This should lead you to conclusion that BI.year pointer was some way broken, after it was assigned memory address from malloc.

    Then you easily conclude that "BI.year=&yr;" assignes new address to the pointer, which refers to statically allocated memory, which could not be freed. Meanwhile pointer to allocated memory chunk is completely lost.

    That is why "free" ends with error.

    Comments on this post

    • salem agrees
    CodeAbbey - programming problems for novice coders
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    7
    Rep Power
    0
    You include stdio.h twice.
  6. #4
  7. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,075
    Rep Power
    1802
    Originally Posted by dingmutt
    You include stdio.h twice.
    ... but that is not the cause of his problem and entirely harmless.
  8. #5
  9. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,075
    Rep Power
    1802
    Originally Posted by rodiongork
    For example you can insert dummy prints or returns, or comment out fragments of code and see, whether problem exists.
    Why does no one ever advocate actually using a debugger!? Modifying broken code to find an error changes the code and may change the behaviour with respect to the error. It is sometimes but should be your last rather than your first resort.

IMN logo majestic logo threadwatch logo seochat tools logo