tags:

views:

35

answers:

3

Hello, I would like to know this:

Let's assume I let system allocate memory for an array e.g. MyArray. How then computer knows when I use this MyArray variable where to look? How are these named variables managed at low level?

In short I want to know the relation between defined variable and physical address in memory.

I hope you can understand as this is little cumbersome explanation I couldn't find better words.

A: 

I assume what you are looking for is how runtime systems do dynamic memory allocation. Depending on the programming language / runtime however, memory can be also allocated on the stack.

There was a similar question that you may have a look at too.

inflagranti
A: 

A variable stores the address of the memory so when your program ask for the value in myVar it is actually getting a memory address.

A bit like when you search for a website. You my ask for www.google.com to be displayed but it's actually the ip of google that is used to get the information.

Ash Burlaczenko
That address is stored on a stack, which is another memory allocation method :) An a reference to that address is calculated as the stack top address minus constant value corresponding to that variable.
Basilevs
A: 

In short I want to know the relation between defined variable and physical address in memory.

Your variable=its virtual address.

The virtual to physical address mapping is done by the virtual memory subsystem of your operating system. It is generally uninteresting to know the physical address of your variables.

Jens Björnhager
So if I declare a variable, program creates some sort of "pointer" and when i refer to that variable in my program later on then in machine code I access my variable data by this "pointer" which points to actual memory - is it something like that?
uther.lightbringer
The variable you are declaring IS a pointer to the variable type of your choice. When you want to allocate space for your variable, you ask the operating system for it, and you get a pointer to the start of the memory allocated. For comparison, static variables are already allocated when you start your program, and their pointers hard-coded into the code.
Jens Björnhager