Link to home
Start Free TrialLog in
Avatar of lwinkenb
lwinkenb

asked on

freeing allocated memory

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?
SOLUTION
Avatar of jhance
jhance

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lwinkenb
lwinkenb

ASKER

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?
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>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?
SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial