Thread: clearing memory allocated to a struct

  1. #1
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065

    clearing memory allocated to a struct

    I'm having trouble figuring out how to free memory allocated to a
    struct. Here's my code:

    Code:
    //global defines
    struct Test
    {
       long l;
       int i;
       char c[20];
       bool b;
    };
    
    Test tTest;
    
    //initialization
    tTest.l=1233455;
    tTest.i=12;
    strcpy(tTest.c,"TestChar");
    tTest.b=true;
    
    //test/deallocation (this is what I can't get right)
    if(&tTest)
    {
        MessageBox(NULL,"Allocated","Debug",NULL);
        tTest.l=NULL;
        tTest.i=NULL;
        strcpy(tTest.c,"");
        tTest.b=NULL;
    }
    else
    {
       MessageBox(NULL,"Memory cleared","Debug",NULL);
    }
    I've also tried:
    Code:
    Test* tTest;
    
    //initialization
    tTest=new Test[1];
    tTest->l=1233455;
    tTest->i=12;
    strcpy(tTest->c,"TestChar");
    tTest->b=true;
    
    //test/deallocation (this is what I can't get right)
    if(tTest)
    {
        MessageBox(NULL,"Allocated","Debug",NULL);
        delete [] tTest;
    }
    else
    {
       MessageBox(NULL,"Memory cleared","Debug",NULL);
    }
    With the second bit of code I get the following error:

    Debug Assertion Failed!

    Anybody know what I'm doing wrong?

  2. #2
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    AAAGGGHHH!

    Ok, now I've got it to work (that is return the message I want)
    but I'm not sure if it's doing what I really want in the background.

    what I did was using the second code snippet I replaced the
    line:
    Code:
    delete [] tTest;
    with this:
    Code:
    tTest=NULL;
    By setting the pointer to the structure to NULL am I really freeing
    the memory? (I'm sorry if this is a stupid question, but I've never
    really learned that much about memory management and I'm
    trying to learn it now; playing catch up I guess)

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    52
    I shook the 8 ball and it said you had to release it back to the heap. NULL still occupies the address. Que no? Of course, the 8 ball (or Horton) could be wrong.

  4. #4
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    The delete (or delete[] for arrays) will free the memory up to be used again. Setting the pointer to NULL is a safety measure because technically it still "points" to the memory location (dangling pointer). You could in theory still use the pointer but the memory may have been reallocated. You set the pointer to NULL to prevent accidental use after the memory has been released. If you set the pointer to NULL before releasing it you end up with a memory leak which is where you've allocated memory but have no way to access it anymore.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  3. Malloc & Calloc difference in terms of memory allocated
    By swapnaoe in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 12:57 AM
  4. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM