473,765 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8948
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...@schwabcen ter.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...@schwabcen ter.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**@schwabcen ter.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...@schwabcen ter.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...@schwabcen ter.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**@schwabcen ter.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...@schwabcen ter.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...@schwabcen ter.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

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

Similar topics

6
2832
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 generateVals() { ..... //generate some values
8
3686
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 static; if the input file contained more than entries, tough. This time I want to do it right - use a dynamic array that increases in size with each word read from the file. A few test programs that make use of **List and realloc( List, blah...
1
4456
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 array of objects. I am skipping initialization and other member functions. class Inventory { Item *itemList; int idx, count, size;
6
4416
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 file. I have set up a Summary on another form that will break down all of the saved BMI numbers by percentage. I am using a dynamic array to load the information from the disk file, but for some reason it is not inputing the last record in the file. ...
19
6073
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. allocate an array of the same size as the vector and copy elements from the vector into the array */
1
2322
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 parent , will it be possible to pass the parent structure thru network using sockets onto other application running on different system provided the API is known to both the sender and the receiver.
11
7706
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
6283
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 my problem: #include<iostream> ... using namespace std;
1
4518
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 values to it inside form. VB6 (Code inside Module) 'Global type array to hold printer info. Public Type OShare PrinterName As String
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9399
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10007
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9957
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8832
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.