Why normally declare variables when you have pointers?

Dec 22, 2010 at 11:05pm
I'm just now learning about pointers, and read about the new operation:

1
2
//example of using new
int * pointer = new int;


If you can do this to create a variable-like pointer, why use and declare variables normally?
Last edited on Dec 22, 2010 at 11:42pm
Dec 22, 2010 at 11:12pm
I think that you have that backwards. Why use new to create a variable when you can just do it normally? You should be avoiding this kind of dynamic memory allocation as much as possible.
Dec 22, 2010 at 11:19pm
Why? (not arguing with you, just curious)

With pointers you have a variable and the address all in one.
Last edited on Dec 22, 2010 at 11:21pm
Dec 22, 2010 at 11:29pm
You always have the address. You can just use the & operator. With a pointer, you are forced to store the address in memory (usually the stack) and the actual variable somewhere else (probably on the heap), and when you want to access it, you have to perform a dereference operation. With a "normal" variable you only have to store the variable on the stack and can access it without that "extra" dereference. In addition, if you use pointers, you have to remember to delete the variable when you are finished unless you want to end up with a memory leak.

So basically, if you use pointers for every variable, you end up paying a speed and size cost (even if it is small) as well as having to worry about memory leaks and other issues associated with dynamic memory allocation.
Dec 22, 2010 at 11:32pm
So you have to use the delete operation on every unnamed block of memory you use with new if you don't want to get a memory leak?
Dec 22, 2010 at 11:33pm
Yes. Every new must be paired with a delete somewhere. Same with new[] and delete[].
Dec 23, 2010 at 9:20am
I think you are asking, why should you create (automatic) variable and assign its address to a pointer, when using dynamic allocation is much better (the new int).

it has to do with heap and stack allocation, the memory you just allocated to hold that variable (using new int) is dynamic, that means you created it, and therefore you must make sure that you delete it before the program ends, or goes out of scope, else it would be seen as a memory leak.

now using automatic variables are much better, since they are created automatically, thus they will be deallocated automatically.

although dynamic allocation for pointers might seems easy, they are often nasty (if you are writing a big program), you must make sure at all time you have access to them, and if you loose accesses to them, then you won't be able to get hold of them again. think of it like this, if a person has no name, how are you going to find them?





Last edited on Dec 23, 2010 at 9:26am
Dec 23, 2010 at 10:12am
Heap fragmentation might also be a headache?
Dec 23, 2010 at 8:43pm
although dynamic allocation for pointers might seems easy, they are often nasty (if you are writing a big program), you must make sure at all time you have access to them, and if you loose accesses to them, then you won't be able to get hold of them again. think of it like this, if a person has no name, how are you going to find them?


How would you lose access to them?
Dec 23, 2010 at 8:56pm
By changing to which block of memory a pointer points without storing the original address.

-Albatross
Last edited on Dec 23, 2010 at 8:58pm
Topic archived. No new replies allowed.