I wish I had some paper and a pencil. I find pointers easiest to explain in pictures.
Your code...
double * buffer = new double[8];
... does a couple things at once. And, yes it's a total of 64 bytes in size if you have 8 byte doubles.
First double * buffer makes a simple variable named "buffer" that holds an integer number. That integer is actually a spot in the computers memory, an address, and the thing at that address is a decimal number (the double). I think you already understand that, but I'm just going full tilt for ease and completeness (no offense intended
).
Imagine a set of mailboxes. Each box has a number and if I told you I'm at number 15 you'd know where to go to give me a letter.
double * buffer
is like
letter * boxNum
The next part, = new double[8] makes eight decimal numbers out in the heap, all in an unbroken line of memory. Their addresses (whatever they are) are contiguous, just like real-world mailbox usually are.
So, to answer your questions more directly:
so i would use a pointer to probably store a large object/variable?
Yes. Pointers are most often used to point at very large/complex objects that you don't want to be copying around, OR, for pointing at individual objects within a list, like the 8 element array in your example.
so i can store multiple double variables within the buffer object/variable? (would my example buffer be a variable or would it be a object?)
You can, in a way of thinking, store multiple objects, but technically no you are only "storing" one thing. The pointer itself is a variable. It only stores the location of a single double. What makes the pointer act like a container is that you know you have 8 doubles in a row and are looking at one of them. You can then make the pointer move around through your array by adding to or subtracting from the address that it holds. The pointer itself knows basically nothing about what it's pointing at. buffer points at some 8 byte thing and it's up to you to know what that is and how to use it. Add 1 to the buffer and it jumps ahead another 8 bytes. It's more than happy to point at literally any 8 bytes in the computer's memory.
so let me know if that makes sense to you in a real world scenario, if not, let me know what is a good example in a real world scenario.
One common use for pointers would be drawing images to the screen. For example: 1600 pixels by 900 pixels times 3 bytes per pixel (one byte for red, green, and blue) is a whopping 4,320,000 bytes! If you drew a nice picture to some drawing area in memory then wanted to transfer that to your monitor it will most likely go so slow that you would see it happening (even within a split second). So we might make something like:
drawingSurface * buffer = new drawingSurface; // Where we're going to draw.
drawingSurface * screen = new drawingSurface; // What the physical monitor will show.
setMonitorDisplay(screen); // Tell the computer which pointer points to displayable imagery.
/* Drawing code goes here. */
// Swap the pointers so that we "move" our drawing to the screen instantaneously.
drawingSurface * temp = nullptr;
temp = buffer;
buffer = screen;
screen = temp;
I hope that all made sense. Please let me know if you have any other questions and I'd be happy to clarify.
Talk to you later Siten. 