1. The address of variables stored in data segment never changes.
2. All the functions are allocated memory in data segments whether it is defined inside main(locally) or outside main(globally).
3. Initialized global variables are stored in data segment, UN-initialized global variables are stored in BSS.
If you know anything new , please let me know.
thanx…..:-)
also the memory allocated by malloc is heap …because memory in heap remains till we free it explicitly a program to depict the usefullness of malloc
int* func()
{
int i;i=10;
return(&i);
}
int main()
{
int* ptr;
ptr=func();
printf(“%d”,*ptr);
}when we execute this program it gives a warning …reason being when we are returning the address of i from func then i is a local variable of func which vanishes after func exits ….thus the pointer in the main function becomes a bad pointer that is not pointing towards anything…but if we use int* ptr=(int*)malloc(sizeof(int))
and then use return(ptr); and then dereference the ptr pointer in main we will not get any warning because the memory in heap remains
thanx Gaurav..
hi manoj,
all the functions are allocated memory in text segment not in the data segment. only global and static variabls are allowed to be stored in data segment. The variable of functions are stored in stack.