static memory allocation versus dynamic memory allocation

Discussion in 'C Programming' started by Ken, Nov 26, 2006.

  1. Ken

    Ken Guest

    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.
    Ken, Nov 26, 2006
    #1
    1. Advertising

  2. Ken

    Tom St Denis Guest

    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
    Tom St Denis, Nov 26, 2006
    #2
    1. Advertising

  3. 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
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, Nov 26, 2006
    #3
  4. Ken

    santosh Guest

    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.
    santosh, Nov 26, 2006
    #4
  5. Ken

    Malcolm Guest

    "Richard Heathfield" <> wrote in message
    news:...
    > 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!).
    >

    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.
    --
    www.personal.leeds.ac.uk/~bgy1mm
    freeware games to download.
    Malcolm, Nov 26, 2006
    #5
  6. Ken

    rhle.freak Guest

    On Nov 26, 10:48 am, "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.
    >
    > 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
    rhle.freak, Nov 26, 2006
    #6
  7. Ken

    CBFalconer Guest

    "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 incomprehensibles
    as u and n?

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>
    CBFalconer, Nov 26, 2006
    #7
  8. Ken

    Malcolm Guest

    "CBFalconer" <> wrote in message
    news:...
    > "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 incomprehensibles
    > as u and n?
    >

    Or nonsense words like "int", "float" and "char" for
    "integer", "real number", and "character"?
    --
    www.personal.leeds.ac.uk/~bgy1mm
    freeware games to download.
    Malcolm, Nov 26, 2006
    #8
  9. "Malcolm" <> writes:

    > "CBFalconer" <> wrote in message
    > news:...
    >> "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 incomprehensibles
    >> 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.
    Ben Bacarisse, Nov 26, 2006
    #9
  10. Ken

    santosh Guest

    Ben Bacarisse wrote:
    > "Malcolm" <> writes:
    >
    > > "CBFalconer" <> wrote in message
    > > news:...
    > >> "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 incomprehensibles
    > >> 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.
    santosh, Nov 27, 2006
    #10
  11. "santosh" <> writes:

    > Ben Bacarisse wrote:
    >> "Malcolm" <> writes:
    >>
    >> > "CBFalconer" <> wrote in message
    >> > news:...

    <original snipped>
    >> >> You appear to be fairly capable of using English. Why spoil your
    >> >> articles and make them hard to read by using such incomprehensibles
    >> >> 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.
    Ben Bacarisse, Nov 27, 2006
    #11
  12. Ken

    Malcolm Guest

    "Ben Bacarisse" <> wrote in message
    news:...
    > "santosh" <> writes:
    >
    >> Ben Bacarisse wrote:
    >>> "Malcolm" <> writes:
    >>>
    >>> > "CBFalconer" <> wrote in message
    >>> > news:...

    > <original snipped>
    >>> >> You appear to be fairly capable of using English. Why spoil your
    >>> >> articles and make them hard to read by using such incomprehensibles
    >>> >> 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.
    >

    If you attack "u" for "you" on what basis do you defend "int" for
    "integer?".
    --
    www.personal.leeds.ac.uk/~bgy1mm
    freeware games to download.
    Malcolm, Nov 28, 2006
    #12
  13. Ken

    Ben Pfaff Guest

    "Malcolm" <> writes:

    > 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
    Ben Pfaff, Nov 28, 2006
    #13
  14. Ben Pfaff <> writes:
    > "Malcolm" <> writes:
    >
    >> 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_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.
    Keith Thompson, Nov 28, 2006
    #14
  15. Ken

    Ben Pfaff Guest

    Keith Thompson <> writes:

    > Ben Pfaff <> writes:
    >> "Malcolm" <> writes:
    >>
    >>> 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[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
    );while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p\
    );}return 0;}
    Ben Pfaff, Nov 28, 2006
    #15
  16. Ben Pfaff <> writes:
    > Keith Thompson <> writes:
    >> Ben Pfaff <> writes:
    >>> "Malcolm" <> writes:
    >>>
    >>>> 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.


    Of course, I agree completely. I was merely quibbling about the way
    you expressed the point. We do that here, you know. :cool:}

    --
    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.
    Keith Thompson, Nov 29, 2006
    #16
  17. On Wed, 29 Nov 2006 00:01:59 GMT, Keith Thompson <> wrote:

    >Ben Pfaff <> writes:
    >> Keith Thompson <> writes:
    >>> Ben Pfaff <> writes:
    >>>> "Malcolm" <> writes:
    >>>>
    >>>>> 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.

    >
    >Of course, I agree completely. I was merely quibbling about the way
    >you expressed the point. We do that here, you know. :cool:}


    Oh well, as well as we are going to quibble, the "both" should refer to
    "u" and "int", the issue being parallelism of usage. OTOH, this being
    comp.lang.c the governing rule is that if it can be misread, then it
    should be. :)
    Richard Harter, Nov 29, 2006
    #17
  18. "Malcolm" <> writes:

    > "Ben Bacarisse" <> wrote in message
    > news:...
    >> "santosh" <> writes:
    >>
    >>> Ben Bacarisse wrote:
    >>>> "Malcolm" <> writes:
    >>>>
    >>>> > "CBFalconer" <> wrote in message
    >>>> > news:...

    >> <original snipped>
    >>>> >> You appear to be fairly capable of using English. Why spoil your
    >>>> >> articles and make them hard to read by using such incomprehensibles
    >>>> >> 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.
    >>

    > If you attack "u" for "you" on what basis do you defend "int" for
    > "integer?".


    On the basis that it is the keyword for an important type in the
    language discussed here. Can you discuss C without using the keywords
    that designate the core types of the language?

    I though I had misunderstood you comment but it seem I had not.

    (BTW, I don't attack using "u" for "you". I find it does not impede
    my understanding and it probably gives useful "out of band" data about
    the poster.)

    --
    Ben.
    Ben Bacarisse, Nov 29, 2006
    #18
  19. Ken

    CBFalconer Guest

    Keith Thompson wrote:
    > Ben Pfaff <> writes:
    >

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

    >
    > Of course, I agree completely. I was merely quibbling about the
    > way you expressed the point. We do that here, you know. :cool:}


    Quibble? Here? I am shocked, shocked I tell you.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>
    CBFalconer, Nov 29, 2006
    #19
  20. CBFalconer <> writes:
    > Keith Thompson wrote:
    >> Ben Pfaff <> writes:
    >>

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

    >>
    >> Of course, I agree completely. I was merely quibbling about the
    >> way you expressed the point. We do that here, you know. :cool:}

    >
    > Quibble? Here? I am shocked, shocked I tell you.


    "Your winnings, sir."

    --
    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.
    Keith Thompson, Nov 29, 2006
    #20
    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. Ravi Shankar Nair

    Static Versus Non Static

    Ravi Shankar Nair, Sep 14, 2006, in forum: Java
    Replies:
    9
    Views:
    5,715
    Chris Uppal
    Sep 18, 2006
  2. chris
    Replies:
    6
    Views:
    974
    chris
    Oct 28, 2005
  3. Replies:
    7
    Views:
    786
    cr88192
    May 9, 2008
  4. Shivanand Kadwadkar

    Dynamic memory allocation with static pointer variable

    Shivanand Kadwadkar, Dec 25, 2010, in forum: C Programming
    Replies:
    7
    Views:
    2,509
    Seebs
    Jan 4, 2011
  5. Paul Butcher
    Replies:
    12
    Views:
    693
    Gary Wright
    Nov 28, 2007
Loading...

Share This Page