I'm not skilled in formal language expresion so I hope it will help you.
When you initialize
"Test* test = new Test();"
you have a test Pointer, that is size of 4 bytes, that points to
location where real test object is located.
If you initialize
"Test test;"
You get "realtime" an object, that is directly accessed, not thro pointer.
The difference is, that if you initialize it with new,
you have to delete it somewhere, and you have to set pointer back to NULL.
First example to correctly delete, clean it from memory is.
int main()
{
{
Test* test = new Test();
// Do whatever
delete test; // Delete class object
test = NULL;
// If you don't delete this object here ...
// you will lose object, pointer,
// -> MEMORY LEAK
}
return 666;
}
Without new
int main()
{
{
Test test;
// This object is only valid in this block
// When you leave this block, descructor is automaticly
// called
}
// here object is deleted
return 666;
}
Tony Johansson wrote:
Hello Experts!!
I have an easy question when you allocate memory by using new you allocate
memory dynamically but what is it called
when you allocate memory without using new.
Here is an example of allocating memory dynamically. You instansiate object
obj of class Test. The class is called Test
Test obj = new Test();
Here is an example of allocating memory without using new. You instansiate
object obj of class Test. The class is called Test.
You know you can allocate memory in different scope.(local(block),
file(global), function or class)
Test obj;
Many thanks!!
//Tony
--
Gregor Razdrtic [Mufe]
mu**@poizen.org