Heap & BSS

K

karthikbalaguru

Hi,

Memory allocated in heap remains until the end of the program.
So, global variables & static variables are allocated on heap.

In the same time, I find that BSS also allows the placement of static
variables and global variables initialised to zero.

So, while run-time, where does the static/global variables lie in the
Memory Organisation.
Is it in BSS or HEAP ?

I tried googling, i did not find any specific info w.r.t deciding
between bss or heap .
Could someone here clarify me regarding this ?

Thx in advans,
Karthik Balaguru
 
S

santosh

karthikbalaguru said:
Hi,

Memory allocated in heap remains until the end of the program.
So, global variables & static variables are allocated on heap.

In the same time, I find that BSS also allows the placement of static
variables and global variables initialised to zero.

So, while run-time, where does the static/global variables lie in the
Memory Organisation.
Is it in BSS or HEAP ?

I tried googling, i did not find any specific info w.r.t deciding
between bss or heap .
Could someone here clarify me regarding this ?

From a standard C perspective, we cannot answer your question, since it does
not mention either the heap, nor the bss. They may be present in certain
implementations while absent in others.

<OT>
Try looking at the assembler output of your compiler. It's usually an '-S'
option. Generally global and static objects may be defined in data or bss
sections. The heap is used for runtime allocation through malloc and co.
<OT>
 
C

Chris Torek

Memory allocated in heap remains until the end of the program.

Unless it does not, e.g., when using any of the Unix-like systems'
malloc() implementations that use mmap() and munmap() or similar.
So, global variables & static variables are allocated on heap.

Unless they are not, as is usually the case.
In the same time, I find that BSS also allows the placement of static
variables and global variables initialised to zero.

Unless the system in question lacks the concept of a "BSS segment"
and/or does something fancier.
So, while run-time, where does the static/global variables lie in the
Memory Organisation.

Wherever the system decides.
Is it in BSS or HEAP ?

Maybe. Probably not. It depends on the system.
I tried googling, i did not find any specific info w.r.t deciding
between bss or heap .

Since it varies from one system to the next, you need to choose
which system(s) you care about.
Could someone here clarify me regarding this ?

On most modern Unix-like systems, "global" variables are not in
*either* "heap" *or* "bss", except in some cases. Most of these
systems have sections or segments (the name varies) which may have
names like "text", "data", and "bss"; or ".text", ".rodata", ".data",
".sdata", ".bss", ".sbss"; and possibly things like "gnu.linkonce"
and ".init" and ".fini" and so on. In general, uninitialized
static-duration variables wind up in a "pre-zeroed" segment that
is called "bss", ".bss", ".sbss", or similar, while initialized
static-duration variables wind up in "data", ".data", ".rodata",
".sdata", or whatever.

But things do vary.
 
D

Default User

karthikbalaguru said:
Hi,

Memory allocated in heap remains until the end of the program.
So, global variables & static variables are allocated on heap.

If you're looking for a general answer, then stop. There isn't one.
Many implementations do things differently.

If you have a particular platform in mind, then find a newsgroup
dedicated to it and ask there.




Brian
 
K

karthikbalaguru

If you're looking for a general answer, then stop. There isn't one.
Many implementations do things differently.

If you have a particular platform in mind, then find a newsgroup
dedicated to it and ask there.

Brian

W.r.t this , there are 2 things that i am thinking of and that i have
looked for in internet.
1) Generic answer
2) w.r.t 'C' Compiler

Thx,
Karthik Balaguru
 
D

Default User

W.r.t this , there are 2 things that i am thinking of and that i have
looked for in internet.
1) Generic answer

As I said, there isn't one.
2) w.r.t 'C' Compiler

Which C compiler? When you have that answer, then you'll know where to
look. C compilers are only required to implement the requirements laid
out in the Standard. How they do this is up to them, and usually
dependent on the operating system.




Brian
 
F

Flash Gordon

karthikbalaguru wrote, On 21/08/07 21:54:
W.r.t this , there are 2 things that i am thinking of and that i have
looked for in internet.
1) Generic answer

As Brian said, there isn't one.
2) w.r.t 'C' Compiler

As Brian said, ask in a group for your platform.
 
K

Keith Thompson

Chris Torek said:
Unless it does not, e.g., when using any of the Unix-like systems'
malloc() implementations that use mmap() and munmap() or similar.
[...]

I'm probably misunderstanding you. Regardless of the underlying
implementation, memory allocated by malloc() (i.e., on the "heap") has
to remain available until it's deallocated by free() or realloc(), or
until the program ends, yes?
 
S

santosh

