473,394 Members | 1,526 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

C99 dynamic array

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

regards
Andy Little
Feb 13 '08 #1
13 8910
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>
, 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.
Feb 13 '08 #2
On Feb 13, 4:52*am, Jeff Schwab <j...@schwabcenter.comwrote:
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")

regards
Andy Little
Feb 13 '08 #3
kwikius wrote:
On Feb 13, 4:52 am, Jeff Schwab <j...@schwabcenter.comwrote:
>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.
Feb 13 '08 #4
On 2008-02-13 15:50:05 -0500, Jeff Schwab <je**@schwabcenter.comsaid:
>
I believe VLAs use the stack, just like traditional arrays.
They're not required to.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Feb 13 '08 #5
On 2008-02-13 21:50, Jeff Schwab wrote:
kwikius wrote:
>On Feb 13, 4:52 am, Jeff Schwab <j...@schwabcenter.comwrote:
>>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.

--
Erik Wikström
Feb 13 '08 #6
Erik Wikström wrote:
On 2008-02-13 21:50, Jeff Schwab wrote:
>kwikius wrote:
>>On Feb 13, 4:52 am, Jeff Schwab <j...@schwabcenter.comwrote:
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...
Feb 14 '08 #7
Pete Becker wrote:
On 2008-02-13 15:50:05 -0500, Jeff Schwab <je**@schwabcenter.comsaid:
>>
I believe VLAs use the stack, just like traditional arrays.

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.
Feb 14 '08 #8
On Feb 13, 4:52*am, Jeff Schwab <j...@schwabcenter.comwrote:
<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

Feb 14 '08 #9
kwikius wrote:
On Feb 13, 4:52 am, Jeff Schwab <j...@schwabcenter.comwrote:
><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[s];
}

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

--
Ian Collins.
Feb 14 '08 #10
On Feb 14, 8:18*am, Ian Collins <ian-n...@hotmail.comwrote:
kwikius wrote:
On Feb 13, 4:52 am, Jeff Schwab <j...@schwabcenter.comwrote:
<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[s];

}

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



Feb 14 '08 #11
James Kanze wrote:
>On Feb 14, 8:18 am, Ian Collins <ian-n...@hotmail.comwrote:
>>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++.
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++.

--
Ian Collins.
Feb 14 '08 #12
In article <_Z******************************@comcast.com>,
je**@schwabcenter.com says...

[ ... ]
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Feb 18 '08 #13
Jerry Coffin wrote:
In article <_Z******************************@comcast.com>,
je**@schwabcenter.com says...

[ ... ]
>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.
Feb 18 '08 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Vasileios Zografos | last post by:
Hello, I have a function that generates some values (e.g. vertices in 2d space) the number of which I dont know. So, it could generate 20 vertices, 100 vertices, or even 1 vertex. void...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
1
by: lemonade | last post by:
Hello! Can someone explain to me the difference between dynamic array of pointers vs dynamic array of objects by giving a real life example. Following is the code that I am using for dynamic...
6
by: KeithJ | last post by:
Hello, I am fairly new to VB6 and I am experiencing a problem with a dynamic array. I made a program that calculates Body Mass Index and saves each individual BMI number to a sequential disk...
19
by: arnuld | last post by:
/* C++ Primer - 4/e * chapter 4- Arrays & Pointers, exercise 4.28 * STATEMENT * write a programme to read the standard input and build a vector of integers from values that are read....
1
by: Gurur | last post by:
Hi all, I have a doubt. If I have 2 structures and one is parent of other , ie the child structure is present in the parent one . And if the child structure is declared as dynamic array in the...
11
by: C C++ C++ | last post by:
Hi all, got this interview question please respond. How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Rgrds MA
1
by: lumumba401 | last post by:
Hello everybody, i am asking about how to define a bidimensional dynamic array as a global variable to use as incoming variable in a function Let us see , for example in a part of a programm...
1
by: remya1000 | last post by:
i'm using VB.net 2003 application program. i'm trying to convert a VB6 program to VB.NET. The VB6 code i'm trying to convert is shown below. declared g_Share() array in module and trying to add...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.