Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 647
  • Last Modified:

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?
0
lwinkenb
Asked:
lwinkenb
4 Solutions
 
jhanceCommented:
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
 
mnashadkaCommented:
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
 
lwinkenbAuthor Commented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
mnashadkaCommented:
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
 
itsmeandnobodyelseCommented:
> 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
 
lwinkenbAuthor Commented:
>>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
 
AxterCommented:
Whenever you use new(), you have to use delete to properly free the memory.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now