-3

IMPORTANT: This tried to ask too many things at once and was misleading because I wrote it under a false assumption about how pointers can be used, and it ended up just looking like a duplicate. Please see this instead: How are variables tied to their values in C?


Let's consider that there is a value 4 at address 0001, and then we assign the address 0001 to the variable num. We could visualize this as two tables:

VARIABLE|ADDRESS    ADDRESS|VALUE
num     |0001       0001   |4

From what I understand, this would be the end product of the following code:

int temp = 4;
int * num = &temp;

However, what is going on at the first line, int temp = 4;? Does that first line produce something like this?

VARIABLE|ADDRESS    ADDRESS|VALUE
        |           temp   |4

And how do pointers to pointers work? Would the code:

int temp = 4;
int * num = &temp;
int ** pnum = #

produce this?

VARIABLE|ADDRESS    ADDRESS|VALUE
num     |0001       0001   |4
pnum    |0002       0002   |0001

What is the right way to think of this? What is actually going on under the hood? Also, how does this change when a struct is stored instead of a number?

I understand that the above examples are probably entirely incorrect; they were simply to contextualize my question.

NetherGranite
  • 1,940
  • 1
  • 14
  • 42
  • Primitive values are stored on the stack, so the first line you mentioned will store the value 4 on the stack, relative to the function encapsulating the value declaration (see more on the stack memory usage here : https://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap) – Rann Lifshitz Mar 05 '18 at 04:17
  • "What is the right way to think of this?" For me, I found this good: the address of `temp` is on "int street" with some house number "123". `temp` has the value of 4. The address of `num` is on "int * street" with some house number "456". `num` has the value of "int street:123". "int street" and "int * street" might refer to the same "street", or maybe not. Uncertain if this abstraction would work well for a learner. What is actually going on under the hood depends on the compiler and processor - no one answer - too broad. – chux - Reinstate Monica Mar 05 '18 at 04:28
  • 1
    There will also be a table (at compile-time) saying that the name `temp` refers to the address `0001` – M.M Mar 05 '18 at 04:31
  • How about the 123421 duplicate prior questions, how do they relate to your problem? – Antti Haapala -- Слава Україні Mar 05 '18 at 06:54
  • 1
    In any case your tables are conceptually very wrong. A pointer has an address and a value. The value assigned to a pointer is an address but it is not *the* address of the pointer itself. – Antti Haapala -- Слава Україні Mar 05 '18 at 07:04
  • [This answer](https://stackoverflow.com/a/21606805/584518) from the linked duplicate basically gives exactly the same scenario as described in this question, and shows how the variables end up in memory. – Lundin Mar 05 '18 at 08:34
  • @AnttiHaapala I couldn't find any questions that asked specifically what was actually happening in memory, with most simply looking for conceptual understanding. The question you have linked to does not ask what is actually going on in memory; it merely looks for conceptual understanding. Even if you can link a question that actually matches mine, your rude comment was uncalled for. – NetherGranite Mar 14 '18 at 04:34
  • @Lundin Even then, people who make a search query for a question like mine are not going to easily find that answer, especially since 1) people will be looking through question titles and 2) even if they do choose to view that question and its answers, it is unlikely they will find that specific answer because it is the fifth one down on a page of very long answers. – NetherGranite Mar 14 '18 at 04:36
  • @Lundin Yep, that does answer my question correctly. Would the appropriate thing to do here be to "answer my own question" with a link to that response? That question only asked for conceptual understanding, yet that answer was in fact the answer to my question, which asked for understanding of the actual mechanisms. Do you still consider my question to be a duplicate? – NetherGranite Mar 14 '18 at 04:48
  • @NetherGranite If there are answers in the duplicate that answer this question, that's usually good enough. Some flavour of this question has been asked many times before and people can usually not be bothered to hunt down the perfect dupe. The problem of finding duplicates is known defect with the SO system and have been debated endlessly at meta.stackoverflow. – Lundin Mar 14 '18 at 07:43
  • @Lundin Wait, I just realized that that answer does not actually answer my original question of what happens when non-pointer values are assigned to variables. And even if it did, the criterium for marking something as a duplicate is it being an "exact duplicate of an existing question", which mine is not. – NetherGranite Mar 23 '18 at 13:45
  • @NetherGranite All variables have an address, no matter if you point at them with a pointer or not. If you wish, I can re-open the question but I'll doubt you'll get any better answers. – Lundin Mar 23 '18 at 14:01
  • @Lundin Actually, I believe that I have lost sight of what I was originally asking. I will try asking a new question and attempt to better clarify what exactly I am looking for. Sorry for the trouble. – NetherGranite Mar 23 '18 at 14:02

1 Answers1

1

Not all variables need to have an address in the memory system, some variables are short lived enough that they can live their whole life-span in registers. In such a case, they get allocated (renamed) by the compiler to things like eax, ebx or r1, r2. Registers are slots in the CPU that can hold variable contents.

Because lots of architecture have limited register numbers (8 virtual (visible to the machine language) registers in x86-64, 256 registers in IA64...) the rest of the variables get allocated (compiled) to an address in memory, which will always be on the stack. The stack (tracked by esp register, a special register) is a last in first out allocator with support of the operating system (memory pages get live as it grows) so the compiler just has to take the current stack pointer and increment it by the size of the variable to allocate, and that's the variable's address.

Value assignation, like in the first case you demonstrated, is done by issuing a mov assembly command with a hardcoded value, the constant thus exist in the memory space of the program. Which means the value comes from the instruction itself. (L1:inst cache->fetcher->CPU pipeline->mov->STORE pipeline->L1:data cache)

The rest works like you sensed.

v.oddou
  • 6,476
  • 3
  • 32
  • 63