Thread: Quesion about realloc()

  1. #1
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303

    Quesion about realloc()

    Consider this snippet:

    Code:
    int main()
    {
        int *pInt;
        int i;
    
        pInt = malloc(200 * sizeof(int));
    
        for (i = 0; i < 300; i++)
            *(pInt + i) = 1;
    
        pInt = realloc (pInt, 400 * sizeof(int));
    
        return 0;
    }
    The program is quite happy to give me the benefit of the doubt in writing ints past the 200th place of the memory I allocated with malloc(). However, the realloc() causes this error:

    Heap block at 00301B40 modified at 00301E8C past requested size of 344
    I know this is probably a dumb question, but what does realloc() care that I assigned to out of bounds memory in the for-loop? Why doesn't it just reallocate the block I originally asked for and have done with it?

  2. #2
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    realloc needs to examine the pointer you pass it in order to determine how it's going to reallocate the memory. It may end up moving it or simply expanding the block. It is possible that malloc keeps some of this data past the end of the allocated block. As such, over-writing whatever information it had stored causes the crash.

    Basically, whenever you enter "undefined" territory, things like this are bound to happen.
    Stick close to your desks and never program a thing,
    And you all may sit in the standards commitee!

  3. #3
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    realloc needs to examine the pointer you pass it in order to determine how it's going to reallocate the memory. It may end up moving it or simply expanding the block. It is possible that malloc keeps some of this data past the end of the allocated block. As such, over-writing whatever information it had stored causes the crash.
    Do you meant that malloc() stores information about the block in an extra hunk of memory on the end of what I asked for, and I'm over-writing this block information in the for-loop? I presume then that this must be the extra 36 bytes it's telling me was requested in the error message?

  4. #4
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    Quote Originally Posted by Sharke View Post
    Do you meant that malloc() stores information about the block in an extra hunk of memory on the end of what I asked for, and I'm over-writing this block information in the for-loop? I presume then that this must be the extra 36 bytes it's telling me was requested in the error message?
    It's possible, yes. Of course, the crash may disappear in other implementations that don't do such a thing or even if you write less bytes than you're doing now.
    Stick close to your desks and never program a thing,
    And you all may sit in the standards commitee!

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I think Salem's signature sums it up quite well:

    "If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut."
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Registered User jephthah's Avatar
    Join Date
    May 2010
    Location
    seattle
    Posts
    49
    this is also a good example why you should not automatically assign the return of realloc() to your original pointer.

    good practice is to first assign the return of realloc to a temporary pointer. once this checks out okay, copy it to the original pointer

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by jephthah View Post
    this is also a good example why you should not automatically assign the return of realloc() to your original pointer.

    good practice is to first assign the return of realloc to a temporary pointer. once this checks out okay, copy it to the original pointer
    AFAIK, if the call fails then the original pointer is useless anyway (or, at least the standard doesn't make any guarantees about it, so you'd be relying on undefined behaviour).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Sebastiani View Post
    AFAIK, if the call fails then the original pointer is useless anyway (or, at least the standard doesn't make any guarantees about it, so you'd be relying on undefined behaviour).
    I don't have my copy of the standard on me, but I believe it guarantees that if the call fails the original block remains.

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by tabstop View Post
    I don't have my copy of the standard on me, but I believe it guarantees that if the call fails the original block remains.
    Yep, you're right, it does. Sorry!
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. did i understood right this explantion of realloc..
    By transgalactic2 in forum C Programming
    Replies: 3
    Last Post: 10-24-2008, 07:26 AM
  2. using realloc
    By bobthebullet990 in forum C Programming
    Replies: 14
    Last Post: 12-06-2005, 05:00 PM
  3. segfault on realloc
    By ziel in forum C Programming
    Replies: 5
    Last Post: 03-16-2003, 04:40 PM
  4. Realloc inappropriate for aligned blocks - Alternatives?
    By zeckensack in forum C Programming
    Replies: 2
    Last Post: 03-20-2002, 02:10 PM
  5. realloc realloc realloc
    By Linette in forum C++ Programming
    Replies: 4
    Last Post: 01-19-2002, 09:18 PM