Data Segment -- C lang

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

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

Accepted Solution

grg99 earned 1000 total points
ID: 20033250
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

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

Expert Comment

ID: 20034263
I meant "initialized to something other than zero".

LVL 10

Assisted Solution

peetm earned 1000 total points
ID: 20035521
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

ID: 20036780

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

850 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