reallocating memory 
Author Message
 reallocating memory

hi all,

I was recently writing a function which automatically enlarges an array of
int's, reallocating the memory accordingly.

I was using realloc at the start, something like:

if ((realloc(mystruct->arrayptr, (mystruct->size+1) * sizeof(int))) != NULL)
{
  ...  //memory reallocated correctly

Quote:
}

else
  //error: didn't reallocate memory

It always crashed with more than 3 items. So i decided to change the realloc
to a combination of free() and malloc(), which works fine now. I was just
wondering whether or not there are any known issues regarding the realloc()
function? Thanks for your help:)

Sean



Sun, 14 Nov 2004 13:28:03 GMT  
 reallocating memory


Quote:
> hi all,

> I was recently writing a function which automatically enlarges an array of
> int's, reallocating the memory accordingly.

> I was using realloc at the start, something like:

> if ((realloc(mystruct->arrayptr, (mystruct->size+1) * sizeof(int))) !=
NULL)
> {
>   ...  //memory reallocated correctly
> }
> else
>   //error: didn't reallocate memory

> It always crashed with more than 3 items. So i decided to change the
realloc
> to a combination of free() and malloc(), which works fine now. I was just
> wondering whether or not there are any known issues regarding the
realloc()
> function? Thanks for your help:)

Whether there are known issues regarding realloc depends upon the
compiler/implementation you're using. You should ask in a group dedicated to
the compiler/implementation you are using, or ask your vendor.

Another possibilty is that you're not using realloc correctly. The following
code demonstrates a correct use of realloc. Hope this makes you able to use
realloc instead of the free, malloc combi.

{
    char *ptr;

    /* first time allocation: 56 bytes */
    ptr = malloc(56);
    if (ptr == NULL)
    {
        /*malloc failed: handle appropriately */
    }
    else
    {
        /* malloc succeeded */
    }

    /* SOME CODE HERE */

    /* try to reallocate memory: new size: 102 */
    {
        char *new_ptr;
        new_ptr = realloc(ptr, 102);
        if (new_ptr == NULL)
        {
            /* realloc failed: handle appropriately */
        }
        else
        {
            /* realloc succeeded */
            ptr = new_ptr;
        }
    }

    /* SOME CODE HERE */

    /* free allocated memory */
    free(ptr);
    ptr = NULL;

    /* SOME CODE HERE */

- Show quoted text -

Quote:
}



Sun, 14 Nov 2004 13:42:52 GMT  
 reallocating memory

Quote:

> I was recently writing a function which automatically enlarges an array of
> int's, reallocating the memory accordingly.
> I was using realloc at the start, something like:
> if ((realloc(mystruct->arrayptr, (mystruct->size+1) * sizeof(int))) != NULL)
> {
>   ...  //memory reallocated correctly
> }
> else
>   //error: didn't reallocate memory
> It always crashed with more than 3 items. So i decided to change the realloc
> to a combination of free() and malloc(), which works fine now. I was just
> wondering whether or not there are any known issues regarding the realloc()
> function? Thanks for your help:)

The problem with your code is that you seem to assume that the first
argument of realloc() gets changed during the call (or that the memory
address never changes on realloc()), which isn't the case. You never
store the address of the new memory you got but just use the address
to compare it to NULL. Unfortunately, this might work for a few times
if there is still free memory directly following the block you already
allocated, in which case the address of the reallocated memory block
may not change. But when you later get a new block of memory (because
the old memory block could not be extended any further) you get your
crash because you still work with the pointer to the old memory block
(which does not belong to you anymore) instead of the pointer to the
new memory. That's not an "issue" with realloc() but simply a bug in
your code.
                                             Regards, Jens
--
      _  _____  _____

  _  | |  | |    | |          AG Moebius, Institut fuer Molekuelphysik
 | |_| |  | |    | |          Fachbereich Physik, Freie Universitaet Berlin
  \___/ens|_|homs|_|oerring   Tel: ++49 (0)30 838 - 53394 / FAX: - 56046


Sun, 14 Nov 2004 20:41:04 GMT  
 reallocating memory

Quote:

> I was recently writing a function which automatically enlarges
> an array of int's, reallocating the memory accordingly.

> I was using realloc at the start, something like:

> if ((realloc(mystruct->arrayptr, (mystruct->size+1) * sizeof(int))) != NULL)
> {
>   ...  //memory reallocated correctly
> }
> else
>   //error: didn't reallocate memory

> It always crashed with more than 3 items. So i decided to change

Of course.  You threw away the realloced pointer.

  void *tp;
  ...
  if (NULL == (tp = realloc(ptr, newsize))) {
     /* abject failure */
  }
  else {
     ptr = tp;
     /* carry on, it worked */
     ...
  }

--

   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!



Sun, 14 Nov 2004 22:15:10 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Freeing reallocated memory

2. Reallocating memory without disturbing the original address

3. Reallocate memory

4. dynamically reallocate memory for object derived from class without destroying the data

5. How to Reallocate memory ?

6. reallocating space for pointer to structs?

7. getting a reallocated string back

8. Allocating anf Reallocating in COM

9. Reallocating space for an array from VB

10. Reallocating a heap created by HeapCreate

11. REALLOCATE vs DEALLOCATE

12. Question: reallocating a 2 dimentional array

 

 
Powered by phpBB® Forum Software