Re: On VLAs and incomplete types
- From: user923005 <dcorbit@xxxxxxxxx>
- Date: Fri, 21 Mar 2008 12:24:53 -0700 (PDT)
On Mar 21, 11:55 am, Sensei <Sensei's e-mail is at Mac-dot-com> wrote:
On 2008-03-21 16:19:10 +0100, Ben Bacarisse <ben.use...@xxxxxxxxx> said:
Is x allocated on the
fly as we enter the code block (somehow, on the heap or by any other
non C-related means)
Well put. How or where does not matter. It is likely to allocated
from a stack ("the" stack if the compiler is using only one).
and then subsequently freed when exit?
The way I used to put it was: the storage lasts until the program flow
passes the end of the variable's scope (unless the program exits before
that). It is surprisingly hard to get the words exactly right, but
most people find the intent more natural than the wording used to
explain it!
Then what
are the advantages (not counting the "syntactic sugar" I can think of)
of having VLAs into the standard?
It allows you to have arrays of variable size, efficiently allocated.
malloc and free will usually be slower and you need to do the freeing.
The real advantage, though, comes with code like your 2D array
parameter called C. Such things are simpler with VLA parameters.
Well, I think you clarified a lot. Thanks!
One last question, you are saying that VLAs are "usually" faster than
the malloc/free counterpart, although arrays are passed by means of
pointers. How can it be so, if the array lives as long as the code
block it refers to is executing? I mean, the code will effectively
allocate and deallocate memory, so there must be an explanation for
being faster. Is heap and stack actually different in this?
As for 2D arrays, I'm using a simple array with appropriate indexing,
is there a performance reason for preferring a variable length array?
Automatic variables are typically placed on a *cough* stack. The
generation of these variables is really just a simple subtraction.
The generation of dynamic memory is more complicated and requires
functions to track the allocations carefully.
The speed of access will be similar between automatic and dynamically
allocated memory. It is the book keeping of generation and disposal
that differ significantly.
So why not just use automatic memory all the time? It is a very
limited resource, and when it fails, it does not fail gracefully like
malloc() which will tell you that something went wrong by returning an
NULL pointer. If an automatic allocation fails you can expect a core
dump or some sort of undefined behavior.
Thanks for bearing with me :)
.
- Follow-Ups:
- Re: On VLAs and incomplete types
- From: Sensei
- Re: On VLAs and incomplete types
- References:
- On VLAs and incomplete types
- From: Sensei
- Re: On VLAs and incomplete types
- From: Ben Bacarisse
- Re: On VLAs and incomplete types
- From: Sensei
- On VLAs and incomplete types
- Prev by Date: Re: Keep getting a warning.. Can someone please tell me why?
- Next by Date: Re: I need help please!
- Previous by thread: Re: On VLAs and incomplete types
- Next by thread: Re: On VLAs and incomplete types
- Index(es):
Relevant Pages
|
|