What happens when you have the following code:
void makeItHappen()
{
char* text = "Hello, world";
}
Does text
go out of scope and get deleted automatically or does it stay in the memory?
And what about the following example:
class SomeClass
{
public:
SomeClass();
~SomeClass();
};
SomeClass::SomeClass() { }
SomeClass::~SomeClass()
{
std::cout << "Destroyed?" << std::endl;
}
int main()
{
SomeClass* someClass = new SomeClass();
return 0;
} // What happend to someClass?
Does the same thing occur here?
Thanks!
Yes! It is local to the function
makeItHappen()
and when the function returns it goes out of scope. However the pointed to string literal"Hello, world";
has static storage duration and is stored in read only section of the memory.Your second code sample leaks memory.
SomeClass* someClass = new SomeClass();
someClass
is local tomain()
so when main returns it being an automatic variable gets destroyed. However the pointed to object remains in memory and there's no way to free it after the function returns. You need to explicitly writedelete someClass
to properly deallocate the memory.Here an automatic variable (a pointer) is created on the stack and set to point to a value in constant memory, which means:
""
exists through the whole program execution.When the pointer goes out of scope, the memory pointer itself (4 bytes) is freed, and the string is still in the same place - constant memory.
For the latter:
Then
someClass
pointer will also be freed when it goes out of scope (since the pointer itself is on the stack too, just in the first example)... but not the object!The keyword
new
basically means that you allocate some memory for the object on free store - and you're responsible for callingdelete
sometime in order to release that memory.The variable text does go out of scope (however the string literal is not deleted).
For objects that you allocate with new (like your SomeClass), you need to explicitly delete them. If you want objects allocated like this to be automatically deleted, take a look at boost smart pointers (std::unique_ptr if your compiler is c++0x aware).
This will automatically delete the allocated object when the shared pointer goes out of scope.
Your code would then look like this:
Note: In this particular example, you could also use std::auto_ptr (but this will be deprecated in c++0x).
Note 2: As was pointed out in the comments by Kos, it is in this case more appropriate to use boost::scoped_ptr or std::unique_ptr (c++0x). My answer first used boost::shared_ptr, which is more appropriate if you need to share ownership of a pointer between several classes for instance.
In the first example the string literal is stored in data segment of your executable.
In the second case you do not have to call
delete
(in your example program just terminates) since on program termination the heap is freed anyway for the process.Note though that there are OS (as I have read) that you have to explicitly release heap even if the program terminates since it will not be cleaned up at termination for you.
Of course programmer is responsible for memory management in C++ and objects you create on heap should be
delete
ed once unneeded.