M
M
Max Onflic2015-09-16 09:39:24
Java
Max Onflic, 2015-09-16 09:39:24

Where are primitive types stored and how does the program access them?

Let we have a variable int a = 2;, global. Where is it stored and how is the low-level assignment handled? How does an application understand where a variable is located in RAM if it is not of a reference type? Or is there some link to this primitive?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Oxoron, 2015-09-16
@maxonflic

Basic answer: The value of a variable is stored on the stack. There is a program, when the OS starts, it gives out a piece of memory. This piece has a stack of variables, and this global is stored there.
Any variable hides a pointer. Roughly speaking, you write int a=2, which at runtime is transformed into mov 00ff00aa, 2. During operation, the program will ask the OS for a piece of memory, and it will hard-allocate space for the primitive in it. Wherever the variable 'a' is in the sources, there will be a dedicated address in the binaries (the compiler will try).
Plus, there is a strong dependence on the language. If Java is used, the primitive may end up on the heap (not a Java developer myself, but such situations are possible in .NET). There, in theory, the exact location in memory depends on the implementation of the collector. But here you need a Java specialist.

A
AxisPod, 2015-09-18
@AxisPod

The first problem is that there can be no global variables in C#, just like in Java.
If you declare a local variable in a method, it will go on the stack. If it is a field of a class or structure, then the variable becomes part of the object and the behavior depends on the parent.
C Objective-C I will not prompt.
In C, otherwise, global variables are compiled into a binary file. A data segment is allocated, memory will be allocated for it during loading and the data will be copied there by means of the OS. It depends on the compiler and qualifiers, const will most likely end up in a separate segment without write privileges.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question