Understanding the point of pointers... Pun intended.

I understand that pointers allow a better use of memory. This is the scenario I keep using to wrap my mind around the use of pointers:

If you (possibly) use 100 arrays in a program, but you know that you only need to use one at a time, why not just use a pointer? You can use the new keyword to allocate new memory for one array and then use the delete keyword to free the memory, before using the same pointer again for the second array!

^^^Please let me know if that is an incorrect example.

Here is what I don't understand. Consider the following example (taken from my textbook):

1
2
3
4
5
6
7
8
struct things
{
int good;
int bad;
}

things grubnose = {3, 453};
things *pt = &grubnose;


How does this save memory?! You're already assigning static memory to structure when you create it, right? So even if you create a pointer to this structure, that static memory can not be saved, so what is the point in doing something like this???

Also, as a bonus question, when we talk about better using memory, what does that entail exactly?? Going back to the 100 array example near the top, would those 100 arrays be loaded into RAM when you first start the program? Or is "memory" referring to the overall size of the program?

Thanks for taking your time to read this!!! :)
Last edited on
Hiya,

I understand that pointers allow a better use of memory.

No.

How does this save memory?!

It doesn't.

Pointers don't "save you memory". They allow dynamic memory allocation (i.e. at runtime) and allow the ability to pass variables by reference, and some other stuff.

Have a read of these:
http://www.tutorialspoint.com/cprogramming/c_pointers.htm
http://www.learncpp.com/cpp-tutorial/79-the-stack-and-the-heap/


would those 100 arrays be loaded into RAM when you first start the program?

No, because you said you only need to use one at a time so you wouldn't need to allocate memory for 100 of them. you probably wouldn't even have to use a pointer if you knew the size of the array and it was constant over your 100 iterations.

if you used smart pointers (e.g. http://en.cppreference.com/w/cpp/memory/unique_ptr) you shouldn't need to worry about newand delete.
Last edited on
Awesome! Thanks Mutexe! Good Reads!

I would like to try and place the solution into my own words here:

The bad thing about using an array instead of a pointer is that if I use the elements in an array (by calling on them with cout or something similar) the static allocated memory for the array will get used, and then (BAD PART) that memory will be reserved by this array for the rest of the duration of the program.

If I, however, initialize a pointer to take on the value of this array, I can use the pointer instead of the array. Doing this allows the program to use the allocated memory from the heap instead of the static allocated memory from the array. The benefit of this is that I can simply overwrite the memory in the heap by using the delete option.

Does this sound right, or did I misunderstand something?

Last edited on
Topic archived. No new replies allowed.