Memory storage

int A=1;

we all know that "1" is stored in somewhere in the memory, which will take the space of a int type.

And we all know, when we are using variable A, the cpu will find the location of A and get the value of it.

However, my question is:

computers=CPU+Memory, then if CPU does the calculation then memory have to store all information.

How could cpu knows where to look for the value of A?
There must be some place stored the memory location information of A, then where is that information located? How much memory space it takes? and how to fetch that information? Another location...??? never ending forever?
computers=CPU+Memory
+I/O devices. That's the third component in Von Neumann architectures.

How could cpu knows where to look for the value of A?
There's a register that points to the top of the stack. The compiler actually generates code that treats the stack as another array:
1
2
int A=1;
int B=A+100;
could become
1
2
stack[offset_of_A]=1;
stack[offset_of_B]=stack[offset_of_A]+100;
offset_of_* are constants and hard-coded into the generated instructions.
thanks!
not that clear yet!

Then, is there any difference in memory allocation between variable declaration and variable definition?
What do you mean? Declarations (e.g. extern int a;) are only for the sake of the compiler. They don't translate to actual code.
A literary constant is not stored in memory, right? but where is that stored?
Depends on the type and size of the literal. 192 will probably be stored as part of a machine instruction, but "Hello, World!\n" does need to have memory set aside for it.
Larger literals like 3.141592f or 2404515840 may also need to be stored at distinct addresses.

EDIT: It also depends on what kind of operation you're performing. Whether the compiler decides to optimize some aspect of your program away also matters.
Last edited on
192 will probably be stored as part of a machine instruction
Which of course is also stored in memory.
Not necessarily. Imagine a hypothetical embedded device where code resides in a ROM chip in a separate address space. It's also memory (from the point of view of a Von Neumann architecture), but it's a kind of memory that a C program can't read, write, or even be aware of.
It's also memory
My point.
that a C program can't read, write, or even be aware of.
If this hypothetical device is explicitly built like this.
The microcontrollers I'm aware of allow me to read everywhere.

I was mainly reacting to
A literary constant is not stored in memory, right?
which is imho outright wrong.
Well, the C abstract machine makes a clear distinction between memory for code and memory for data.
"Memory" without any qualifiers refers to data memory, so I think OP was asking whether a literal is stored in data memory in the general case.
Whether some specific implementations combine both types of memory into one is irrelevant, in my opinion.
Last edited on
Well, if OP read our little chat he can draw his own conclusion.
Going to bed now.

Wish everybody a good $local-time-of-day.
Topic archived. No new replies allowed.