+ 4

what exactly happens when I declare a variable?

I know variable as well as instruction data is stored in RAM but, does executable contains any kind of table containing variable names with virtual memory address? and then while executing os maintains another table containing virtual memory address mapped with physical memory address? what exactly happens?

17th Mar 2018, 1:35 PM
code learner
code learner - avatar
6 Answers
+ 7
I guess we can think of it like an office building, an application or program is like a company that rents room(s) or floors, and the O/S is the office building management, who rents out the rooms or floors. When a company (program/application) needs an office space they go ask the management (O/S) to inform how many rooms are required, or if it needs a large space, it can reserve for an entire floor. Then the building management can reserve (allocate) and prepare the room(s) for the company, or floor, as necessary, for the company to put its assets in, things that the company needs to get its business running. In computing the assets are data that needs to be processed to become information. When the renting company finds that they need more space, e.g. expanding or creating new divisions they can ask the management whether if they can have more space, the management will then reserve and prepare more space (if there was any available), or, reject the request if there are no more rooms spared. In computing this results in error "Insufficient memory" or "Not enough memory to ...". At this point, the company has two choices, either force itself to continue business with the rooms allocated (with the consequences), or cancel the plan for division expansion. In computing however, running out of space to work with will generally fail the operation, the level of failure depends on how "prepared" the program was designed to cope with such events. NOTE: This is just a simplified illustration, deep inside there's much more complications to what was here, but I guess it's pretty much like that : ) Hth, cmiiw
18th Mar 2018, 4:26 AM
Ipang
+ 5
When you declare a variable. The variable is allocated memory dynamically during runtime, that is when the code is running. Like in Java, we can say that, String s = "apple"; String s = new String("apple"); Both are actually the same. In the first one, you directly allocate some value to that variable. In the second, one we achieve that using the 'new' operator. New operator is used to make a object of any class that will have the copies of the instance variables in it dynamically. So this also stores "apple" in s. Hope this clears your concept.
17th Mar 2018, 3:45 PM
Aaron Stone
Aaron Stone - avatar
+ 5
They are usually assigned using some combination of the memory in the stack and the heap, depending on variable type and programming language. More info on stack and heap here: https://www.gribblelab.org/CBootCamp/7_Memory_Stack_vs_Heap.html
17th Mar 2018, 4:24 PM
Peter David Carter
Peter David Carter - avatar
+ 3
nope I was asking about physical memory address , virtual memory allocated to program by os and actual variables
17th Mar 2018, 4:23 PM
code learner
code learner - avatar
+ 3
The physical memory is created and soon automatically deleted by the Java Garbage Collection when the program has finished functioning.
17th Mar 2018, 4:53 PM
Aaron Stone
Aaron Stone - avatar
+ 1
when you declare a variable a named memory gets allocated in memory.so this variable actually holds the address of that location where you want to store the data. There are two types of memory static and dynamic .it store data in static memory.
18th Mar 2018, 2:23 PM
Shivangi Singh
Shivangi Singh - avatar