On 16 May, 15:11, Prasanth <prasanth...@gmail.com> wrote:
I am novice to c++,
I have the following code :
class A{
int c;
};
What is the difference between the declarations
A a;
A *a = new A();
While creating any object compiler checks for whether there is enough
memory or not. cane someone please elaborate in detail
what happens in the perspective of the memory allocations in both the
cases.... ??? And in what situations we use this.. ??
Here's an example to illustrate the difference:
A *x; // global variable
void fun() {
A a;
A *b = new A();
x = b;
}
In this case, a is created when the function starts, and is destroyed
again when the function finishes. On the other hand, the one pointed
at by b is created when the function gets to the "new", but continues
to live until it is explicitly destroyed by using "delete". I've added
a global variable just so that there is a way for other parts of the
program can find the created object. Better methods are available.
Hope that helps.
Paul.
Thanks for the answer...