Memory allocation for C programs

This is a discussion on Memory allocation for C programs within the C Programming forums, part of the General Programming Boards category; Hi All, Please let me know, For a C program, where the statements and variables are stored in memory (heap, ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    22

    Question Memory allocation for C programs

    Hi All,

    Please let me know,

    For a C program, where the statements and variables are stored in memory (heap, stack) and which segment of memory?

    any link for memory allocation for C programs.......


    Thanks in advance.
    Bhupesh

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Your question sounds like part of an interview or school question.

    If you can pose a more precise question, I'd be happy to answer you question, but as it stands, your question is a bit vague.

    --
    Mats
    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
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I too have no clue what the question is, but here's a guess at the answer:
    Use malloc() to allocate memory on the heap:
    Code:
    char* str = malloc( 80 * sizeof( char ) );
    Normal (non-pointer) variables are allocated on the stack:
    Code:
    char str[80];

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    22

    Question

    Hi

    Thanx for the reply, actually what i m asking is,

    I am asking about the memory segment, as i read earlier there are four memory segments (Code segment, Data segment, Stack segment, Extra segment).

    I know data with the melloc, calloc, alloc, takes memory in heap segment.

    while the data arguments used in function call are stored in stack segment.

    Suppose we write a C program,

    like,

    Code:
    main()
    {
          int a = 9, b = 10;
          int *c;
          c = &a;
    
          //already defined some computational function
          compute(a, b)
    
    }



    Where this all statement and data will be stored? its an interview question only...

    As i know statements will go to the code segment.

    if we allocate memory to a data, will go to the heap(extra).

    function been called will go to the stack segment (in stack segment, here the arguments and other variables will b stored here.)



    Please clear me these things........


    Thanx
    ----------------------------
    bhupesh

  5. #5
    Registered User
    Join Date
    Nov 2007
    Location
    Bangalore, India
    Posts
    24
    Code:
          int a = 9, b = 10;
          int *c;
          c = &a;
    The variables a, b and c would be stored on stack.
    And the values 9 and 10 would be saved along with the code ( in text segment ).

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Note that ALL of this is completely and utterly meaningless if you are using a modern 32-bit compiler, such as gcc or MS Visual Studio. Using a "DOS" compiler these days is pretty bad.

    The "segments" you are talking of are the processor segments in x86 real-mode. In this case, code (that is your "statements of C") end up in the Code-segment, the Data segment contains your global variables, and the heap, Stack is used for the call-stack and local variables.

    Note however that the segments are not entirely static. There may for example be several sections of code, in different segments. The processor uses the code-segment to point to the current section of code, but it can change from one section to another by what's called a "Far call" or "far return" - "far" here means that not only the instruction pointer, but also the code-segment is changed.

    Likewise, the data-segment can "move around", such that the data-segment register doesn't always point to the same place. If you use the heap, then DS will (for a time) point to some heap location.

    ES is uses, as the name implies, as an "extra" segment, when for example copying from one data segment to another [typically from the heap to a global data variable, or the other way around]. Functions like memcpy() are often implemented as "rep movs", which implicitly takes DS as the source segment and ES as the destination segment. Modern x86 processors [286 and later] have two more "extra" segments, called FS and GS.

    Have a look here for a further explanation of C and Memory Models in real-mode
    http://en.wikipedia.org/wiki/C_memory_model

    --
    Mats
    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.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    > I am asking about the memory segment, as i read earlier there are four memory segments
    > (Code segment, Data segment, Stack segment, Extra segment).
    That's a very implementation specific view of what C uses.

    C doesn't even assume a stack, only that there is a mechanism for managing dynamic scope, of which a stack is a common implementation choice.
    Also, I don't think there is any distinction in the standard between what would be code and data memory.

    Historically, the original C compilers used sections called .text, .data and .bss (program, initialised data, uninitialised data respectively). With C89, .rodata (for constants) became common as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory allocation question
    By dakarn in forum C Programming
    Replies: 11
    Last Post: 12-01-2008, 11:41 PM
  2. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 04:59 AM
  3. C memory allocation to c++
    By markucd in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2005, 05:56 AM
  4. Understanding Memory Allocation
    By Ragsdale85 in forum C Programming
    Replies: 7
    Last Post: 10-31-2005, 08:36 AM
  5. memory allocation ptr to array? how?
    By kokopo2 in forum C Programming
    Replies: 8
    Last Post: 09-01-2005, 06:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21