If you have a line of code like
int num = 4;
Does this result in the following tables?
VARIABLE|ADDRESS ADDRESS|VALUE
num |0001 0001 |4
If you were to then say
int* num_p = #
Would this result in the following tables?
VARIABLE|ADDRESS ADDRESS|VALUE
num |0001 0001 |4
num_p |0002 0002 |0001
Would then saying
int** num_pp = &num_p;
Result in the following tables?
VARIABLE|ADDRESS ADDRESS|VALUE
num |0001 0001 |4
num_p |0002 0002 |0001
num_pp |0003 0003 |0002
And so on? If so, would this same logic hold true if the initial variable were not an int
but instead a struct
?
EDIT: Check the comments on this question for info on what the addresses would actually look like as opposed to this made up 0001
, 0002
, 0003
scheme.
EDIT 2: This answer to this question points out the fact that variables do not necessarily have to have an address. This answer to an earlier question also goes into this.
Yes, what you illustrate with your tables is roughly correct. Variable names are assigned to addresses at compile time. This is called a "symbol table" and is analogous to the left-hand tables in your question. When the program runs, the variable names no longer appear in the executable and there are only addresses like you have in your tables on the right.
If so, would this same logic hold true if the initial variable were not an int but instead a struct?
Yes, a struct
is a value, so assigning an address to a variable and a value to that address works the same way. The difference is that a struct
might take more memory than an int
depending on its members. This affects what address is available for the next variable.
Note that the addresses assigned will be offsets from some base memory address. When the OS loads the executable, it gives this base address and the executable calculates the absolute memory addresses by adding the offsets to the base address.
If you are interested in learning more about how this works, you can study compilers and operating systems in more detail.
would this same logic hold true if the initial variable were not an int but instead a struct?
A structure is a type (like int, double, char ...) not a variable, so yes, what you described will apply as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With