Click here to Skip to main content
15,900,110 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Size of an executable Pin
Vinaya12-Apr-05 21:27
Vinaya12-Apr-05 21:27 
GeneralProblem with CListCtrl Pin
Neeranjan12-Apr-05 1:47
Neeranjan12-Apr-05 1:47 
GeneralHttpURLConnection Pin
cberam12-Apr-05 1:17
cberam12-Apr-05 1:17 
GeneralRe: HttpURLConnection Pin
ThatsAlok12-Apr-05 2:46
ThatsAlok12-Apr-05 2:46 
GeneralLogging output of ActiveX Pin
Member 147966912-Apr-05 0:25
Member 147966912-Apr-05 0:25 
GeneralOperator new and delete mechanism! Pin
HoDuc12-Apr-05 0:15
HoDuc12-Apr-05 0:15 
GeneralRe: Operator new and delete mechanism! Pin
Cedric Moonen12-Apr-05 0:25
Cedric Moonen12-Apr-05 0:25 
GeneralRe: Operator new and delete mechanism! Pin
Cedric Moonen12-Apr-05 1:17
Cedric Moonen12-Apr-05 1:17 
Ok, I will go a little bit deeper in details. It's true that it is a part that is not easy to understand at the begining.

The destructor of a class is there to free the memory that has been dynamically allocated (so with a new) for one or several member variables of your class (could do other things also but here we will just speak about the memory management). This is typically the place where you need to put the delete of str in your example. Even if the variable has not been allocated in the constructor for example !!

A good way of working is that if you don't allocate memory in the constructor for a pointer of your class, make it NULL:

T::T()<br />
{<br />
   str = NULL;<br />
}


Thus, when you destructor will be called, you can check if this memory has been allocated or not and free it in the first case:

T::~T()<br />
{<br />
if(str)<br />
{<br />
delete[] str;<br />
str = NULL;<br />
}<br />
} 


So, now when is the destructor of a class called ? Simple: whenever the object is being destroyed. By destroyed I mean not only when calling delete but also when your object (not allocated by new) goes out of scope. Example:

int foo()<br />
{<br />
T Object;<br />
....<br />
....<br />
return 0;<br />
}     // Your object is destroyed here !!


In this case, your object has not been allocated dynamically but its destructor is called when the object is destroyed (leaving the function).

I hope this is a little bit clearer ? If you have further questions, do ask Wink | ;)
GeneralRe: Operator new and delete mechanism! Pin
toxcct12-Apr-05 0:32
toxcct12-Apr-05 0:32 
GeneralRe: Operator new and delete mechanism! Pin
sunit512-Apr-05 2:54
sunit512-Apr-05 2:54 
GeneralRe: Operator new and delete mechanism! Pin
Cedric Moonen12-Apr-05 3:06
Cedric Moonen12-Apr-05 3:06 
GeneralRe: Operator new and delete mechanism! Pin
S. Senthil Kumar12-Apr-05 5:55
S. Senthil Kumar12-Apr-05 5:55 
GeneralRe: Operator new and delete mechanism! Pin
Rick York12-Apr-05 9:24
mveRick York12-Apr-05 9:24 
GeneralRe: Operator new and delete mechanism! Pin
Ryan Binns12-Apr-05 18:37
Ryan Binns12-Apr-05 18:37 
GeneralRe: Operator new and delete mechanism! Pin
S. Senthil Kumar12-Apr-05 19:39
S. Senthil Kumar12-Apr-05 19:39 
GeneralProblem with OnNotify Pin
thanh-hai.bui12-Apr-05 0:13
thanh-hai.bui12-Apr-05 0:13 
GeneralRe: Problem with OnNotify Pin
Joel Holdsworth12-Apr-05 2:07
Joel Holdsworth12-Apr-05 2:07 
GeneralRe: Problem with OnNotify Pin
thanh-hai.bui12-Apr-05 3:15
thanh-hai.bui12-Apr-05 3:15 
GeneralRe: Problem with OnNotify Pin
Joel Holdsworth12-Apr-05 4:10
Joel Holdsworth12-Apr-05 4:10 
GeneralRe: Problem with OnNotify Pin
Ryan Binns12-Apr-05 18:43
Ryan Binns12-Apr-05 18:43 
GeneralRe: Problem with OnNotify Pin
thanh-hai.bui12-Apr-05 21:46
thanh-hai.bui12-Apr-05 21:46 
QuestionHow to pass string in DLL? Pin
Member 184868012-Apr-05 0:04
Member 184868012-Apr-05 0:04 
QuestionHow to pass string in DLL? Pin
Member 184868012-Apr-05 0:01
Member 184868012-Apr-05 0:01 
AnswerRe: How to pass string in DLL? [edited] Pin
toxcct12-Apr-05 0:08
toxcct12-Apr-05 0:08 
GeneralRe: How to pass string in DLL? Pin
Cedric Moonen12-Apr-05 1:31
Cedric Moonen12-Apr-05 1:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.