Reputation: 6008
Let's say I have a
class A
{
int A1;
void Af();
};
Then I do:
A hA;
A* pA = new A();
now, hA
is an object in the stack; I can use
hA.A1 = 52 // for example
but pA
just points to class A
, how is that useful or handy? (any examples please?)
second issue:
when I did A* pA = new A()
; did I allocate anything in the heap? was there any malloc()
in the background? if not, then why not? and how come int *p_array = new int[5] will allocate memory in the heap of 5 ints and not A* pA = new A()?
Upvotes: 2
Views: 247
Reputation: 2071
but pA just points to class A, how is that useful or handy? (any examples please?)
It's useful and handy because now you have the address of an instance of class A that does not exsist on the stack, but on the heap. So it will survive if you fall out of scope(return from a function ect). You can pass the pointer around all over the place and you instance remains
when I did A* pA = new A(); did I allocate anything in the heap? was there any malloc() in the background?
Yes Yes and yes, this is the point of the new operator.
Upvotes: 0
Reputation: 88225
but
pA
just points toclass A
, how is that useful or handy? (any examples please?)
That's not the case. pA points to an A object, so *pA
is an object just like hA
is an object:
(*pA).A1 = 52 // for example
When you do A* pA = new A()
, yes a dynamic allocation occurs. Not necessarily a call to malloc()
, but something. (Ignoring uses of user defined operator new
which can do anything you like)
new A()
does two main things. It allocates space for the object somewhere. And if that succeeds then it constructs an object of the type you requested at that location. If the object is constructed successfully then it simply returns a pointer to the object.
Upvotes: 0
Reputation: 57749
Once use for pointers to objects is when the capacity or the data quantity is unknown at compile-time. A good example is receiving text from a User. Will the user enter 5 characters, 10, 1k?
If my program allocates 1k and the user never enters more than 32 characters, this is a waste of space.
Another use for dynamic memory allocation is passing information between objects:
Object 1 creates a book, but is not responsible for destroying it.
Object 2 proofreads the book, generates a list of corrections, but doesn't destroy the book.
Object 3 Adds tables to the book, still doesn't destroy it.
Object 4 Saves the book, then destroys it.
Please read the "C++ FAQ" for fast answers to common C++ Questions.
Upvotes: 0
Reputation: 133112
A hA = new A();
leads to a compiler error. The correct way of defining a variable "on the stack" is
A ha;
but pA just points to class A, how is that useful or handful (any examples please?)
In some place in the memory, you have an object of type A, and pa points to it. If you want to set the A1 member of that object to 52, you write
pA->A1 = 52;
The why is it useful part is not a real question.
when I did A* pA = new A(); did I allocate anything in the heap?
Yes, you did. new
does two things: it allocates memory and invokes the constructor.
was there any malloc() in the background?
That is unspecified, but in many implementations new
is implemented via malloc
and how come int[] A = A[42] will allocate memory in the heap of 42 ints and not A pA = new A() ?
This, sir, is also a compiler error. What you meant was
int* A = new A[42];
This is the operator new[]
which allocates arrays on the heap and calls constructors if necessary (in case of ints it isn't).
Upvotes: 4