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.
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.
No comments:
Post a Comment