Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

freeing allocated memory

Posted on 2003-11-23
7
Medium Priority
?
646 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 32

Assisted Solution

by:jhance
jhance earned 400 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 800 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
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.

 
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 400 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 400 total points
ID: 9807603
Whenever you use new(), you have to use delete to properly free the memory.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

722 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