|
Re: C++ help: Understanding pointers?
Okay so I know that pointers simply point to another variable/memory location and can both change locations or change the value of the pointed-to's variable. I know pointers are used to borrow memory when you need it.
I DON'T know when/why you need extra memory and when you need to/dont need to use pointers.
I DON'T know how new/delete work all the way. here's an example of what I'm confused on for them:
int Number;
int *p_Number = new int;
int p_Number = Number;
So this creates an integer (Number), creates a pointer (p_Number) and gets enough memory for an integer ("new int"), then makes the pointer point to the original integer.
1. Is this how you would go about making room for a variable, making that variable and setting it to the pointer?
2. Is there a certain order or does it not matter?
3. Why do I need to get room (via "new") if the program will run fine without having to grab memory for it? And doesn't it defeat the purpose if I create an integer before getting room for one?
P.S. Why are there references? They're just half-pointers.. Why not use pointers when you'd use references and have one less thing to remember?
__________________
Team 3753 BulahBots
|