C99 dynamic array

K

kwikius

Does anyone know what a C99 dynamic array is, and if it will be
useable in C++?

regards
Andy Little
 
A

Alf P. Steinbach

* kwikius:
Does anyone know what a C99 dynamic array is,

It is an array with size determined at run-time.

and if it will be
useable in C++?

No, but it's supported by g++.

C++ unfortunately lacks syntax for that, but you can do very primitive
like things via non-standard library functions such as _alloca.


Cheers, & hth.,

- Alf
 
J

Jeff Schwab

kwikius said:
Does anyone know what a C99 dynamic array is

A variable-length array, i.e. an array whose size can be set by a
run-time variable.

<C>
#include <stdlib.h>

void f(size_t z) {

typedef enum some_t { some_value } some_type;

some_type a[5] = { some_value }; // always ok
// some_type b[z] = { some_value }; // always illegal
some_type c[z]; // ok in c99 only
}
, and if it will be useable in C++?

Not AFAIK, although C++0x will incorporate some features of C99. The
std::vector is preferable in most respects, anyway. The down-side of
vector is that it uses dynamic storage, which may incur more performance
overhead than automatic (stack-based) storage. Chances are excellent
that whatever you're doing, std::vector is a better choice than a raw array.
 
K

kwikius

kwikius said:
Does anyone know what a C99 dynamic array is

A variable-length array, i.e. an array whose size can be set by a
run-time variable.

<C>
#include <stdlib.h>

