Dynamic memory allocation for array(ex: int a[size]) with C99 and older

Discussion in 'C Programming' started by Shivanand Kadwadkar, Dec 24, 2010.

  1. Before C99 we cant decide the size of the array in runtime because
    since they are allocated in stack(not the global variable) we need the
    size of the array at compile time so we can allocate the stack frame
    of particular size.

    But as i heard C99 removes this restriction, is anyone please tell how
    in this case stack frame size is determined? can we decide stack frame
    size at runtime ?

    With C99 is that array is stored in stack or some other place ?
    Shivanand Kadwadkar, Dec 24, 2010
    #1
    1. Advertising

  2. Shivanand Kadwadkar

    Tim Prince Guest

    Re: Dynamic memory allocation for array(ex: int a[size]) with C99and older

    On 12/23/2010 9:35 PM, Shivanand Kadwadkar wrote:
    > Before C99 we cant decide the size of the array in runtime because
    > since they are allocated in stack(not the global variable) we need the
    > size of the array at compile time so we can allocate the stack frame
    > of particular size.
    >
    > But as i heard C99 removes this restriction, is anyone please tell how
    > in this case stack frame size is determined? can we decide stack frame
    > size at runtime ?
    >
    > With C99 is that array is stored in stack or some other place ?

    VLA typically goes on stack, but that's an implementation detail not
    specified by standard.
    http://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html documents a
    typical implementation.
    Beyond the standard objections, that there is no reliable way to check
    success of the allocation, and the incompatibility with C++, VLA
    implementation has been flaky in several compilers. It's surprising
    that it is described as "more elegant."

    --
    Tim Prince
    Tim Prince, Dec 24, 2010
    #2
    1. Advertising

  3. Shivanand Kadwadkar

    Seebs Guest

    Re: Dynamic memory allocation for array(ex: int a[size]) with C99and older

    On 2010-12-24, Shivanand Kadwadkar <> wrote:
    > Before C99 we cant decide the size of the array in runtime because
    > since they are allocated in stack(not the global variable) we need the
    > size of the array at compile time so we can allocate the stack frame
    > of particular size.


    Wrong. You can't decide the size of an array at runtime because the
    specification states that array sizes must be integer constant expressions.

    > But as i heard C99 removes this restriction, is anyone please tell how
    > in this case stack frame size is determined? can we decide stack frame
    > size at runtime ?
    >
    > With C99 is that array is stored in stack or some other place ?


    Your question is wrong.

    With C99, the compiler must arrange for the array to be accessible during
    its lifetime.

    We don't know whether that means that the compiler is adjusting the size
    of a "stack frame", or doing dynamic allocation with a callback to free
    stuff that's hit as part of a function epilogue, or what. It's not
    relevant. You're trying to ask how "C99" does something, but C99 doesn't
    do anything, individual compilers do, and *not necessarily all the same way*.

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    I am not speaking for my employer, although they do rent some of my opinions.
    Seebs, Dec 24, 2010
    #3
  4. Shivanand Kadwadkar

    Nobody Guest

    On Thu, 23 Dec 2010 21:35:15 -0800, Shivanand Kadwadkar wrote:

    > Before C99 we cant decide the size of the array in runtime because
    > since they are allocated in stack(not the global variable) we need the
    > size of the array at compile time so we can allocate the stack frame
    > of particular size.
    >
    > But as i heard C99 removes this restriction, is anyone please tell how
    > in this case stack frame size is determined? can we decide stack frame
    > size at runtime ?
    >
    > With C99 is that array is stored in stack or some other place ?


    Typically the stack. Even before C99, many platforms provided equivalent
    functionality via alloca().
    Nobody, Dec 24, 2010
    #4
  5. Re: Dynamic memory allocation for array(ex: int a[size]) with C99and older

    Shivanand Kadwadkar wrote:
    > Before C99 we cant decide the size of the array in runtime because
    > since they are allocated in stack(not the global variable) we need the
    > size of the array at compile time so we can allocate the stack frame
    > of particular size.


    That's wrong. Before C99 we can't decide the size of the array at
    runtime because the definition of the C language requires that it be
    decided at compile time.

    > But as i heard C99 removes this restriction, is anyone please tell how
    > in this case stack frame size is determined?


    The same way as ever, add up the sizes of whatever needs to be in it and
    make whatever adjustments are needed.

    > can we decide stack frame size at runtime ?


    Yes, why not?

    > With C99 is that array is stored in stack or some other place ?


    Yes, just the same as in all previous versions of C, the array is stored
    in the stack or some other place - it's stored wherever the compiler
    chooses to store it. Different compilers (or different options to the
    same compiler) can choose different places.
    J. J. Farrell, Dec 28, 2010
    #5
    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. Schnoffos
    Replies:
    2
    Views:
    1,198
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,615
    Old Wolf
    Jan 20, 2004
  3. Ken
    Replies:
    24
    Views:
    3,839
    Ben Bacarisse
    Nov 30, 2006
  4. chris
    Replies:
    6
    Views:
    974
    chris
    Oct 28, 2005
  5. Replies:
    3
    Views:
    584
    Keith Thompson
    Mar 31, 2007
Loading...

Share This Page