CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  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.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: realloc() : invalid pointer ?

    What you are doing wrong is
    Code:
    values++;
    in the loop. After that, values no longer points to the array returned by malloc(), so it is improper to pass it to realloc.

    However, your start pointer should still be pointed at the proper location. Try passing that instead.

    Also, no program may be considered correct if it does not free() all allocated memory before program termination.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: realloc() : invalid pointer ?

    Code:
        values =(int *) realloc(values,10 * sizeof(int));
    
        values=start;
        for(i=0 ; i<20 ; i++)
    See the problem in red?

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Oct 2007
    Posts
    63

    Re: realloc() : invalid pointer ?

    Quote Originally Posted by Lindley View Post
    What you are doing wrong is
    Code:
    values++;
    in the loop. After that, values no longer points to the array returned by malloc(), so it is improper to pass it to realloc.

    However, your start pointer should still be pointed at the proper location. Try passing that instead.
    thanks. i forgot about that basic point.

    Also, no program may be considered correct if it does not free() all allocated memory before program termination.
    that point taken too! thanks.

  5. #5
    Join Date
    Oct 2007
    Posts
    63

    Re: realloc() : invalid pointer ?

    Quote Originally Posted by Paul McKenzie View Post
    Code:
        values =(int *) realloc(values,10 * sizeof(int));
    
        values=start;
        for(i=0 ; i<20 ; i++)
    See the problem in red?

    Regards,

    Paul McKenzie
    i willingly set i till 20 because i wanted to see if i'd get any runtime errors because i have deallocated those memory locations...

    i dont get any errors.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: realloc() : invalid pointer ?

    Quote Originally Posted by creeping death View Post
    i willingly set i till 20 because i wanted to see if i'd get any runtime errors because i have deallocated those memory locations...

    i dont get any errors.
    That is because accessing memory that has been deallocated is undefined behaviour. This means that anything can happen -- work all the time, crash all the time, work some of the time and crash other times, work at 12:00 on Tuesday and fail at 5:00 PM on a Wednesday, etc...

    Regards,

    Paul McKenzie

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: realloc() : invalid pointer ?

    Paul is correct. However, some practical knowledge which may help:

    Since you're only reading from invalid locations, odds are nothing will go wrong until you try to do a calculation with the retrieved values. Since all you're doing is outputting them, the worst that will (probably) happen is you might get wonky output.

    If you were to *write* to an invalid location, that is far more likely to cause a runtime error. Either an immediate segmentation fault or, under some circumstances, you may get a "heap corruption detected" message the next time you try to allocate or free memory. The fact that this message may not correspond to the location where the corruption occurred is a particularly troublesome issue.

    One of the many reasons to prefer std::vector over bare arrays in C++ is because it will (under Windows at least) throw an exception immediately if you try to access an invalid array index. It is *much* better to catch these things as they happen (or even at compile time if possible) than to let them pass unremarked and try to figure out what went wrong when things start breaking further down the line.

  8. #8
    Join Date
    Oct 2007
    Posts
    63

    Re: realloc() : invalid pointer ?

    thanks for the useful insights!!


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