EmbLogic's Blog

Some important things about segments…

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…..:-)

 

3 Responses to Some important things about segments…

  1. gaurav sharma says:

    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

  2. nitin sharma says:

    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.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>