CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Threaded View

  1. #1
    Join Date
    Oct 2007
    Posts
    63

    realloc() : invalid pointer ?

    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)=&#37;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?
    Last edited by creeping death; February 28th, 2009 at 11:39 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured