-2

I am a C tutor and dev that is trying to deepen my understanding about what's going on during compilation. I have come across C tutorial which says that

After compilation there are no more variable names, just addresses

I assume the the left operand of the = operator must be an ADDRESS!
Following that logic, it's safe to assume that the assembly command for x=9 will be something like:
put the literal 9 into the address of x

So isn't it safe to say the the lvalue x is replaced with its own address?
And if so - why isn't is possible to just give the address directly like so:
&x=9
Which seems to me will result in the same assembly commands..
After all, this lvalue on the left hand side will be evaluated to its address, and this address will be sent as an operand to the = operator

Jason
  • 36,170
  • 5
  • 26
  • 60
  • when `&x` is the adress of `x` then you can replace `x` with `*(&x)` not with `&x` – 463035818_is_not_an_ai Jul 25 '22 at 13:21
  • The pointer-to operator `&` will give you a pointer to something. The dereference operator `*` will give you the value a pointer is pointing to. So the assignment should in that case be `*(&x) = 9`- – Some programmer dude Jul 25 '22 at 13:22
  • 2
    How is this different from [your earlier question](https://stackoverflow.com/questions/73091689/why-are-lvalues-converted-to-values-in-certain-places-and-not-others) on this same issue? – Nathan Pierson Jul 25 '22 at 13:23
  • 1
    The best way to learn is by referring to a good book and not by watching video tutorials. – Jason Jul 25 '22 at 13:27
  • 4
    "x=9;" says "Compiler, please emit code which makes the value of x equal to 9." so emits code that writes 9 to the address of x. "&x=9;" says "Compiler, please emit code which makes the value of &x equal to 9" which is impossible. – user253751 Jul 25 '22 at 13:28
  • 3
    Let's say that after compilation an address is used instead of `x`. How would "the address of an address" make any sense? – Weather Vane Jul 25 '22 at 13:29
  • @user253751 but what is the meaning of "x" after compilation? isn't the variable 'x' replaced with its address? Isn't it more accurate to say "Set the bits at address of x to be 9"? – forstack overflowizi Jul 25 '22 at 13:31
  • @WeatherVane It is my understanding that at compilation the lvalue (x) will be evaluated/replaced with its own address, so the assembly command will eventually be "
    ="
    – forstack overflowizi Jul 25 '22 at 13:38
  • Why do you think `x` will be replaced by its own address but nothing happens to the `=`? You seem to be randomly mixing C instructions and assembly instructions. You can't half-convert a line of code into assembly and expect it to still have the same meaning as the original C. – Nathan Pierson Jul 25 '22 at 13:39
  • @forstack the `&x` isn't an lvalue. You can use it in a computation but you cannot assign anything to it. – Weather Vane Jul 25 '22 at 13:42
  • "just addresses and registers" would be more accurate. The compiler might choose to store a variable of automatic storage class the same as it stores a variable of register storage class if no code uses the address of the variable. – Ian Abbott Jul 25 '22 at 13:49
  • 1
    @forstackoverflowizi The compiler's job is to make it seem like variables exist. Yes, the compiler will work out some address for the variable x and in the compiled program it will just say the address instead of saying the letter x – user253751 Jul 25 '22 at 13:51
  • 1
    *put the literal 9 into the address of x* I think part of your confusion may be an unclear understanding of the vital distinction between *a pointer value* versus *the thing the pointer points to*. Or, using more assembly-language terminology, the distinction is between *an address* versus *the value stored at that address*. So the more correct interpretation of the code fragment you asked about would be "store the value 9 at the memory location with the address of `x`". – Steve Summit Jul 25 '22 at 13:51
  • @user253751 Wow that's beautifully put – forstack overflowizi Jul 25 '22 at 13:53
  • Perhaps think of it this way. My name is Steve. I live at #139 on my street, and out by the street I have a mailbox. You can put a letter for me in that mailbox. When you try to figure out which mailbox to put it in, at a low level you can put it in mailbox #139, or at a higher level you can put it in Steve's mailbox — since Steve's address is 139. – Steve Summit Jul 25 '22 at 13:53
  • or more generically, a compiler's job is to make a program that simulates a different program. In this specific case the compiler's job is to simulate a variable because your program has a variable in it. As long as the compiler does it properly, the variable will act like a variable - guaranteed. Some languages don't have variables, or they have different variables, like Haskell. Haskell compilers have to simulate different kinds of variable than C compilers do. – user253751 Jul 25 '22 at 13:56
  • I appreciate all of your answers, you are awesome, this really helped – forstack overflowizi Jul 25 '22 at 13:59
  • So basically you guys are saying that there is NO direct assignment to an address. I can only assign to memory through the optical illusion created by the compiler and called VARIABLES – forstack overflowizi Jul 25 '22 at 14:01
  • 2
    If you have an address and you want to assign to the object pointed to by that address, you can dereference that address first. If `someAddr` is an address of an `int`, you could do `*someAddr = 9;` to assign `9` to the object pointed to by `someAddr`. What you could not do is say `someAddr = 9;` and expect that to change the object pointed to by `someAddr`, because that's not what `=` means in C. – Nathan Pierson Jul 25 '22 at 14:03
  • @NathanPierson Thank you for your detailed and helpful insights, this is appreciated – forstack overflowizi Jul 25 '22 at 14:06
  • Of course you can assign values directly to addresses, on those (perhaps rare) occasions when you need to. See [this previous question](https://stackoverflow.com/questions/72760320). – Steve Summit Jul 25 '22 at 14:07
  • Languages offer logical variables, whereas processors offer physical storage, which include both registers and memory. Translators/compilers translate variables in programs into reservations and access of this physical storage, which could be memory or registers. – Erik Eidt Jul 25 '22 at 14:38
  • @NathanPierson Your answer was exactly what i was looking for. The evaluation of x doesn't happen in vacuum, but both `=` sign and `x` evaluated together to be "Assign rhs to the address of variable x". Up until now i didn't realize the operators and variables are being evaluated together, i honestly thought that they are evaluated sequentially and independently – forstack overflowizi Jul 25 '22 at 14:42
  • Perhaps it would help to take a look at the compiler-generated assembly for some very simple functions (not whole programs). Use `volatile int x;` if you want to force operations on it to not be optimized away, even with optimization enabled to get rid of other noise. See Matt Godbolt's CppCon2017 talk [“What Has My Compiler Done for Me Lately? Unbolting the Compiler's Lid”](https://youtu.be/bSkpMdDe4g4) and [How to remove "noise" from GCC/clang assembly output?](//stackoverflow.com/q/38552116). That might help to understand what an address is, but `&x=9` being wrong is more of a C thing. – Peter Cordes Jul 25 '22 at 16:33

1 Answers1

0

Variable is just a chunk of memory. Assume that integer is 4 bytes long.

int x;

/* ... */

x = 9;

Simplifying: x has to be stored somewhere in the memory. Memory cells are identified by their addresses. The compiler and linker convert the human-readable symbol x to the addresses of some memory cells in memory. Machine code instructions write some values (in our case 4 bytes) representing the number 9. In most modern implementations it will be 9,0,0,0.

In real life, it can more complicated as the compiler can keep it in registers or optimize it out completely.

And if so - why isn't is possible to just give the address directly like so: &x=9

&x is a reference to the object x. You can't assign it as it is a fixed location in memory (you can't change it). But you can of course dereference (access) this location

*&x = 9;

References (addresses) can be assigned to pointers.

int x = 0;
int *pointer = &x;
0___________
  • 60,014
  • 4
  • 34
  • 74