-2

Am having a hard time trying to figure this out. When we type in c++

int a = 4;

It is clear that the memory manager allocates 4 bytes to hold the value 4 and keeps that value in a specific address.. Giving the fact that "a" variable is just a name.

  1. Where is the variable "a" stored?
  2. Does it store it somewhere in memory and points to the memory address of 4, since we know that 4 is just a temporary objects on it's own and "a" kinda binds either a reference or pointer to it
theProgrammer
  • 226
  • 1
  • 9
  • 1
    *It is clear that the memory manager allocates 4 bytes* -- Where did you get this information from? – PaulMcKenzie Oct 15 '20 at 16:35
  • [Does this answer your question](https://stackoverflow.com/questions/64092889/how-is-code-stored-and-executed-on-the-c-abstract-machine)? – PaulMcKenzie Oct 15 '20 at 16:38
  • 2
    Might go in register without *"allocation"* (neither stack nor heap). – Jarod42 Oct 15 '20 at 16:39
  • or it may get completely optimized away. – PaulMcKenzie Oct 15 '20 at 16:39
  • I might be wrong, but 4 bytes is needed to store an integer object in memory I guess – theProgrammer Oct 15 '20 at 16:40
  • 3
    The compiler as allowed to do whatever it wants with the variable if you won't be able to see the difference in the program's behaviour. See the [As-if Rule](https://stackoverflow.com/questions/15718262/what-exactly-is-the-as-if-rule) – user4581301 Oct 15 '20 at 16:41
  • 1
    Fun fact: C++ only specifies the minimum size of `int`, 16 bits, and that it can be no larger than `long`. [Here is a page describing the rules and common implementations](https://en.cppreference.com/w/cpp/language/types) – user4581301 Oct 15 '20 at 16:42
  • 1
    @theProgrammer -- When you write C++ code, you are describing what you want to do. What happens with that description -- why do you really care, as long as the resulting program performs what you have described. That's what writing to an abstract machine, as well as the as-if rule is all about. – PaulMcKenzie Oct 15 '20 at 16:45
  • @PaulMcKenzie am just a curious beginner – theProgrammer Oct 15 '20 at 16:46
  • If the variable `a` is not modified after initialization, the compiler may consider the variable `a` as a constant, and store the value in the executable (provided that the variable `a` is accessed). – Thomas Matthews Oct 15 '20 at 16:56
  • Curiosity is good, but one of the unfortunate truths is compilers and what they do is a LOT more complicated than is generally covered in introductory books and courses. To find out what is really happening takes a lot of reading and experimentation, and often the results of that work are not portable. Another compiler may do something else. The same compiler may do different thigs with different options set and different target platforms. This isn't a "Give up. It's unknowable." because it is knowable, but a warning that you need to be pretty fluid. – user4581301 Oct 15 '20 at 16:56
  • 1
    You may want to take some courses in Compiler Theory. The variable `a` is a *symbol* with a type and a value. The compiler will place this into a "symbol" table for later usage. So, `a` is more than a name. – Thomas Matthews Oct 15 '20 at 16:59
  • @Thomas Mathews....This is the best comment – theProgrammer Oct 15 '20 at 17:10

2 Answers2

3
  1. Where is the variable "a" stored?

From my experience, "a" is not stored at run time. The standard does not require it to be stored either. It is a name used for compiling, linking, and debugging. The data needed by the compiler, the linker, and the debugger are platform-specific. The standard does not dictate the nature of that data.

Given that, your second question can be ignored.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
2

Are Variables in C++ implicitly pointers

No. Variables are not implicitly pointers.

Where is the variable "a" stored?

Somewhere (or nowhere) in memory. Allocation of memory for variables is responsibility of the language implementation.

Does it store it somewhere in memory

Yes (or nowhere).

and points to the memory address of 4

No. There is no such pointer in the abstract machine.

So how does "a" know about the object "4"

4 is not an object. 4 is a literal, or a value depending on context / perspective.

a isn't something that knows anything. The compiler knows that the object named by a has the value 4.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Am having a hard time understanding this...So how does "a" know about the object "4" – theProgrammer Oct 15 '20 at 16:48
  • Another fun fact: 4 is not an [object](https://en.cppreference.com/w/cpp/language/object). It is a [literal](https://en.cppreference.com/w/cpp/language/integer_literal). – user4581301 Oct 15 '20 at 16:53