Thread: Test pointer if successfully freed.

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    3

    Question Test pointer if successfully freed.

    Once I've freed allocated memory, is there any way of testing the block? free() has no return and I need to be able to ensure the memory has in fact been freed.

    Also, if I do this:

    NODE *ptr1, *ptr2;

    ptr1 = malloc(sizeof(NODE));
    ptr2 = ptr1;

    free(ptr2);


    Does this free the memory allocated to ptr1?

  2. #2
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633

    Re: Test pointer if successfully freed.

    Originally posted by UniMord
    Code:
    NODE *ptr1, *ptr2;
    
    ptr1 = malloc(sizeof(NODE));
    ptr2 = ptr1;
    
    free(ptr2);
    Does this free the memory allocated to ptr1?
    Yes, both ptr1 and ptr2 refer to the same address on the heap, so freeing either one will work.

    If your operating system supports it, you could use memorymap() to print diagnostic memory info. If you need something a little better than dumps to stdout, take a look at mallinfo().
    Jason Deckard

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    3
    Thanks Deckard.

    I find that if I used realloc on ptr1, ptr2 gets left behind pointing to the original block. Normally, realloc frees before abandoning, so do I correctly assume that ptr2 is now pointing to released memory? Is it possible to enforce that ptr2 point to ptr1 no matter where ptr1 gets realloced to?

    NODE *ptr1, *ptr2;

    ptr1 = malloc(sizeof(NODE));
    ptr2 = ptr1;
    ptr1 = realloc(ptr1,sizeof(NODE) * 10); /* ptr2 stays behind */
    free(ptr2);

  4. #4
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    ptr2 might be pointing to unallocated memory. It has to do with the way realloc() conducts it's business. Assuming the reallocated area is larger than the currently allocated area, realloc() will:[list=1][*]Attempt to expand current memory area[*]If not enough room to simply expand, allocate room somewhere else, move the contents, and free the original location[*]If room cannot be allocated somewhere else, return NULL and leave the original memory area alone[/list=1] Note that in your code example there is no difference between the amount of memory allocated with malloc and the amount of memory you intend to resize it to (nothing changes, realloc() won't fail).

    Consider this:
    Code:
    void *ptr1, *ptr2;
    
    ptr1 = malloc( 1024 );
    ptr2 = ptr1;
    
    ptr1 = realloc( ptr1, 4096 );
    If realloc() had to relocate the memory block to accomodate the request, ptr2 now points to unallocated memory. Attempting to free ptr2 in this scenario will raise SIGBUS (which is a bad thing). Remember that pointers are nothing more than variables that hold memory addresses. You can simply check the value of ptr1 to see how realloc() handled the request (nested for clarity):
    Code:
    if ( ptr1 )           /* realloc did not return NULL */
    {
      if ( ptr1 != ptr2 ) /* realloc moved the memory block */
      {
        ptr2 = ptr1;      /* make sure ptr2 has the new address */
      }
    }
    else                  /* realloc failed, and assigned NULL to ptr1 */
    {
      ptr1 = ptr2;        /* reset ptr1 to the allocated memory area */
    }
    At this point, ptr1 and ptr2 reference the same memory location, regardless of how the realloc() turned out.
    Jason Deckard

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    3
    Note that in your code example there is no difference between the amount of memory allocated with malloc and the amount of memory you intend to resize it to (nothing changes, realloc() won't fail).
    What do you mean by that? In my code, realloc() ordered 10 NODEs instead of 1.

    Once I have you here: When I wish to return a string from a function, does it have to be a static variable? In general, I find that everything works just fine without it being static, but that of course is no proof that it's a good idea, this is C, after all, it might come back and bite me sometime.

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. scope of a pointer?
    By Syneris in forum C++ Programming
    Replies: 6
    Last Post: 12-29-2005, 09:40 PM
  4. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:19 PM
  5. memory management...
    By master5001 in forum Game Programming
    Replies: 24
    Last Post: 01-07-2002, 05:50 PM