Data Segment -- C lang

Posted on 2007-10-08
Last Modified: 2008-01-09
What is the difference between

Heap vs Data Segment vs Stack and BSS ?
Question by:gauravflame
    LVL 22

    Accepted Solution

    Well, the question is a little bit too assuming on details, but here goes with the general drift:

    "BSS" is where initialized data in a C program is usually kept.

    The "Stack" is the most common place where local variables are put, along with function return addresses and parameters.

    The "Data segment" is a generic term, often theplace where uninitilized global variables go.

    The Heap is from where malloc() space comes from


    Author Comment

    what do you mean by initialized data -- BSS
    int a;
    int a = 10;
    LVL 22

    Expert Comment

    I meant "initialized to something other than zero".

    LVL 10

    Assisted Solution

    In C, something like the data in this would/could be stored in the bss segment:

    char * p = "Boo!";

    Note that that is not the case however with

    char boo[] = "Boo!";

    The former has p pointing to a literal, whilst the latter *initializes' the boo character array to the characters in the initializer [which is given in string form].

    bss will also contain stuff like the initial value given to a variable, e.g.,

    int a = 10; // the 10 *may* be dragged in from the bss segment.

    However, it is more likely that the 10 will exist in the generated code, e.g,

    mov ax, ah

    It's up the the compiler writer - and perhaps the linker writer if the compiler writer has allowed the latter such access.

    Also see here for bss stuff:

    As far as the stack/heap are concerned - well, a disclaimer first ... the C standard includes not a single mention of these terms.

    However, the heap is *usually* what C compiler writers will use and refer to as arbitrary memory available to an application vai calls to malloc/calloc etc.

    The stack is mainly used for temporary variables, and the return address of functions - as has been pointed out.

    e.g., a and b here would be allocated on the stack - if the compiler writer uses such a thing [they could be kept in a linked list on the heap for example.]

    int main(void)

    int a;
    int b;

    scanf("%d", &a);

    // blah blah

    return 0;


    Author Comment


    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
    An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
    The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now