static memory allocation versus dynamic memory allocation

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

    #1

    static memory allocation versus dynamic memory allocation

    In C programming, I want to know in what situations we should use
    static memory allocation instead of dynamic memory allocation. My
    understanding is that static memory allocation like using array is
    faster than malloc, but dynamic memory allocation is more flexible.

    Please comment... thanks.

  • Richard Heathfield

    #2
    Re: static memory allocation versus dynamic memory allocation

    Ken said:
    In C programming, I want to know in what situations we should use
    static memory allocation instead of dynamic memory allocation. My
    understanding is that static memory allocation like using array is
    faster than malloc, but dynamic memory allocation is more flexible.
    That's about right. Broadly, if you know in advance how much memory you
    need, you don't need to use malloc (unless your implementation has a very
    low limit on the amount of memory available for auto objects, but a less
    restrictive limit on free store).

    But malloc & co give you the flexibility to grow your data structures only
    as large as they need to be, and (most importantly) the decision is
    deferred until runtime (which is when you actually find out how much data
    you've got!).

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999

    email: rjh at the above domain, - www.

    Comment

    • Tom St Denis

      #3
      Re: static memory allocation versus dynamic memory allocation


      Ken wrote:
      In C programming, I want to know in what situations we should use
      static memory allocation instead of dynamic memory allocation. My
      understanding is that static memory allocation like using array is
      faster than malloc, but dynamic memory allocation is more flexible.
      Ho, Ho, Ho, Hold the homework!

      Tom

      Comment

      • santosh

        #4
        Re: static memory allocation versus dynamic memory allocation

        Ken wrote:
        In C programming, I want to know in what situations we should use
        static memory allocation instead of dynamic memory allocation. My
        understanding is that static memory allocation like using array is
        faster than malloc, but dynamic memory allocation is more flexible.
        Use static allocation when you know in advance the amount of memory
        needed, or if dynamic allocation is not available, (as may happen in
        some embedded systems). Use of auto variables, (a form of static
        allocation, though in reality it may not be), also helps in data
        encapsulation and automatic memory management.

        Dynamic memory is particularly relevant when you anticipate growth in
        your data structures, where you may only know the maximum needed
        memory, but actual usage may be far less than that, and when you need
        for the data to persist throughout the process's lifetime but you
        don't, (rightly), want to use global memory for the purpose.

        Comment

        • Malcolm

          #5
          Re: static memory allocation versus dynamic memory allocation




          "Richard Heathfield" <rjh@see.sig.in validwrote in message
          news:2vqdnTKWha BksfTYRVnytA@bt .com...
          Ken said:
          >
          >In C programming, I want to know in what situations we should use
          >static memory allocation instead of dynamic memory allocation. My
          >understandin g is that static memory allocation like using array is
          >faster than malloc, but dynamic memory allocation is more flexible.
          >
          That's about right. Broadly, if you know in advance how much memory you
          need, you don't need to use malloc (unless your implementation has a very
          low limit on the amount of memory available for auto objects, but a less
          restrictive limit on free store).
          >
          But malloc & co give you the flexibility to grow your data structures only
          as large as they need to be, and (most importantly) the decision is
          deferred until runtime (which is when you actually find out how much data
          you've got!).
          >
          The other factor is that malloc() makes modules much more resuable.
          Often you will know the size of the object at complie time, but not when you
          write the code that handles arbitrary objects. For instance, a hash table
          could hold any number of objects of any size, even though you know that you
          need at most 1000 of type CAR.
          --

          freeware games to download.


          Comment

          • rhle.freak

            #6
            Re: static memory allocation versus dynamic memory allocation



            On Nov 26, 10:48 am, "Ken" <java...@gmail. comwrote:
            In C programming, I want to know in what situations we should use
            static memory allocation instead of dynamic memory allocation. My
            understanding is that static memory allocation like using array is
            faster than malloc, but dynamic memory allocation is more flexible.
            >
            Please comment... thanks.
            Static Memory Allocation is used when u know the memory requirement in
            advance ;memory is assigned by the compiler at runtime . The space is
            allocated once, when your program is started (part of the exec
            operation), and is never freed.
            Static allocation is what happens when you declare a static or global
            variable. Each static or global variable defines one block of space, of
            a fixed size.

            Dynamic Memory Allocation is used when the memory you need, or how
            long you continue to need it, depends on factors that are not known
            before the program runs or if u working with a dynamic data structure
            ..Memory is allocated at runtime rather than during compilation n u can
            free a block of memory once u no longer need it during the execution of
            the program.

            further Dynamic allocation is not supported by C variables; there is no
            storage class "dynamic", and there can never be a C variable whose
            value is stored in dynamically allocated space. The only way to get
            dynamically allocated memory is via a system call and the only way to
            refer to dynamically allocated space is through a pointer.
            the actual process of dynamic allocation requires more computation time
            n hence its slower than static memory allocation

            Comment

            • CBFalconer

              #7
              Re: static memory allocation versus dynamic memory allocation

              "rhle.freak " wrote:
              >
              .... snip ...
              >
              Dynamic Memory Allocation is used when the memory you need, or
              how long you continue to need it, depends on factors that are not
              known before the program runs or if u working with a dynamic data
              structure .Memory is allocated at runtime rather than during
              compilation n u can free a block of memory once u no longer need
              it during the execution of the program.
              You appear to be fairly capable of using English. Why spoil your
              articles and make them hard to read by using such incomprehensibl es
              as u and n?

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

              Comment

              • Malcolm

                #8
                Re: static memory allocation versus dynamic memory allocation




                "CBFalconer " <cbfalconer@yah oo.comwrote in message
                news:456993E8.6 E2B8086@yahoo.c om...
                "rhle.freak " wrote:
                >>
                ... snip ...
                >>
                >Dynamic Memory Allocation is used when the memory you need, or
                >how long you continue to need it, depends on factors that are not
                >known before the program runs or if u working with a dynamic data
                >structure .Memory is allocated at runtime rather than during
                >compilation n u can free a block of memory once u no longer need
                >it during the execution of the program.
                >
                You appear to be fairly capable of using English. Why spoil your
                articles and make them hard to read by using such incomprehensibl es
                as u and n?
                >
                Or nonsense words like "int", "float" and "char" for
                "integer", "real number", and "character" ?
                --

                freeware games to download.


                Comment

                • Ben Bacarisse

                  #9
                  Re: static memory allocation versus dynamic memory allocation

                  "Malcolm" <regniztar@btin ternet.comwrite s:
                  "CBFalconer " <cbfalconer@yah oo.comwrote in message
                  news:456993E8.6 E2B8086@yahoo.c om...
                  >"rhle.freak " wrote:
                  >>>
                  >... snip ...
                  >>>
                  >>Dynamic Memory Allocation is used when the memory you need, or
                  >>how long you continue to need it, depends on factors that are not
                  >>known before the program runs or if u working with a dynamic data
                  >>structure .Memory is allocated at runtime rather than during
                  >>compilation n u can free a block of memory once u no longer need
                  >>it during the execution of the program.
                  >>
                  >You appear to be fairly capable of using English. Why spoil your
                  >articles and make them hard to read by using such incomprehensibl es
                  >as u and n?
                  >>
                  Or nonsense words like "int", "float" and "char" for
                  "integer", "real number", and "character" ?
                  Why would you even consider removing such important distinctions in a
                  C new group? Did you forget the smiley?

                  --
                  Ben.

                  Comment

                  • santosh

                    #10
                    Re: static memory allocation versus dynamic memory allocation

                    Ben Bacarisse wrote:
                    "Malcolm" <regniztar@btin ternet.comwrite s:
                    >
                    "CBFalconer " <cbfalconer@yah oo.comwrote in message
                    news:456993E8.6 E2B8086@yahoo.c om...
                    "rhle.freak " wrote:
                    >>
                    ... snip ...
                    >>
                    >Dynamic Memory Allocation is used when the memory you need, or
                    >how long you continue to need it, depends on factors that are not
                    >known before the program runs or if u working with a dynamic data
                    >structure .Memory is allocated at runtime rather than during
                    >compilation n u can free a block of memory once u no longer need
                    >it during the execution of the program.
                    >
                    You appear to be fairly capable of using English. Why spoil your
                    articles and make them hard to read by using such incomprehensibl es
                    as u and n?
                    >
                    Or nonsense words like "int", "float" and "char" for
                    "integer", "real number", and "character" ?
                    >
                    Why would you even consider removing such important distinctions in a
                    C new group? Did you forget the smiley?
                    No distinction is removed. He's probably just pointing out that full
                    names like 'integer' would have been a better design choice than terse
                    abbreviations like int, float etc. Too late now and anyway, Pascal is
                    always available.

                    Comment

                    • Ben Bacarisse

                      #11
                      Re: static memory allocation versus dynamic memory allocation

                      "santosh" <santosh.k83@gm ail.comwrites:
                      Ben Bacarisse wrote:
                      >"Malcolm" <regniztar@btin ternet.comwrite s:
                      >>
                      "CBFalconer " <cbfalconer@yah oo.comwrote in message
                      news:456993E8.6 E2B8086@yahoo.c om...
                      <original snipped>
                      >You appear to be fairly capable of using English. Why spoil your
                      >articles and make them hard to read by using such incomprehensibl es
                      >as u and n?
                      >>
                      Or nonsense words like "int", "float" and "char" for
                      "integer", "real number", and "character" ?
                      >>
                      >Why would you even consider removing such important distinctions in a
                      >C new group? Did you forget the smiley?
                      >
                      No distinction is removed. He's probably just pointing out that full
                      names like 'integer' would have been a better design choice than terse
                      abbreviations like int, float etc. Too late now and anyway, Pascal is
                      always available.
                      Oh, OK. Because the reply was to a comment about *posting* style
                      (using u and n) I though Malcolm was suggesting is was equally silly
                      to use words like int and char in posting in this group! Not to self:
                      sleep more, post less.

                      --
                      Ben.

                      Comment

                      • Malcolm

                        #12
                        Re: static memory allocation versus dynamic memory allocation




                        "Ben Bacarisse" <ben.usenet@bsb .me.ukwrote in message
                        news:87psb9avba .fsf@bsb.me.uk. ..
                        "santosh" <santosh.k83@gm ail.comwrites:
                        >
                        >Ben Bacarisse wrote:
                        >>"Malcolm" <regniztar@btin ternet.comwrite s:
                        >>>
                        >"CBFalconer " <cbfalconer@yah oo.comwrote in message
                        >news:456993E8. 6E2B8086@yahoo. com...
                        <original snipped>
                        >>You appear to be fairly capable of using English. Why spoil your
                        >>articles and make them hard to read by using such incomprehensibl es
                        >>as u and n?
                        >>>
                        >Or nonsense words like "int", "float" and "char" for
                        >"integer", "real number", and "character" ?
                        >>>
                        >>Why would you even consider removing such important distinctions in a
                        >>C new group? Did you forget the smiley?
                        >>
                        >No distinction is removed. He's probably just pointing out that full
                        >names like 'integer' would have been a better design choice than terse
                        >abbreviation s like int, float etc. Too late now and anyway, Pascal is
                        >always available.
                        >
                        Oh, OK. Because the reply was to a comment about *posting* style
                        (using u and n) I though Malcolm was suggesting is was equally silly
                        to use words like int and char in posting in this group! Not to self:
                        sleep more, post less.
                        >
                        If you attack "u" for "you" on what basis do you defend "int" for
                        "integer?".
                        --

                        freeware games to download.


                        Comment

                        • Ben Pfaff

                          #13
                          Re: static memory allocation versus dynamic memory allocation

                          "Malcolm" <regniztar@btin ternet.comwrite s:
                          If you attack "u" for "you" on what basis do you defend "int" for
                          "integer?".
                          Which one of them is defined in the C standard?
                          --
                          "You call this a *C* question? What the hell are you smoking?" --Kaz

                          Comment

                          • Keith Thompson

                            #14
                            Re: static memory allocation versus dynamic memory allocation

                            Ben Pfaff <blp@cs.stanfor d.eduwrites:
                            "Malcolm" <regniztar@btin ternet.comwrite s:
                            >
                            >If you attack "u" for "you" on what basis do you defend "int" for
                            >"integer?".
                            >
                            Which one of them is defined in the C standard?
                            Both are. There are several integer types; "int" is one of them. See
                            C99 6.2.5.

                            Malcolm appears to be of the opinion that there should be one and
                            only one integer type. He is, as far as I can tell, in a minority of
                            one in this regard.

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

                            Comment

                            • Ben Pfaff

                              #15
                              Re: static memory allocation versus dynamic memory allocation

                              Keith Thompson <kst-u@mib.orgwrites :
                              Ben Pfaff <blp@cs.stanfor d.eduwrites:
                              >"Malcolm" <regniztar@btin ternet.comwrite s:
                              >>
                              >>If you attack "u" for "you" on what basis do you defend "int" for
                              >>"integer?".
                              >>
                              >Which one of them is defined in the C standard?
                              >
                              Both are. There are several integer types; "int" is one of them. See
                              C99 6.2.5.
                              My point is that whereas "u" is *not* defined in the C standard,
                              "int" *is*, so it is a proper term for use in comp.lang.c.
                              --
                              int main(void){char p[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
                              \n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
                              );while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof p-1;putchar(p[i]\
                              );}return 0;}

                              Comment

                              Working...