realloc() doesnt copy the block correctly!

This is a discussion on realloc() doesnt copy the block correctly! within the C Programming forums, part of the General Programming Boards category; I remembered that when you call realloc() and the return address is different from the original one, then realloc() should ...

  1. #1
    Run! Forrest Gump! TalosChen's Avatar
    Join Date
    May 2006
    Location
    Shanghai
    Posts
    27

    realloc() doesnt copy the block correctly!

    I remembered that when you call realloc() and the return address is different from the original one, then realloc() should copy the content of the original block to the newly allocated block. But in my recent program I found it didnt do it correctly. So how could this happen?

    My platform is Mingw32.

  2. #2
    Run! Forrest Gump! TalosChen's Avatar
    Join Date
    May 2006
    Location
    Shanghai
    Posts
    27
    for example,

    before realloc()
    Code:
    msg_in_all, len: 111, addr: 009C1960
    01000029 0301446D 6B1D278D 11AE381E 01000029 0301446D 6B1D278D 11AE381E
    FB49D271 EA2D0F36 F97B8E85 EFA0AA64 B5117225 DB740000 02001301 00000000
    01000029 0301446D 6B1D278D 11AE381E FB49D271 EA2D0F36 F97B8E85 EFA0AA64
    B5117225 DB74000B 00000010 000000
    after realloc()
    Code:
    msg_in_all, len: 115, addr: 009C19E0
    7E740000 13000000 00000000 00000000 00000000 FFFFFFFF 00000000 00000000
    00000000 01000000 00000000 00000000 00000000 00000000 00000000 00000000
    00000000 00000000 00000000 00000000 00000000 00000000 00000000 D7A10B10
    00000000 10F7C277 00000000 0000000F 000000

  3. #3
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Please show a minimal test case that we can see your code and run it.

  4. #4
    Run! Forrest Gump! TalosChen's Avatar
    Join Date
    May 2006
    Location
    Shanghai
    Posts
    27
    Sorry, my program is large. So I cannot give you such code to run for test. But I did write a small test program to test realloc(), and nothing abnormal happened.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    Which basically means you've pre-corrupted the memory pool in some way, and here is where you get to notice your prior mistake.

    Finding the root original cause of the problem is of course another matter.

    If you're using linux, I would suggest electic fence.
    gcc -g prog.c -lefence

    Followed by running the code in the debugger.

    You should end up with a stack trace to the point of first corruption of the memory pool.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Run! Forrest Gump! TalosChen's Avatar
    Join Date
    May 2006
    Location
    Shanghai
    Posts
    27
    Well, how can memory pool be corrupted?

    free, malloc, realloc or something like this?

  7. #7
    Run! Forrest Gump! TalosChen's Avatar
    Join Date
    May 2006
    Location
    Shanghai
    Posts
    27
    I'm really sorry for this thread, because it's not the realloc()'s fault but mine. I actually use the shorter size for realloc() but not the longer which I respect, so the problem has been solved. Any way, thanks for your warmly help.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,259
    Quote Originally Posted by TalosChen
    Well, how can memory pool be corrupted?

    free, malloc, realloc or something like this?
    Corruption of the "memory pool" is more usually caused by pointer molestation (i.e. doing an invalid operation involving a pointer) or falling of the end of an array. Examples include dereferencing a NULL, accessing memory after free()ing it, accessing array[5] when the array only has 5 elements, dereferencing a dangling reference, etc.

    None of these errors specifically corrupt the "memory pool": the issue with pointer errors is that your program usually modifies some area of memory that it shouldn't and, in some cases, the affected memory happens to be used by the free/malloc/realloc/calloc functions. Pointer molestation could just as easily tromp some other area of memory and cause some other strange symptom.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer conversion problems with a copy constructor
    By stanlvw in forum C++ Programming
    Replies: 8
    Last Post: 01-13-2008, 11:06 PM
  2. Template overload of operator ++/--
    By Elysia in forum C++ Programming
    Replies: 26
    Last Post: 10-23-2007, 08:45 AM
  3. Faster way to copy memory?
    By @nthony in forum C Programming
    Replies: 4
    Last Post: 09-16-2007, 03:47 PM
  4. vector won't copy correctly...
    By talz13 in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2003, 07:35 AM
  5. open directory and copy files
    By 5n4k3 in forum C++ Programming
    Replies: 3
    Last Post: 08-06-2003, 09:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21