Solved

freeing allocated memory

Posted on 2003-11-23
7
641 Views
Last Modified: 2010-04-01
Let's say I have a stucture that looks like:

struct STRUCT1
{
  int someInt;
  char* szText;
  LPVOID someMemory;
};

Now lets say I have another struct:

struct STRUCT2
{
  BYTE* memory;
  CMyClass* myClass;
};

Ok, now here is some code I have:

//...
STRUCT2* struct2 = new STRUCT2;
struct2->memory = new BYTE[number];
struct2->myClass = new CMyClass(param list);

STRUCT1* struct1 = new STRUCT1;
struct1->szText = new char[number];
struct1->someInt = 0;
struct1->someMemory = (void*)struct2;
m_structReference = struct1;
//...

Now later in the program, I call:
delete m_structReference;

Would this actually delete all of the memory I allocated?  If not, what would be the correct way to do this?
0
Comment
Question by:lwinkenb
7 Comments
 
LVL 32

Assisted Solution

by:jhance
jhance earned 100 total points
ID: 9806935
No, deleting the struct that contains pointers to other allocated memory will NOT free the other memory.  It will free ONLY the memory allocated with the new that returned the pointer you called delete on.

The correct approach is to delete all allocated memory pointed to by a pointer BEFORE freeing the pointer's memory.
0
 
LVL 8

Accepted Solution

by:
mnashadka earned 200 total points
ID: 9806937
This will not delete all of the associated memory.  It will only delete the memory that's been allocated for the class, not for the class members.  To do this correctly, you would have to delete each item individually:
delete [] struct2->memory;
delete struct2->myClass;

Probably a better way to do this would be to put that code in the STRUCT1 and STRUCT2 destructors (keep in mind that structs in C++ have constructors, destructors, and member functions):
struct STRUCT1
{
  int someInt;
  char* szText;

  // Initialize the data to NULL
  STRUCT1() : szText(NULL), someMemory(NULL) {}

  ~STRUCT1()
  {
    // Delete the szText memory if it was allocated
    if(szText != NULL) // This check is not necessary, but gives me warm fuzzies
    {
      delete [] szText;
    }
  }
};

Hope this helps.
0
 
LVL 5

Author Comment

by:lwinkenb
ID: 9807114
hmm... well I dont have a pointer to struct2, so I might have a problem.

What would happen if I called:
delete m_structReference->szText; // Im assuming this would work fine
delete m_structReference->someMemory; // what would happen here, since someMemory is of type void*?  Would it call a destructor  inside struct2?
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 8

Expert Comment

by:mnashadka
ID: 9807153
Ok, your example showed you having a pointer to struct2:
STRUCT2* struct2 = new STRUCT2;
struct2->memory = new BYTE[number];
struct2->myClass = new CMyClass(param list);

Either way, deleting the members by themselves would be fine.  The only thing you have to watch out for here is that you need to make sure that you delete them as single elements or arrays depending upon how you allocate them.  For instance, if you say:
m_structReference->szText = new char;
Then you would delete it like:
delete m_structReference->szText;

If you allocated it as an array:
m_structReference->szText = new char[100];
Then you need to delete it like an array:
delete [] m_structReference->szText;

In this case, depending upon the compiler you are using, if you don't delete it as array then it will only free the memory from the first element (in this case 1 char would be deleted and the other 99 would be left as a memory leak).  You obviously have to be careful about this.  Some compilers would handle this for you in this case, but if it's a non-POD type (i.e. CMyClass), then none of the compilers would handle this for you, and honestly they're not supposed to.  Be careful about that.
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 100 total points
ID: 9807205
> What would happen if I called:
> delete m_structReference->someMemory;

This wouldn't call any destructor, because void is no base class:

you would have to do this:

delete (STRUCT2*)m_structReference;  // Then the destructor STRUCT2 is called

or if you have

struct BASESTRUCT
{
   ...
};

struct STRUCT2 : public BASESTRUCT
{
  BYTE* memory;
  CMyClass* myClass;
  STRUCT2() : { memory(NULL), myClass(NULL) {}
  virtual ~STRUCT2() { delete [] memory; delete myClass; }
 
};

struct STRUCT1
{
  int someInt;
  char* szText;
  BASESTRUCT* someMemory;
};

then

delete m_structReference->someMemory;   // would call the destructor of STRUCT2

Regards, Alex









0
 
LVL 5

Author Comment

by:lwinkenb
ID: 9807504
>>Ok, your example showed you having a pointer to struct2:
Those references have gone out of scope.  That is why I saved the one reference to a member variable.

Is it possible to free memory without using the delete function?

Let's say I have a pointer to some memory, and I know the size of the memory I want to free.

struct STRUCT1
{
  LPVOID memory;
  int memorySize;
}

STRUCT1 struct1 = new STRUCT1;

BYTE bytes[] = new BYTE[32];
struct1->memory = bytes;
struct1->memorySize = 32;

How could I free up the memory without calling delete?
0
 
LVL 30

Assisted Solution

by:Axter
Axter earned 100 total points
ID: 9807603
Whenever you use new(), you have to use delete to properly free the memory.
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

827 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question