1

We recently learned pointers in my college c++ class, and I just have a bit of a technical question, because when I am speaking about pointers, I want to make sure what I am saying makes sense.

So, a pointer stores a memory address for something like a variable or object. But, when you dereference that variable to edit the value AT that address, behind the scenes is that really accessing and editing the value stored in the RAM at that location, or is it accessing the variable from that memory location and then just changing it's value. Or is it one in the same? I hope that makes sense.

In other words, would it make more sense to say "Editing the dereferenced pointer changes the value at that memory location" or "Editing the dereferenced pointer changes the variable connected to that location". Or am I just overthinking this, and those statements are really one in the same because editing the value at that memory location also edits the variable at the same time because that's where the variable gets the data anyways?

David G
  • 94,763
  • 41
  • 167
  • 253
  • 1
    The "contents of a variable" is a programming construct. Those contents are "stored in memory", an implementation detail. They're "the same thing" - at two different levels of abstraction. One could also talk about the NAND gates that implement "memory". Or the "domain model" the variable represents. Again - "different levels of abstraction". – FoggyDay Feb 13 '20 at 19:17
  • 1
    I also think this is a perfectly valid question. The downvotes may be because the answer can be found here on SO already, and just about everywhere on the net, given a good search. Examples: https://stackoverflow.com/questions/57483/what-are-the-differences-between-a-pointer-variable-and-a-reference-variable-in?rq=1 and/or https://stackoverflow.com/questions/4955198/what-does-dereferencing-a-pointer-mean?rq=1 – thomas_f Feb 13 '20 at 19:22
  • 1
    Keep in mind that C++ compiles down to raw binary code -- things like "variables" don't really exist at run-time, rather there is only an undifferentiated soup of CPU opcodes and raw memory. – Jeremy Friesner Feb 13 '20 at 19:31

2 Answers2

2

Maybe this is just a terminology issue. Without being too pedantic, the standard terminology is:

  • Object is a thing that has a storage area assigned to it, and that area stores a value. The object may or may not have a name,
  • Variable is either an object that was declared with a name, or a reference with a name.

In your question, you modify the value of an object by writing a new value to its storage area .

An expression of the form *p has the same category as the name of an object (the term for this is lvalue) , it designates the storage area of an object and it can be used to read or write that area.

M.M
  • 138,810
  • 21
  • 208
  • 365
1

In this code:

int *p = new int;
*p = 5;

the value that p points to does not have a name. There is no "variable" there, just a memory location. So it is probably better to just say "the value at that location has been set to 5".

M.M
  • 138,810
  • 21
  • 208
  • 365
stark
  • 12,615
  • 3
  • 33
  • 50
  • Yes but in this case you are dynamically allocating the pointer, what about if you initialize the pointer from the address of a variable you have created like int x = 56; – Kody Simpson Feb 13 '20 at 21:37
  • 1
    The point is that there is no requirement for doing it that way, and the pointer doesn't know whether it points to a variable or anonymous memory. It is changing the location. If the pointer points to a variable, it is changing the variable's value, but that isn't always the case. – stark Feb 13 '20 at 21:55