Monday, June 15, 2009

Scope/Life Time

Ok to set the things straight

there are three kind of scope ( place in the code where variable can be accesed )

extern : entire prog
auto : in the block where its defined.
static : now there is a nice twist here in words. I will say if defined inside a funtion it has a auto scope.
But it you define it before a function then it has a scope there after but only in the file where its defined.


Now its life time utill when variable is having life.... fundu haan :)

Not like that it simply means that point of time when variable can be accessed.

Unfortunatly there is no keyword as scope to define life time.

Life time of static and extern variable is before main is called and utill program exits.
Life time of arguments and that of local variables it is from the point when funciton is called and untill function return.
Life time of dynamic allocated block is from the point when we issue malloc or calloc command to the point when we use free call.

Why Do we Require Segments in C/C++ Language

Why to have many segments in a ELF file?

This can be easily explained why number of storage classes available in C language and some basic computer Science knowledge.

Code Segment

When we compile code we will get binary language code. These are the instructions to the processor what to do.

So to execute any program you need these instructions. These instructions will not change throughout you program. Processor needs to have these instruction in the RAM or in some cases of embedded system they can be directly read form the ROM (if byte addressable). That’s the reason to put them in a segment called Code or Text segment. Before executing the program this segment is copied to RAM and program starts executing by reading these instructions.

Data Segments (initialized variables)

C has provided global variables (for the time being consider initialized variables). Why to use global variable? Answer is very simple we want to have variable visible to entire program/file and more importantly they will retain their values throughout the program. Aren’t they required in RAM for full life span of program? Yes that’s why placed in the segment called data segment. Data segment is part of the object file as well as executable.

Initialized Static variables also has behavior they stay alive full span of program so are also placed in the data segment.

BSS (Un- initialized Static/Global Variable)

Ok you want global variable but its not being initialized. As it doesn’t have any value associated till run time, why to waste the space in the object file/ executable. That’s the reason there is one more segment made called block started by symbol BSS where just the information about these kind of variable is stored. At run time appropriate memory is allocated to these types of variables.

Read Only data Segment

I something is const, how does file format will ensure that modification of such kind of data is prohibited and if you try to do so; compilation/runtime error is resulted. Very easy, file format store such kind of variables in a region which is read only. You can only modify the region value one time and that is during initializing. So all the constants, string literals are stored in this read only segment.

Heap Memory and Stack Memory

These are not the part of the executable/ object file. These are run time entities.

Where Const Variables are stored

Yeah this is very important question and to answer it in one sentence will be “implementation dependent”. Now one thing should be clear and that is difference between constant and read-only. To be honest term “constant variable” is oxymoron. Data can be constant a object can’t. Its pretty obvious if entity changes its value then that’s not a constant.

A variable can have cont qualifier in that case its just marked as read only. As I have told in one more post also “adding const qualifier to a variable is promises you make that variable will not change its value and it’s up to the compiler to keep this promise”. Const variable is marked as read only but there are still ways to breach that read-only hindrance. Want to have a proof in “C”

Look at this

Cont int a =20;

int *p=(int *)&a;

a=30; //Compilation erro

but

*p =30; will get through.

If you have noticed the above few lines carefully you can make out that const variables are not stored in rodata segment. They will go to stack only (if local).

Making any variable const does not change the place where variable is stored.

Register Variables

Register variable ;

Its simply a request to the compiler that perticular variable will be used heavly and place it in CPU registers.

But there are lots of gotchas here

1) Which data can be held in registers depend on register size. Normaly its size of word or interger elemnet max. Though some system do have the registers capalbe of holding the floating point values in registers.

2) Though its a request but once you declare a variable as register it may go to the register. So you cannot use its address in your program. It means you can not use & operator on the register variable. If you try to do the same you are welcomed with compiler error.


3) More over you declared a variable regiter and system choose to put it in the register. It may actually slow down your system. Because no of registers are limited in numbers and you have reserved some of them, now the variable you have placed in the register is not modifying enough so its like wasting a resource.


4) You can ask when to use register type. Plain answer is never.
In earlier days it was a value addtion to the 'C' language as earlier compiler were not using register at all implicitly.
But mordern compilers are smart enough to decide which value is to be placed where. Actully its a rare event that by putting some value in register can increase the efficeny of your program.

Some of todays compiler simply ignore the register keyword as its perfecly inline with reiger definition "Its a request not an order".

Static Vs Dynamic Ram

What is the difference between static RAM and dynamic RAM?

Inside this Article

1. What is the difference between static RAM and dynamic RAM?

2. Lots More Information

3. See all Memory articles

Computer Videos

* More Computer Videos »

capacitor
A capacitor stores electrons in computer memory cells. The memory must then be refreshed or flip-flopped.

­Your computer probably uses both static RAM and dynamic RAM at the same time, but it uses them for different reasons because of the cost difference between the two types. If you understand how dynamic RAM and static RAM chips work inside, it is easy to see why the cost difference is there,­ and you can also understand the names.

Dynamic RAM is the most common type of memory in use today. Inside a dynamic RAM chip, each memory cell holds one bit of information and is made up of two parts: a transistor and a capacitor. These are, of course, extremely small transistors and capacitors so that millions of them can fit on a single memory chip. The capacitor holds the bit of information -- a 0 or a 1 (see How Bits and Bytes Work for information on bits). The transistor acts as a switch that lets the control circuitry on the memory chip read the capacitor or change its state.

A capacitor is like a small bucket that is able to store electrons. To store a 1 in the memory cell, the bucket is filled with electrons. To store a 0, it is emptied. The problem with the capacitor's bucket is that it has a leak. In a matter of a few milliseconds a full bucket becomes empty. Therefore, for dynamic memory to work, either the CPU or the memory controller has to come along and recharge all of the capacitors holding a 1 before they discharge. To do this, the memory controller reads the memory and then writes it right back. This refresh operation happens automatically thousands of times per second.

This refresh operation is where dynamic RAM gets its name. Dynamic RAM has to be dynamically refreshed all of the time or it forgets what it is holding. The downside of all of this refreshing is that it takes time and slows down the memory.

*

Static RAM uses a completely different technology. In static RAM, a form of flip-flop holds each bit of memory (see How Boolean Gates Work for detail on flip-flops). A flip-flop for a memory cell takes 4 or 6 transistors along with some wiring, but never has to be refreshed. This makes static RAM significantly faster than dynamic RAM. However, because it has more parts, a static memory cell takes a lot more space on a chip than a dynamic memory cell. Therefore you get less memory per chip, and that makes static RAM a lot more expensive.

So static RAM is fast and expensive, and dynamic RAM is less expensive and slower. Therefore static RAM is used to create the CPU's speed-sensitive cache, while dynamic RAM forms the larger system RAM space.