Saturday, March 17, 2012

Memory Segment

Computer Memory is organized into following -
Data Segment- Consist of Data, BSS and Heap)
Stack
Code segment

Code/Text segment - Contains all the executable code.CS register is a 16-bit register containing address of 64 KB segment with processor instructions. CS + IP(instruction pointer) gives the access to all instruction in the code segment.

DataSegment -
Data- Data area contains non zero or non null static and global variables. It consist of read only as well as read-write block.
BSS(Block Started by Symbol)- It contains all global and static variables that are initialized to zero or don't have any initialization at all.
DS register is a 16-bit register containing address of 64KB segment with program data.Index register (SI, DI) are used to access all data on data segment

Heap - Heap is the area where all dynamic memory comes from. This area contains all objects which are created by new key word.

Stack -
Stack contains all other local variables used in program. It is the use of stack for function parameters and return values that makes it convenient to write recursive functions. Variables stored on stack disappear when function containing them returns. It grows opposite to heap.

Here is the diagram which shows how a program fits into this memory model -
Concept- Assume there are 3 different instance of class A, say i1,i2 and i3. Now if we execute f1, f2, and f3 for all these instance then as per the memory model it get executed from same memory location. Then how do the CPU knows whether the code it is executing is for i1,i2 or i3? The answer is "this" pointer. Whenever we execute any function like i1.f1() then actually its signature become f1(this), then it gets to know the object for which it is executing this function.


Concept - Now if i have a function f1 like this -
f1()
{
     int a;
     int b;
     a++;
}What do you think where will this a and b will be defined? The answer is simple, as these are local variable it means those will be defined on stack.
Can you think how a++ will be stored in code segment? When processor execute this function it initialize a then b and then it comes to increment a, how do it know which value need to increment? Do the CPU maintains some kind of map to identify where a is? If you think so, then think about the performance hit you will get because of that.Actually the instructions are stored in following way-
a -> SP + 0 ( 0 is just for example, it might be anything)
b -> SP + 4
a++ -> [SP + 0] + 1
so actually at the low level it never know what a or b is, it just understand the memory addresses.

Example- Say there are 3 instance of class A i1,i2,i3. and it execute this f1 on 3 instance in 3 different thread. Is it possible that value of a get modified by thread 1 and the modified value is seen by thread 2? if Yes/No how?
Ans - No, one thread will never be able to see a because they have different stacks.
for example a in thread 1 is SP + 100, then in thread 2 it will be SP + 200(any other number but 100).
So actually this SP + X is always different for different thread.

No comments:

Post a Comment