By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,432 Members | 790 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,432 IT Pros & Developers. It's quick & easy.

Rules for statically dimensioning arrays

P: n/a
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

Jul 23 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
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;
-}

Jul 23 '05 #2

P: n/a
Samee Zahur wrote:
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.

Jul 23 '05 #3

P: n/a
"Samee Zahur" <sa*********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 23 '05 #4

P: n/a
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

Jul 23 '05 #5

P: n/a
Samee Zahur wrote:
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.

--
Anti-spam address, change each 'X' to '.' to reply directly.
Jul 23 '05 #6

P: n/a
Samee Zahur wrote:
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

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #7

P: n/a
Samee Zahur wrote:
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.

Jul 23 '05 #8

P: n/a
On 7 May 2005 06:56:41 -0700, "Samee Zahur" <sa*********@gmail.com>
wrote in comp.lang.c++:
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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 23 '05 #9

P: n/a
On Sat, 07 May 2005 19:46:26 +0200, Rolf Magnus <ra******@t-online.de>
wrote in comp.lang.c++:
Samee Zahur wrote:
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.


Still the same in C for statically allocated arrays. Only arrays with
automatic storage duration may be variable array types.
Exactly, by how many years am I back-dated ???


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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 23 '05 #10

P: n/a
On Sat, 7 May 2005 20:44:28 +0200, "Ivan Vecerina"
<IN*************************@vecerina.com> wrote in comp.lang.c++:
"Samee Zahur" <sa*********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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.
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


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 23 '05 #11

P: n/a
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. :-)
What a mess. Why did not they define them to be stored in the free store?

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #12

P: n/a
"Ioannis Vranos" <iv*@remove.this.grad.com> wrote in message
news:1115623637.830416@athnrd02...
: 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
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 23 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.