Data segment in c


All the segments are used for specific purpose. Like segment number 15 is used for ROM, segment number 14 is used for BIOS etc.  



We will discuss about how to access text video memory, graphics video memory in the pointer and union chapters of 255 bits color graphics programming.

Segment number eight has special name which is known as data segment. This segment has been divided into four parts. This is very important for c programming




1. Stack area

All automatic variables and constants are stored into stack area. Automatic variables and constants in c:

1.       All the local variables of default storage class.
2.       Variables of storage calls auto.
3.       Integer constants, character constants, string constants, float constants etc in any expression.
4.       Function parameters and function return value.

Variables in the stack area are always deleted when program control reaches it out of scope. Due to this stack area is also called temporary memory area. For example:

What will be output of following c code?

#include<stdio.h>
int main(){
    int i;
    for(i=0;i<3;i++){
         int a=5;
         printf("%d",a);
    }
    return 0;
}

Output: 5 5 5

Explanation: Since variable a is automatic variable, it will store in the stack area. Scope of variable a is within for loop. So after each iteration variable a will be deleted from stack and in each iteration variable a will initialize.  


This two concepts are only for Turbo C 3.0
It follows LIFO data structure. That in the stack area of memory data is stored in last in first out. For example:

What will be output of flowing c code. (Turbo c 3.0)?

#include<stdio.h>
int main(){
    int a =5, b = 6, c = 7,d =8;
    printf("%d %d %d");
    return 0;
}

Output: 8 7 6
Explanation:

Default storage class of variables a, b, c and d is auto .Since it automatic variable it will be sorted in the stack area of memory. It will store in the stack as




Stack always follows LIFO data structure. In the printf function, name of variables is not written explicitly. So default output will content of stack which will be in the LIFO order i.e. 8 7 6.

It has two part one for initialize variable another for non-initialize variable. All initialize variables are more nearer than not initialized variable and vice versa. For example:

What will be output of following program (Turbo c 3.0)?

#include<stdio.h>
int main(){
    int a =5, b, c =7;
    printf("%d %d %d");
    return 0;
}

Output: 7 5 garbage value
Explanation:

Automatic variable a and c has initialized while b has not initialized. Initialize variables are more nearer than uninitialized variable .They will be stored in the stack. So due to LIFO first output will be 7 then 6 (since a is more nearer than b with respect to c) then any garbage value will be output which is present in the stack.

Note: Default storage class of any local variable is auto.


2. Data area:

All static and extern variable are stored in the data area. It is permanent memory space and variable will store in the memory unless and until program end. For example:

What will be output of following c code?

#include<stdio.h>
int main(){
    int i;
    for(i=0;i<3;i++){
         static int a=5;
         printf("%d",a);
    }
    return 0;
}

 Output: 5 6 7

3. Heap area:

This memory area is use to allocate memory dynamically. In c we can allocate the memory space dynamically by using function malloc and calloc. It always allocates memory in the heap area. Its size is variable and depends upon free space in the memory.

4. Code area:

Function pointer can only access code area. Size of this area is always fixed and it is read only memory area.







C tutorial home.

15 comments:

cherry said...

in data area program you should write a++ after the printf statement then only output 5 6 7

vin said...

In the above data segment example code should be..
#include
int main(){
int i;


for(i=0;i<3;i++){

static int a=5;

printf("%d",a++);
}
return 0;
}

Anonymous said...

#include
int main(){
int i;
for(i=0;i<3;i++){
static int a=5;
printf("%d",a);
}
return 0;
}



OUTPUT is 5 5 5

Anonymous said...

#include
int main(){
int i;
for(i=0;i<3;i++){
static int a=5;
printf("%d",a);
}
return 0;
}
for this program output is: 5 5 5

Anonymous said...

can any one explain how the Function pointer can access the code area with example .............
thanks in advance

Anonymous said...

add the a++; after the printf() statement the the output is 5,6,7

Anonymous said...

Please expalin how the code area is read only and what is actually meant by function pointer can access only code area

shiva shankar blore said...

function pointer is similar to the instruction pointer we use in assembly programming...

matheen smart vip said...

#include

int *call();
void main(){

int *ptr;
ptr=call();

fflush(stderr);
printf("%d",*ptr);

}
int * call(){

static int x=25;
x++;

return &x;
}

Pensieve Thoughts said...

Good explanations my placement fears are no more. Thanks a lot. Keep up the good work.

Unknown said...

yes you are ri8 cherry..

Unknown said...

Superb explanation.. easily understandable thanks alot

Anonymous said...

are constants really stored at STACK? some other document mentioned that it's stored at data segment. Can somebody clarify?

Unknown said...

where the pointers exactly stored ?

Unknown said...

gud explaination
pls anyone help me where pointers are store