Heap & BSS

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • karthikbalaguru

    Heap & BSS

    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

  • santosh

    #2
    Re: Heap & BSS

    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>

    Comment

    • Chris Torek

      #3
      Re: Heap &amp; BSS

      In article <1187726923.629 830.142780@g4g2 000hsf.googlegr oups.com>
      karthikbalaguru <karthikbalagur u79@gmail.comwr ote:
      >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.linkon ce"
      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.

      Comment

      • Default User

        #4
        Re: Heap &amp; BSS

        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

        Comment

        • karthikbalaguru

          #5
          Re: Heap &amp; BSS

          On Aug 22, 1:48 am, "Default User" <defaultuse...@ yahoo.comwrote:
          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

          Comment

          • Default User

            #6
            Re: Heap &amp; BSS

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

            Comment

            • Flash Gordon

              #7
              Re: Heap &amp; BSS

              karthikbalaguru wrote, On 21/08/07 21:54:
              On Aug 22, 1:48 am, "Default User" <defaultuse...@ yahoo.comwrote:
              >karthikbalagur u 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

              Comment

              • Keith Thompson

                #8
                Re: Heap &amp; BSS

                Chris Torek <nospam@torek.n etwrites:
                In article <1187726923.629 830.142780@g4g2 000hsf.googlegr oups.com>
                karthikbalaguru <karthikbalagur u79@gmail.comwr ote:
                >>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 misunderstandin g 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_Keit h) kst-u@mib.org <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"

                Comment

                • santosh

                  #9
                  Re: Heap &amp; BSS

                  karthikbalaguru wrote:
                  On Aug 22, 1:48 am, "Default User" <defaultuse...@ yahoo.comwrote:
                  >karthikbalagur u 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.program mer.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

                  Comment

                  • Chris Torek

                    #10
                    Re: Heap &amp; BSS

                    >>In article <1187726923.629 830.142780@g4g2 000hsf.googlegr oups.com>
                    >>karthikbalagu ru <karthikbalagur u79@gmail.comwr ote:
                    >>>Memory allocated in heap remains until the end of the program.
                    >Chris Torek <nospam@torek.n etwrites [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 <lnps1gk0cm.fsf @nuthaus.mib.or g>,
                    Keith Thompson <kst-u@mib.orgwrote:
                    >I'm probably misunderstandin g 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(v oid) {
                    char *mem = malloc(HUGE_NUM BER);
                    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 (karthikbalagur u79@gmail.com) 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.

                    Comment

                    • CBFalconer

                      #11
                      Re: Heap &amp; BSS

                      karthikbalaguru wrote:
                      "Default User" <defaultuse...@ yahoo.comwrote:
                      >karthikbalagur u 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

                      Comment

                      • karthikbalaguru

                        #12
                        Re: Heap &amp; BSS

                        On Aug 22, 1:25 am, Chris Torek <nos...@torek.n etwrote:
                        In article <1187726923.629 830.142...@g4g2 000hsf.googlegr oups.com>
                        >
                        karthikbalaguru <karthikbalagur ...@gmail.comwr ote:
                        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.linkon ce"
                        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

                        Comment

                        • Chris Dollin

                          #13
                          Re: Heap &amp; BSS

                          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/

                          Comment

                          • Ravishankar S

                            #14
                            Re: Heap &amp; BSS


                            "karthikbalagur u" <karthikbalagur u79@gmail.comwr ote in message
                            news:1187726923 .629830.142780@ g4g2000hsf.goog legroups.com...
                            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 ?

                            Comment

                            Working...