Advertisement

new and delete

Started by July 12, 2001 12:00 PM
6 comments, last by Zeke 23 years, 7 months ago
I have a struct typedef struct { vars etc char* charPtr; }STRUCT; I then declare an array of these structs using new: STRUCT* StructArr=new STRUCT[Num]; i have a number of functions that return a char* created with new and do this: StructArr.charPtr=GetData(blah,blah); Now when i want to clean up can i just do delete[]StructArr? Will that delete all the charPtr''s created? Or do I have to manually delete[] the charPtr''s like: for (x;x<Num;x++) delete[]StructArr.charPtr I''m asking this because I have a huge memory leak from the function that creates the char* (GetData()) but I do delete[]StructArr as soon as I am able to (once i no longer need the data). Im not too hot on using new in functions and then deleting them outside of that function so this has me a bit puzzled as I cannot delete the StructArr any earlier than I am doing at the moment. If anyone has any tips to give me I would be grateful. Thanks.
Just my thoughts take them as you will. "People spend too much time thinking about the past, whatever else it is, its gone"-Mel Gibson, Man Without A Face
No, the delete Structs[] won''t delete the allocated char buffers, you will have the iterate through the struct array, deleting each allocated buffer.

Daniel.

PD: if you are allocating the char buffers with new [] don''t forget to free with delete []!
Advertisement
No, the delete Structs[] won''t delete the allocated char buffers, you will have the iterate through the struct array, deleting each allocated buffer.

Daniel.

PD: if you are allocating the char buffers with new [] don''t forget to free with delete []!
structs can have constructors and destructors just like classes, use them.

i.e. change your struct to:

struct STRUCT
{
vars etc
char* charPtr;

STRUCT() {charPtr = NULL;}
~STRUCT() {delete [] charPtr;}
};

now when you delete your array the charPtrs will get cleaned up also.

-Mike

-Mike
great!! thanks guys.

Anon Mike>I didnt know that thanks for the info
Just my thoughts take them as you will. "People spend too much time thinking about the past, whatever else it is, its gone"-Mel Gibson, Man Without A Face
To Anon Mike:
Using constructors and destructors with structs is sick!
To Zeke:
new and delete are keywords from c++, this means you are programming c++. So why on earth do you want to use structs? I recommend you use classes:
  class STRUCT{public:   vars etc   char* charPtr;};  

And why do you use char * charwhatever ?
In c++ you can use the very comfortable
  <string>   

class.

Edited by - Jonus on July 12, 2001 3:59:32 PM
Advertisement
Jonus>Thanks for the info. The reason I was using structs instead of classes was because they didnt need to have any functions and I figured in that case I might aswell use the simpler struct than class. But now that I need them to have a constructor and a destructor I might aswell change them to classes. Im using char* instead of string is simply because I dont know anything about string. Thanks again for the info
Just my thoughts take them as you will. "People spend too much time thinking about the past, whatever else it is, its gone"-Mel Gibson, Man Without A Face
quote:
Original post by Jonus
To Anon Mike:
Using constructors and destructors with structs is sick!
To Zeke:
new and delete are keywords from c++, this means you are programming c++. So why on earth do you want to use structs?


Because structs and classes are the same thing with exception of the default range of the members. In a class by default are all members private in a struct all class members are public.

so in C++
  struct SomeStruct{  char *pStr;}  

is the same as
  class SomeClass{public:  char *pStr;}  



/JanneVee
"Some People even believe that COBOL is a real programming language." Scott Meyers - Effective C++
/JanneVee"Some People even believe that COBOL is a real programming language." Scott Meyers - Effective C++

This topic is closed to new replies.

Advertisement