Rules for statically dimensioning arrays

S

Samee Zahur

Back in the days of old C, only numeric literals could be used as
dimensions for statically allocated arrays - the size had to be
resolved to a constant at/before compile time. Now I'm beginning to
suspect(!) that this is no longer the case either with C or with C++ :(

Can anyone upgrade me with the present state/version of rules? (for
C++)

Exactly, by how many years am I back-dated ???

Samee
 
W

wittempj

in c++ you can use stl containers - when use push_back there's no need
to allocate memory beforehand, see as well e.g.
http://www.sgi.com/tech/stl/Vector.html

-#include <iostream>
-#include <vector>
-
-using namespace std;
-
-int main(void)
-{
- vector<int> v;
-
- cout << v.capacity() << endl;
- cout << v.size() << endl;
-
- v.push_back(1);
-
- cout << v.capacity() << endl;
- cout << v.size() << endl;
-
- v.reserve(25);
- cout << v.capacity() << endl;
- cout << v.size() << endl;
-
- return 0;
-}
 
R

Rolf Magnus

Samee said:
Back in the days of old C, only numeric literals could be used as
dimensions for statically allocated arrays - the size had to be
resolved to a constant at/before compile time. Now I'm beginning to
suspect(!) that this is no longer the case either with C or with C++ :(

For C, it is no longer true.
Can anyone upgrade me with the present state/version of rules? (for
C++)

Still the same. sizes of statically allocated arrays must be compile-time
constants.
Exactly, by how many years am I back-dated ???

I don't know.
 
I

Ivan Vecerina

Samee Zahur said:
Back in the days of old C, only numeric literals could be used as
dimensions for statically allocated arrays - the size had to be
resolved to a constant at/before compile time. Now I'm beginning to
suspect(!) that this is no longer the case either with C or with C++ :(

Can anyone upgrade me with the present state/version of rules? (for
C++)
This has not changed for C++ yet (as C++ is derived from the ANSI C '90
standard). But C++ offers standard library containers as a preferred
means to store dynamically sized collections.
Exactly, by how many years am I back-dated ???
In 1999, an update to the ANSI/ISO C standard has introduced
run-time sized arrays. E.g.:
void f(int n)
{
char storage[n] = {0}; // size defined at run time
}
The C++ equivalent is:
std::vector<char> storage(i);
It has several benefits, in terms of being able to resize
the collection, and well-defined behavior in case of an
out-of-memory error -- among other pros.
This said, to maintain close compatibility with C, it is
likely that the next C++ standard will incorporate this new
feature of the C language.


Regards,
Ivan
 
S

Samee Zahur

Hmm... I just tried out my codes in Comeau - doesn't compile...
actually g++ allows that C99's extension even in C++'s code ... that's
what got me confused.

Guess I should get myself a copy of the standard soon(?) ...
Thanks anyway

Samee
 
L

Larry I Smith

Samee said:
Hmm... I just tried out my codes in Comeau - doesn't compile...
actually g++ allows that C99's extension even in C++'s code ... that's
what got me confused.

Guess I should get myself a copy of the standard soon(?) ...
Thanks anyway

Samee

Force ANSI compliance by passing the -ansi and -pedantic
flags to g++ and see if it still compiles.
 
I

Ioannis Vranos

Samee said:
Hmm... I just tried out my codes in Comeau - doesn't compile...
actually g++ allows that C99's extension even in C++'s code ... that's
what got me confused.

Guess I should get myself a copy of the standard soon(?) ...
Thanks anyway


Better get this book:

http://www.acceleratedcpp.com
 
R

Rolf Magnus

Samee said:
Hmm... I just tried out my codes in Comeau - doesn't compile...
actually g++ allows that C99's extension even in C++'s code ... that's
what got me confused.

If you want it to be compliant, you have to specify that. By default, GNU
extensions are on and many warnings are off in gcc. So you should at least
add -ansi -pedantic -Wextra -Wall to the command line options. Instead of
-ansi, you can also try -std=c++98.
 
J

Jack Klein

Back in the days of old C, only numeric literals could be used as
dimensions for statically allocated arrays - the size had to be
resolved to a constant at/before compile time. Now I'm beginning to
suspect(!) that this is no longer the case either with C or with C++ :(

Can anyone upgrade me with the present state/version of rules? (for
C++)

Exactly, by how many years am I back-dated ???

Samee

Actually both Rolf and Ivan are right in general about C, but dead
wrong about the answer to your particular question.

Variable length arrays (VLAs) were added to C by the 1999 update to
the ISO C standard, but not, as your question specifically asked, for
statically allocated arrays.

VLAs are allowed in arrays with automatic storage duration only, that
is arrays defined inside functions.

Under the C99 standard you can do this:

void my_func(int x)
{
double array [x];
/* stuff */
}

....and unlike C's dynamic allocation, there is no warning at all if
sufficient space is not available, and your program is likely to be
terminated abruptly if there is not.

But even under C99, you cannot do this:

static double [x];

....or this at file or namespace scope, which always have static
storage duration:

double [x];

....where x is a variable.

Arrays with static storage in C must be defined with dimensions that
are compile-time integer constant expressions that evaluate to values
greater than 0.
 
J

Jack Klein

For C, it is no longer true.


Still the same. sizes of statically allocated arrays must be compile-time
constants.

Still the same in C for statically allocated arrays. Only arrays with
automatic storage duration may be variable array types.
I don't know.

The C standard that permits variable length arrays with automatic
storage duration was adopted by ISO in November 1999, and ANSI in May
2000.
 
J

Jack Klein

Samee Zahur said:
Back in the days of old C, only numeric literals could be used as
dimensions for statically allocated arrays - the size had to be
resolved to a constant at/before compile time. Now I'm beginning to
suspect(!) that this is no longer the case either with C or with C++ :(

Can anyone upgrade me with the present state/version of rules? (for
C++)
This has not changed for C++ yet (as C++ is derived from the ANSI C '90
standard). But C++ offers standard library containers as a preferred
means to store dynamically sized collections.
Exactly, by how many years am I back-dated ???
In 1999, an update to the ANSI/ISO C standard has introduced
run-time sized arrays. E.g.:
void f(int n)
{
char storage[n] = {0}; // size defined at run time
}

And you have shown a correct example of their use, since 'storage' has
automatic storage duration. But the OP specifically asked about
"statically allocated arrays", and even under C99 they still require
an integer constant expression that evaluates to a value greater than
0.
The C++ equivalent is:
std::vector<char> storage(i);

That is not really equivalent at all, since it uses dynamic
allocation.
 
I

Ioannis Vranos

Jack said:
Under the C99 standard you can do this:

void my_func(int x)
{
double array [x];
/* stuff */
}

...and unlike C's dynamic allocation, there is no warning at all if
sufficient space is not available, and your program is likely to be
terminated abruptly if there is not.


This is interesting. We could say that using VLAs in C99 invokes undefined behaviour. :)
What a mess. Why did not they define them to be stored in the free store?
 
I

Ivan Vecerina

: Jack Klein wrote:
:
: > Under the C99 standard you can do this:
: >
: > void my_func(int x)
: > {
: > double array [x];
: > /* stuff */
: > }
: >
: > ...and unlike C's dynamic allocation, there is no warning at all if
: > sufficient space is not available, and your program is likely to be
: > terminated abruptly if there is not.
:
:
: This is interesting. We could say that using VLAs in C99 invokes
: undefined behaviour. :)
Yes, just as any function call can (cause a stack overflow).

: What a mess. Why did not they define them to be stored in the free store?
I think an implementation is free to use the free store for VLA.
But a free store allocation may fail as well.

The root of the issue is that there is no way (in C99) to detect
and handle this error condition. It seems that the standard
does not even want to specify that there is a way to check
for a stack overflow - which would be useful for recursion
problems as well.



Ivan
 
R

Rolf Magnus

Ioannis said:
Jack said:
Under the C99 standard you can do this:

void my_func(int x)
{
double array [x];
/* stuff */
}

...and unlike C's dynamic allocation, there is no warning at all if
sufficient space is not available, and your program is likely to be
terminated abruptly if there is not.


This is interesting. We could say that using VLAs in C99 invokes undefined
behaviour. :) What a mess. Why did not they define them to be stored in
the free store?

What for? If you want to use the free store in C, you can use malloc.
If VLAs would have to use the free store, that would make a language
built-in feature dependant on a library function, and I guess that
something like that is not wanted in C.
 

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
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top