Thread: problem with realloc()

  1. #1
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136

    problem with realloc()

    i m trying to understand dynamic memory allocation.

    this is what i tried...and failed at

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
        int *start,*values,i;
    
    /*hmm...i dunno lets allocate 20 integer size block, to be on the safe side*/
    
        values=(int *) malloc(20 * sizeof(int));
        start=values;
    
        printf(" add(start)=%u, add(value)=%u\n",start,values);
        printf("Enter the values(0 to stop)\n");
        for(i=0 ; i<10 ; i++)
        {
            scanf("%d",values); 
            printf("\tvalue(value)=%d\tadd(value)=%u\t",*values,values);  
            values++;
        }
    /* well, only 10 integers were stored! looks like i allocated more earlier, lets reduce the size :) */
    
        values =(int *) realloc(values,10 * sizeof(int));
    
        values=start;
        for(i=0 ; i<20 ; i++)
        {
            printf("%d\t%u\n",*values,values);
            values++;
        }
         return 0;
    }
    there are no errors or warnings in compilation
    when i run the program, the first half of program(taking in values) executes. after i enter the 10th number.

    i get an error message

    Code:
    *** glibc detected *** ./a.out: realloc(): invalid pointer: 0x081df030 ***
    ======= Backtrace: =========
    /lib/libc.so.6[0x48b3a4]
    /lib/libc.so.6(realloc+0x242)[0x48fdc2]
    /lib/libc.so.6(realloc+0x42)[0x48fbc2]
    ./a.out[0x80484f9]
    /lib/libc.so.6(__libc_start_main+0xe5)[0x4326e5]
    ./a.out[0x80483c1]
    ======= Memory map: ========
    00110000-00111000 r-xp 00110000 00:00 0          [vdso]
    003f7000-00417000 r-xp 00000000 08:02 805385327  /lib/ld-2.9.so
    00418000-00419000 r--p 00020000 08:02 805385327  /lib/ld-2.9.so
    00419000-0041a000 rw-p 00021000 08:02 805385327  /lib/ld-2.9.so
    0041c000-0058a000 r-xp 00000000 08:02 808903508  /lib/libc-2.9.so
    0058a000-0058c000 r--p 0016e000 08:02 808903508  /lib/libc-2.9.so
    0058c000-0058d000 rw-p 00170000 08:02 808903508  /lib/libc-2.9.so
    0058d000-00590000 rw-p 0058d000 00:00 0 
    00a6c000-00a79000 r-xp 00000000 08:02 808904130  /lib/libgcc_s-4.3.2-20081105.so.1
    00a79000-00a7a000 rw-p 0000c000 08:02 808904130  /lib/libgcc_s-4.3.2-20081105.so.1
    08048000-08049000 r-xp 00000000 08:02 809327092  /home/c_d/workspace/Info Security/RSA/a.out
    08049000-0804a000 rw-p 00000000 08:02 809327092  /home/c_d/workspace/Info Security/RSA/a.out
    081df000-08200000 rw-p 081df000 00:00 0          [heap]
    b7f8a000-b7f8b000 rw-p b7f8a000 00:00 0 
    b7fae000-b7fb1000 rw-p b7fae000 00:00 0 
    bfb9c000-bfbb1000 rw-p bffeb000 00:00 0          [stack]
    	1	136179756	Aborted
    isnt this how one is supposed to use realloc() ?
    what am i doing wrong here?

  2. #2
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    never mind. i figured it out.

    in the statement
    Code:
    values = realloc(values ,10 * sizeof(int));
    the pointer does not point to the start of values on account of "values++".

    all i had to do was to repoint it to the start of values using start.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Usage of realloc() is correct but not the semantics. realloc() acts on the pointer that was returned originally by the call to malloc() instead of a random one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 08:09 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. using realloc
    By bobthebullet990 in forum C Programming
    Replies: 14
    Last Post: 12-06-2005, 05:00 PM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM