0

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.

Jarek
  • 7,425
  • 15
  • 62
  • 89

3 Answers3

0

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.

Community
  • 1
  • 1
Janick Bernet
  • 20,544
  • 2
  • 29
  • 55
0

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
  • 24,778
  • 15
  • 68
  • 99
  • 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 Aug 01 '10 at 14:51
0

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
  • 5,632
  • 3
  • 27
  • 47
  • 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? – Jarek Aug 01 '10 at 18:51
  • 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 Aug 01 '10 at 21:16
  • This is why it's useful for a programmer to know C. In C all of this is clearly transparent. Afterwards it's always trivial to extrapolate on how a higher language might do the same thing. – Arelius Apr 21 '11 at 18:10