karthikbalaguru said:
W.r.t this , there are 2 things that i am thinking of and that i have
looked for in internet.
1) Generic answer

There is no generic answer to your question at all. You are asking about
object file formats and linkages, something that is _very_ system specific.
2) w.r.t 'C' Compiler

Huh? Which one? As I said, the answer is likely to vary from compiler to
compiler and system to system. You really have to look in a more specific
place. For UNIX systems, (including Linux), ask in
for Windows maybe

See the online version of the book Linkers and Loaders. It will answer many
of your system specific queries in as generic a manner as possible.
 
C

CBFalconer

karthikbalaguru said:
W.r.t this , there are 2 things that i am thinking of and that i
have looked for in internet.
1) Generic answer

Isn't any such. Move to a group dedicated to your peculiar system.
 
C

Chris Torek

I'm probably misunderstanding you. Regardless of the underlying
implementation, memory allocated by malloc() (i.e., on the "heap") has
to remain available until it's deallocated by free() or realloc(), or
until the program ends, yes?

Yes -- but "until free() or realloc()" is significant here. In
particular, people sometimes complain about "endlessly huge
processes" on Unix-like systems, when their code does something
like the following (assume appropriate #includes, etc.):

void startup_phase(void) {
char *mem = malloc(HUGE_NUMBER);
if (mem == NULL)
... /* handle failure to start up */

/* process is "big" here */
... do some work using large amounts of RAM ...

free(mem);
/* user wants process to become "small" here */
}

int main(void) {
startup_phase();
for (;;)
run_phase();
}

Such complaints are sometimes answered with "alas, the memory
remains allocated until the program ends" -- or, sometimes, with
"just use this malloc() variant, so that the memory is given back
to the OS by free()".

I think the OP ([email protected]) believed that only
the first case ("you're stuck") occurred. The second one ("use
this alternative library") does too, though.
 
K

karthikbalaguru

Unless it does not, e.g., when using any of the Unix-like systems'
malloc() implementations that use mmap() and munmap() or similar.


Unless they are not, as is usually the case.


Unless the system in question lacks the concept of a "BSS segment"
and/or does something fancier.


Wherever the system decides.


Maybe. Probably not. It depends on the system.


Since it varies from one system to the next, you need to choose
which system(s) you care about.


On most modern Unix-like systems, "global" variables are not in
*either* "heap" *or* "bss", except in some cases. Most of these
systems have sections or segments (the name varies) which may have
names like "text", "data", and "bss"; or ".text", ".rodata", ".data",
".sdata", ".bss", ".sbss"; and possibly things like "gnu.linkonce"
and ".init" and ".fini" and so on. In general, uninitialized
static-duration variables wind up in a "pre-zeroed" segment that
is called "bss", ".bss", ".sbss", or similar, while initialized
static-duration variables wind up in "data", ".data", ".rodata",
".sdata", or whatever.

But things do vary.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.

Thx for the info.
Actually, i have mixed up two infos w.r.t different systems.

Thx,
Karthik Balaguru
 
C

Chris Dollin

karthikbalaguru said:
Memory allocated in heap remains until the end of the program.

The Standard doesn't require that.
So, global variables & static variables are allocated on heap.

The Standard doesn't require that. It's also false in many implementations.
In the same time, I find that BSS also allows the placement of static
variables and global variables initialised to zero.

And that's one reason why.
So, while run-time, where does the static/global variables lie in the
Memory Organisation.
Is it in BSS or HEAP ?

Why do you care?

No, really. Why does it matter to you? Different answers are appropriate
in different cases.
I tried googling, i did not find any specific info w.r.t deciding
between bss or heap .
Could someone here clarify me regarding this ?

You typically don't need to know, and it's the implementations
responsibility to Get Thing Right.
 
R

Ravishankar S

karthikbalaguru said:
Hi,

Memory allocated in heap remains until the end of the program.
So, global variables & static variables are allocated on heap.
No. Its memory allocated for global and static variables remain till the end
of the program. The memory allocated in the "heap" by malloc will present
untile free'd by free(). So your assumption is wrong. So it is right to say
that global and static variables are never allocated to "heap" ? In most
cases yes. In some cases no, because those systems may not have a notion of
heap or notion of sections (like BSS etc).
In the same time, I find that BSS also allows the placement of static
variables and global variables initialised to zero.
That's right. On many Unix like systems, uninitialised globals and static
vars are allocated to the .bss section. But they as well be in a different
section. But what if the system does not have a notion of memory sections or
segments ??
So, while run-time, where does the static/global variables lie in the
Memory Organisation.
Is it in BSS or HEAP ?

In most cases BSS. But see answers above.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,231
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top