How can I delete a Object which is not created by new operator
Hi gurus.
I want delete a object which was not created dynamically (new or someother way) how can i do this.
ex..
class MyClass
{
.....
....
};
int main()
{
MyClass objMyObj;
.......
.......
/// after some stuffs I want delete this object from Memory How can I remove it from Memory...
......
. .......
.....
return 1;
}
yeah!!its correct way as shivabharat suggests to use delete only when u use new.
And to delete the object which is created without new, here the destructor will do ur job in deleting the memory alocated for the object created without using new.
yeah!!its correct way as shivabharat suggests to use delete only when u use new.
And to delete the object which is created without new, here the destructor will do ur job in deleting the memory alocated for the object created without using new.
When you arent using a new or malloc the memory will be allocated in stacks. Stacks get cleared up automatically and you dont have to clear them explicitly.
I am not sure if this code make any sense
Code:
#include <iostream.h>
class base
{
private:
int i;
public:
base()
{
i=10;
}
~base()
{
cout<<"Object destroyed"<<endl;
}
void show()
{
cout<<"I value is "<<i<<endl;
}
virtual void base::destroy ()
{
base::~base();
}
};
int main()
{
base b;
b.show();
b.destroy();
base c;
c.show();
return 0;
}
#include <iostream.h>
class base
{
private:
int* pi;
public:
base() : pi( 0 )
{
pi = new int;
*pi=10;
}
~base()
{
delete pi;
pi = 0;
cout<<"Object destroyed"<<endl;
}
void show()
{
cout<<"*pi is "<<*pi<<endl;
}
virtual void base::destroy ()
{
base::~base();
}
};
int main()
{
base b;
b.show();
b.destroy();
b.show();
return 0;
}
but after calling ur destry (method) the object's memory still not freeing
The object is not destroyed. It's dtor was explicitly invoked in destroy(), but the object was still "valid" and in scope. If you check the output from the original source, you will see that "Object destroyed" is printed 3 times, even though only 2 objects are created. The implementation of the dtor in the original source was trivial; in the source I posted, it actually and concretely affects the object.
Note that explicit calls to the dtor are seldom necessary. Most frequently, one calls a dtor explicitly on an object allocated using a user-defined new operator that takes a placement argument. See "Explicit Destructor Calls" in the "C++ Language Reference" of the MSDN library for more info.
Last edited by vicodin451; October 20th, 2003 at 08:16 AM.
Thought for the day/week/month/year: Windows System Error 4006:
Replication with a nonconfigured partner is not allowed.
If you used the code I posted, the class is 8 bytes because of the int* class member (4 bytes), and the vtbl ptr (also 4 bytes, as a result of destroy being virtual). If you change destroy to be non-virtual, the size of the class will be 4 bytes (for the int* member).
Whats happening
Even though "destroy" is called, it's not really "destroying" the object. You could have called the function "masticate" and the same thing would happen. "Destroy" explicitly calls the destructor which is a no-no in this case, as the destructor is also automatically invoked with the object goes out of scope - at the end of main. Since it has been explicitly invoked, the second (automatic) invocation will take place when the object is in an indeterminate state.
Thought for the day/week/month/year: Windows System Error 4006:
Replication with a nonconfigured partner is not allowed.
ya I want manage life time of Object myself with out creating dynamically (using New oprator) like Java? I think there ways to do this? I do't know exactly.
regs
Balamurali C
Bookmarks