Solved

freeing allocated memory

Posted on 2003-11-23
7
637 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now