Pointers (new and delete)
Hi, i've just been working through the pointers chapter in my C++ book and come across a confusing part.
What's the difference between using:
int iAge = 20;
int * pAge = &iAge;
and
int * pAge = new int;
*pAge = 20;
i.e. Why use new and delete when you can just make a pointer like the first method?
Re: Pointers (new and delete)
Quote:
Originally posted by Russ
Hi, i've just been working through the pointers chapter in my C++ book and come across a confusing part.
What's the difference between using:
int iAge = 20;
int * pAge = &iAge;
and
int * pAge = new int;
*pAge = 20;
i.e. Why use new and delete when you can just make a pointer like the first method?
With the first method, your creating a variable on the stack.....this only has a fixed amount of memory.....so say at runtime you needed an amount of ints, but you didnt know how many, youd be stumped as you would be stuck to 1 int.....with the second method you can have as many ints as you choose
int * pAge = new int(20);