Thread: Memory layers in c

  1. #1
    Registered User
    Join Date
    Oct 2007

    Memory layers in c

    Hai i need one help from you . Can any one explain more about the memory layers in c.I know that there are three layers
    1.Data segment(used to store global variables and code lines)
    2.Stack segment(used to store function calls and local variables)
    3.Heap segment

    i know this but i need to know more about this please any one explain me in detail...Thanks in advance...

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    Not sure I would call that "layers".

    I'd call it "sections"....

    Code is not stored in "data" - it is stored in "Code" or "Text" segment.

    Data is used for global variables that have been given a value.
    A special type of "data" section is used for other global variables - those that are set to zero by the runtime library. This section is usually called "bss".
    The initialized data section is sometimes also split into a "const data" section and "modifiable data section" - so for example constants for printf() formats, string literals (char *str = "something") will be stored in a const region and can't be modified.

    Stack is a section initialized by the "createprocess" or "createthread". It holds local variables and function return addresses [how to get back to where you came from when you call a function that calls a function that calls a function].

    The heap is (in modern OS's) dynamic, that means that there is no heap at all until somene (part of the C runtime, usually) makes a call to the OS to get some memory for heap. The heap is managed by malloc/free and their close relatives (calloc, realloc for example).

    The memory itself is no different for code, data, heap or stack - it's just plain memory, and the same memory can be used for any purpose.

    Please ask further questions if you feel a need.

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Thank you for your replay . Can you explain more about the code and data segment . You had told that the code lines will be stored in this section . . . And i need to understand more about that . . .

  4. #4
    Registered User
    Join Date
    Oct 2007
    I think that the code and text segment is the part of data segment...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-13-2008, 02:14 AM
  2. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  3. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  4. reading files into memory
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 11-30-2005, 03:39 PM
  5. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 02:15 AM