Heap & BSS

Discussion in 'C Programming' started by karthikbalaguru, Aug 21, 2007.

  1. 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
     
    karthikbalaguru, Aug 21, 2007
    #1
    1. Advertising

  2. karthikbalaguru

    santosh Guest

    karthikbalaguru wrote:

    > 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>
     
    santosh, Aug 21, 2007
    #2
    1. Advertising

  3. karthikbalaguru

    Chris Torek Guest

    In article <>
    karthikbalaguru <> wrote:
    >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.
    --
    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.
     
    Chris Torek, Aug 21, 2007
    #3
  4. karthikbalaguru

    Default User Guest

    karthikbalaguru wrote:

    > 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
     
    Default User, Aug 21, 2007
    #4
  5. On Aug 22, 1:48 am, "Default User" <> wrote:
    > karthikbalaguru wrote:
    > > 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


    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
     
    karthikbalaguru, Aug 21, 2007
    #5
  6. karthikbalaguru

    Default User Guest

    karthikbalaguru wrote:

    > On Aug 22, 1:48 am, "Default User" <> wrote:
    > > karthikbalaguru wrote:
    > > > 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.


    > 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
     
    Default User, Aug 21, 2007
    #6
  7. karthikbalaguru

    Flash Gordon Guest

    karthikbalaguru wrote, On 21/08/07 21:54:
    > On Aug 22, 1:48 am, "Default User" <> wrote:
    >> karthikbalaguru wrote:
    >>> 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

    >
    > 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.
    --
    Flash Gordon
     
    Flash Gordon, Aug 21, 2007
    #7
  8. Chris Torek <> writes:
    > In article <>
    > karthikbalaguru <> wrote:
    >>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.

    [...]

    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?

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Aug 21, 2007
    #8
  9. karthikbalaguru

    santosh Guest

    karthikbalaguru wrote:

    > On Aug 22, 1:48 am, "Default User" <> wrote:
    >> karthikbalaguru wrote:
    >> > 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

    >
    > 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
    news:comp.unix.programmer, for Windows maybe
    news:comp.os.ms-windows.programmer.win32.

    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.

    >
    > Thx,
    > Karthik Balaguru
     
    santosh, Aug 21, 2007
    #9
  10. karthikbalaguru

    CBFalconer Guest

    karthikbalaguru wrote:
    > "Default User" <> wrote:
    >> karthikbalaguru wrote:
    >>
    >>> 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.

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

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Aug 22, 2007
    #10
  11. karthikbalaguru

    Chris Torek Guest

    >>In article <>
    >>karthikbalaguru <> wrote:
    >>>Memory allocated in heap remains until the end of the program.


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


    In article <>,
    Keith Thompson <> wrote:
    >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 () believed that only
    the first case ("you're stuck") occurred. The second one ("use
    this alternative library") does too, though.
    --
    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.
     
    Chris Torek, Aug 22, 2007
    #11
  12. On Aug 22, 1:25 am, Chris Torek <> wrote:
    > In article <>
    >
    > karthikbalaguru <> wrote:
    > >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.
    > --
    > 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
     
    karthikbalaguru, Aug 22, 2007
    #12
  13. karthikbalaguru

    Chris Dollin Guest

    karthikbalaguru wrote:

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

    --
    Eel Hedgehog
    "No-one here is exactly what he appears." G'kar, /Babylon 5/
     
    Chris Dollin, Aug 22, 2007
    #13
  14. "karthikbalaguru" <> wrote in message
    news:...
    > 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.
    >
    > 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 ?
     
    Ravishankar S, Aug 23, 2007
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. termiflyer
    Replies:
    4
    Views:
    655
    S.Tobias
    Oct 10, 2005
  2. onkar

    variable allocated from stack/bss ??

    onkar, Nov 29, 2006, in forum: C Programming
    Replies:
    148
    Views:
    2,356
    Dave Thompson
    Dec 18, 2006
  3. Replies:
    4
    Views:
    473
    Keith Thompson
    May 23, 2007
  4. text,data and bss

    , Nov 16, 2008, in forum: C Programming
    Replies:
    27
    Views:
    7,227
  5. Francis Moreau

    Is my global part of BSS ?

    Francis Moreau, Jan 30, 2009, in forum: C Programming
    Replies:
    2
    Views:
    299
    Kaz Kylheku
    Jan 31, 2009
Loading...

Share This Page