void f(size_t z) {

        typedef enum some_t { some_value } some_type;

        some_type a[5] = { some_value }; // always ok
//      some_type b[z] = { some_value }; // always illegal
        some_type c[z]; // ok in c99 only}

</C>

The std::vector is preferable in most respects, anyway.  The down-side of
vector is that it uses dynamic storage, which may incur more performance
overhead than automatic (stack-based) storage.

Ah ha yes, but does the dynamic array actually just look like a
std::vector underneath (use heap allocation) or is there some other
magic.

Its a sort of holy grail isnt it, that you can create a dynamically
sized array without the heap, so if C99 dynamic array does use another
method to allocate, would be interesting.

(my guess is that it woud have same performance as std::vector. There
is no "magic")

regards
Andy Little
 
J

Jeff Schwab

kwikius said:
kwikius said:
Does anyone know what a C99 dynamic array is
A variable-length array, i.e. an array whose size can be set by a
run-time variable.

<C>
#include <stdlib.h>

void f(size_t z) {

typedef enum some_t { some_value } some_type;

some_type a[5] = { some_value }; // always ok
// some_type b[z] = { some_value }; // always illegal
some_type c[z]; // ok in c99 only}

</C>

The std::vector is preferable in most respects, anyway. The down-side of
vector is that it uses dynamic storage, which may incur more performance
overhead than automatic (stack-based) storage.

Ah ha yes, but does the dynamic array actually just look like a
std::vector underneath (use heap allocation) or is there some other
magic.

Its a sort of holy grail isnt it, that you can create a dynamically
sized array without the heap, so if C99 dynamic array does use another
method to allocate, would be interesting.

(my guess is that it woud have same performance as std::vector. There
is no "magic")

I believe VLAs use the stack, just like traditional arrays. The "magic"
is that the compiler doesn't know a priori how big the function's stack
frame will be; in fact, multiple invocations of the same function might
need frames of different sizes. It would be interesting to know what
kinds of complications this creates for compiler implementers, and how
those problems have been worked around.
 
E

Erik Wikström

kwikius said:
kwikius wrote:
Does anyone know what a C99 dynamic array is
A variable-length array, i.e. an array whose size can be set by a
run-time variable.

<C>
#include <stdlib.h>

void f(size_t z) {

typedef enum some_t { some_value } some_type;

some_type a[5] = { some_value }; // always ok
// some_type b[z] = { some_value }; // always illegal
some_type c[z]; // ok in c99 only}

</C>

The std::vector is preferable in most respects, anyway. The down-side of
vector is that it uses dynamic storage, which may incur more performance
overhead than automatic (stack-based) storage.

Ah ha yes, but does the dynamic array actually just look like a
std::vector underneath (use heap allocation) or is there some other
magic.

Its a sort of holy grail isnt it, that you can create a dynamically
sized array without the heap, so if C99 dynamic array does use another
method to allocate, would be interesting.

(my guess is that it woud have same performance as std::vector. There
is no "magic")

I believe VLAs use the stack, just like traditional arrays. The "magic"
is that the compiler doesn't know a priori how big the function's stack
frame will be; in fact, multiple invocations of the same function might
need frames of different sizes. It would be interesting to know what
kinds of complications this creates for compiler implementers, and how
those problems have been worked around.

There should be little trouble to create the actual frame, nor to use
VLAs, you just have to remember the size of each VLA and use it to
calculate the offsets. Which means that some operations (probably all
that accesses elements in an VLA) will require one or more additional
computations to calculate the offset from the stack-pointer.
 
J

Jeff Schwab

Erik said:
kwikius said:
kwikius wrote:
Does anyone know what a C99 dynamic array is
A variable-length array, i.e. an array whose size can be set by a
run-time variable.

<C>
#include <stdlib.h>

void f(size_t z) {

typedef enum some_t { some_value } some_type;

some_type a[5] = { some_value }; // always ok
// some_type b[z] = { some_value }; // always illegal
some_type c[z]; // ok in c99 only}

</C>
<...>

The std::vector is preferable in most respects, anyway. The down-side of
vector is that it uses dynamic storage, which may incur more performance
overhead than automatic (stack-based) storage.
Ah ha yes, but does the dynamic array actually just look like a
std::vector underneath (use heap allocation) or is there some other
magic.

Its a sort of holy grail isnt it, that you can create a dynamically
sized array without the heap, so if C99 dynamic array does use another
method to allocate, would be interesting.

(my guess is that it woud have same performance as std::vector. There
is no "magic")
I believe VLAs use the stack, just like traditional arrays. The "magic"
is that the compiler doesn't know a priori how big the function's stack
frame will be; in fact, multiple invocations of the same function might
need frames of different sizes. It would be interesting to know what
kinds of complications this creates for compiler implementers, and how
those problems have been worked around.

There should be little trouble to create the actual frame, nor to use
VLAs, you just have to remember the size of each VLA and use it to
calculate the offsets. Which means that some operations (probably all
that accesses elements in an VLA) will require one or more additional
computations to calculate the offset from the stack-pointer.

That's per-invocation overhead, right? So recursive functions with VLAs
could get very messy...
 
J

Jeff Schwab

Pete said:
They're not required to.

Are there any implementations that use free store? If the compiler has
to ensure that all possible return paths free the memory, it seems like
it's half way to C++-style destructors.
 
K

kwikius

<C>
#include <stdlib.h>

void f(size_t z) {

        some_type c[z]; // ok in c99 only}

Ah I think I get it. As far as the array is concerned z is a constant,
so it cant resize after its created. Thats quite neat, if you can grab
an arbitrary amount of space on the stack in a function.

As observed later, it then raises questions about how this affects
optimisations, as you have a runtime constant.

So it looks like you arent getting something for nothing, but maybe
you are getting it quite cheap :)

regards
Andy Little
 
I

Ian Collins

kwikius said:
<C>
#include <stdlib.h>

void f(size_t z) {

some_type c[z]; // ok in c99 only}

Ah I think I get it. As far as the array is concerned z is a constant,
so it cant resize after its created. Thats quite neat, if you can grab
an arbitrary amount of space on the stack in a function.

As observed later, it then raises questions about how this affects
optimisations, as you have a runtime constant.
There is also one potentially crippling problem with VLAs: no indication
of failure. You can bugger your stack without realising.

Another complication is the incompatibility with C caused by the
different interpretation of const,

void f() {
const size_t s = 42;

int bla;
}

requires a VLA in C, but not in C++.
 
K

kwikius

        some_type c[z]; // ok in c99 only}
Ah I think I get it. As far as the array is concerned z is a constant,
so it cant resize after its created. Thats quite neat, if you can grab
an arbitrary amount of space on the stack in a function.
As observed later, it then raises questions about how this affects
optimisations, as you have a runtime constant.

There is also one potentially crippling problem with VLAs: no indication
of failure.  You can bugger your stack without realising.

Another complication is the incompatibility with C caused by the
different interpretation of const,

void f() {
  const size_t s = 42;

  int bla;

}

requires a VLA in C, but not in C++.


My current opinion is:

Looks cool but only on paper. Essentially its a local optimisation,
but in practise gives the optimiser one more extremely heavyweight
variable to deal with, the stack pointer, and so probably causes major
problems in wider optimisations, such as inlining. OTOH I don't know
much about optimisation, but they do seem to run a mile from runtime
constants and especially pointers which this looks likely to create in
abundance.

So in practise I would guess most implementations would end up using
the heap to solve this, unless they are specifically not allowed to,
which apparently isnt the case.

Its interesting though ... I guess C++ can sit back and watch
progress.

regards
Andy Little
 
J

James Kanze

kwikius said:
On Feb 13, 4:52 am, Jeff Schwab <[email protected]>
wrote:
<C>
#include <stdlib.h>
void f(size_t z) {
<..>
some_type c[z]; // ok in c99 only}
Ah I think I get it. As far as the array is concerned z is
a constant, so it cant resize after its created. Thats
quite neat, if you can grab an arbitrary amount of space
on the stack in a function.
As observed later, it then raises questions about how this
affects optimisations, as you have a runtime constant.
There is also one potentially crippling problem with VLAs:
no indication of failure. You can bugger your stack without
realising.

You can do that very well without VLA's as well. Stack overflow
is undefined behavior in both C and C++.
Another complication is the incompatibility with C caused by
the different interpretation of const,
void f() {
const size_t s = 42;
int bla;
}
requires a VLA in C, but not in C++.

My current opinion is:
Looks cool but only on paper. Essentially its a local optimisation,
but in practise gives the optimiser one more extremely heavyweight
variable to deal with, the stack pointer, and so probably causes major
problems in wider optimisations, such as inlining. OTOH I don't know
much about optimisation, but they do seem to run a mile from runtime
constants and especially pointers which this looks likely to create in
abundance.
So in practise I would guess most implementations would end up using
the heap to solve this, unless they are specifically not allowed to,
which apparently isnt the case.

I'm pretty sure that most implementations do allocate on the
stack, at least in simple cases, and probably in more
complicated cases as well. (Remember, the memory has to be
freed if you do a longjmp out of the function.)

Simple cases like these weren't the reason it wasn't adopted
into C++. The fact that you can use a VLA as the last element
of a struct causes problems with inheritance, for example. And
of course, the compiler would have to memorize somewhere how
many times to call the destructor. And sizeof a VLA, or a
struct with a VLA, isn't a constant. It could certainly be made
to work in C++, with enough restrictions. but overall, it would
require some analysis to adapt, and I guess no one felt is was
worth the work.
 
I

Ian Collins

James said:
You can do that very well without VLA's as well. Stack overflow
is undefined behavior in both C and C++.
True, but I was drawing a comparison with the idiomatic C++ solution
(the use of std::vector). VLAs are a C solution to a problem we don't
have in C++.
 
J

Jerry Coffin

[ ... ]
Not AFAIK, although C++0x will incorporate some features of C99. The
std::vector is preferable in most respects, anyway. The down-side of
vector is that it uses dynamic storage, which may incur more performance
overhead than automatic (stack-based) storage. Chances are excellent
that whatever you're doing, std::vector is a better choice than a raw array.

C++ 0x also includes std::array, which is really much closer to a VLA --
i.e. the size is constant once it's created. The only major difference
that occurs to me offhand is that std::array has explicit support for
zero-sized arrays, whereas C99 requires the size of a VLA to be greater
than zero.
 
J

Jeff Schwab

Jerry said:
[ ... ]
Not AFAIK, although C++0x will incorporate some features of C99. The
std::vector is preferable in most respects, anyway. The down-side of
vector is that it uses dynamic storage, which may incur more performance
overhead than automatic (stack-based) storage. Chances are excellent
that whatever you're doing, std::vector is a better choice than a raw array.

C++ 0x also includes std::array, which is really much closer to a VLA --
i.e. the size is constant once it's created. The only major difference
that occurs to me offhand is that std::array has explicit support for
zero-sized arrays, whereas C99 requires the size of a VLA to be greater
than zero.

The point of a VLA is that the size doesn't have to be a constant
expression. The size of a TR1 std::array is a template parameter, and
as such does have to be a constant (at compile-time) expression. As far
as I know, std::vector will continue to be the closest thing C++ has to
a C99 VLA.
 

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

Members online

Forum statistics

Threads
474,179
Messages
2,570,956
Members
47,509
Latest member
Jack116

Latest Threads

Top