ddale@csm9a.UUCP (Doug Dale) (06/13/89)
I am writing a program that dynamically allocates space to store strings, and I have a question that a large number of manuals and C books won't answer for me. When using realloc(), if it is necessary to move the information to another location in memory because more memory is needed, is the old memory freed? I could simply check after calling realloc to see if the position has changed, and then free the old area if necessary, but I seem to remember hearing that freeing space that is already 'free' can be dangerous. So it might cause problems if realloc() does free memory, and then I try to free it again. You would think that I could find such information in any C manual, but... ddale@csm9a.colorado.EDU
r4@cbnews.ATT.COM (richard.r.grady..jr) (06/16/89)
In article <1606@csm9a.UUCP> ddale@csm9a.UUCP (Doug Dale) writes: > > When using realloc(), if it is necessary to move the >information to another location in memory because more memory is needed, is >the old memory freed? I looked through the code for realloc() on my machine (System V Rel 2). The algorithm is: if( old_pointer is not free ) free( old_pointer ) new_pointer = malloc( new_size ) etc. ... Yes, the old memory is freed. ------------------------------------------------------------------- Dick Grady r4@mvuxd.att.com ...!att!mvuxd!r4
gwyn@smoke.BRL.MIL (Doug Gwyn) (06/17/89)
In article <1606@csm9a.UUCP> ddale@csm9a.UUCP (Doug Dale) writes: >When using realloc(), if it is necessary to move the information to >another location in memory because more memory is needed, is the old >memory freed? Yes -- realloc() moves the one-and-only instance of the data to a possibly new location; the former pointer (from an earlier malloc() or realloc()) is no longer usable when realloc() returns successfully. (If realloc() returns unsuccessfully, then the data is supposed to still be where it was and the previous pointer remains valid.)