Why normally declare variables when you have pointers?

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
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.
Why? (not arguing with you, just curious)

With pointers you have a variable and the address all in one.
Last edited on
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.
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?
Yes. Every new must be paired with a delete somewhere. Same with new[] and delete[].
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
Heap fragmentation might also be a headache?
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?
By changing to which block of memory a pointer points without storing the original address.

-Albatross
Last edited on
Topic archived. No new replies allowed.