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

  • Thread starter Shivanand Kadwadkar
  • Start date

S

Shivanand Kadwadkar

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 ?
 
Ad

Advertisements

T

Tim Prince

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

Seebs

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
 
N

Nobody

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().
 
Ad

Advertisements

J

J. J. Farrell

Shivanand said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top