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

variable as an array size

P: n/a
Hi,

I'm wondering what you think about this piece of code:

#include<iostream>

int main()
{
int size;
std::cin >> size;
int array[size];

std::cout<<sizeof(int)<<" "<<sizeof(array);
return 0;
}

Is it legal or not (according to C/C++ standard)?
A couple of days ago I would say it is not, only constant expression can
be used to specify the size, of the array, but now I'm not sure :) The
reason is that gcc 3.4.4 compiles at with no errors and warnings.
And the result printed by the program is correct:

sizeof(arr) = size * sizeof(int)

P.S.
MS Visual C++ 7.1 does not compile this code. It gives the following errors:
main.cpp(7) : error C2057: expected constant expression
main.cpp(7) : error C2466: cannot allocate an array of constant size 0
main.cpp(7) : error C2133: 'array' : unknown size
main.cpp(9) : error C2070: 'int []': illegal sizeof operand
Oct 12 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
HappyHippy wrote:

Hi,

I'm wondering what you think about this piece of code:

it uses an gcc extension.
A couple of days ago I would say it is not, only constant expression can
be used to specify the size,


As for now, you are correct.

--
Karl Heinz Buchegger
kb******@gascad.at
Oct 12 '05 #2

P: n/a
I really don't like this unnecessary extension. I tried to turn on
some flags to force gcc to treat it as an error. But all I got so far
is an warning with -pedantic. Is there any way to escalate it to
error? Thanks.

[nan@xxx test]$ cat ary.c
int main()
{
int size;
int array[size];
return 0;
}

[nan@xxx test]$ gcc -pedantic ary.c
ary.c: In function `main':
ary.c:4: warning: ISO C89 forbids variable-size array `array'

[nan@xxx test]$ gcc -Wall -ansi -std=c89 ary.c
ary.c: In function `main':
ary.c:4: warning: unused variable `array'

[nan@xxx test]$ gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2.3/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--disable-checking --with-system-zlib --enable-__cxa_atexit
--host=i386-redhat-linux
Thread model: posix
gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-20)

Oct 12 '05 #3

P: n/a
Karl Heinz Buchegger wrote:
HappyHippy wrote:

Hi,

I'm wondering what you think about this piece of code:


it uses an gcc extension.
A couple of days ago I would say it is not, only constant expression can
be used to specify the size,


As for now, you are correct.

--
Karl Heinz Buchegger
kb******@gascad.at

I really don't like this unnecessary extension. I tried to turn on
some flags to force gcc to treat it as an error. But all I got so far
is an warning with -pedantic. Is there any way to escalate it to
error? Thanks.

[nan@xxx test]$ cat ary.c
int main()
{
int size;
int array[size];
return 0;
}

[nan@xxx test]$ gcc -pedantic ary.c
ary.c: In function `main':
ary.c:4: warning: ISO C89 forbids variable-size array `array'

[nan@xxx test]$ gcc -Wall -ansi -std=c89 ary.c
ary.c: In function `main':
ary.c:4: warning: unused variable `array'

[nan@xxx test]$ gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2.3/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--disable-checking --with-system-zlib --enable-__cxa_atexit
--host=i386-redhat-linux
Thread model: posix
gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-20)

Oct 12 '05 #4

P: n/a
In article <di**********@charm.magnus.acs.ohio-state.edu>,
HappyHippy <ka******@mail.ru> wrote:
I'm wondering what you think about this piece of code:

#include<iostream>

int main()
{
int size;
std::cin >> size;
int array[size];

std::cout<<sizeof(int)<<" "<<sizeof(array);
return 0;
}

Is it legal or not (according to C/C++ standard)?
It is not legal C++.
It is not legal C90.
It is legal C99.
A couple of days ago I would say it is not, only constant expression can
be used to specify the size, of the array, but now I'm not sure :) The
reason is that gcc 3.4.4 compiles at with no errors and warnings.
And the result printed by the program is correct:

sizeof(arr) = size * sizeof(int)

P.S.
MS Visual C++ 7.1 does not compile this code. It gives the following errors:
main.cpp(7) : error C2057: expected constant expression
main.cpp(7) : error C2466: cannot allocate an array of constant size 0
main.cpp(7) : error C2133: 'array' : unknown size
main.cpp(9) : error C2070: 'int []': illegal sizeof operand


If a compiler supports C99 or at least C99's VLA (variable length array)
features, or some similar extension, then it'll work with that compiler.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 12 '05 #5

P: n/a
There were a lot of discussions at comp.lang.c back in 2003.
http://groups.google.com/group/comp....6113459b9beb6d

Also more clear explanation here.
http://developer.apple.com/documenta...le-Length.html

After reading the above, I think it is actually quite a good feature to
have.

Nan
Greg Comeau wrote:
In article <di**********@charm.magnus.acs.ohio-state.edu>,
HappyHippy <ka******@mail.ru> wrote:
I'm wondering what you think about this piece of code:

#include<iostream>

int main()
{
int size;
std::cin >> size;
int array[size];

std::cout<<sizeof(int)<<" "<<sizeof(array);
return 0;
}

Is it legal or not (according to C/C++ standard)?


It is not legal C++.
It is not legal C90.
It is legal C99.
A couple of days ago I would say it is not, only constant expression can
be used to specify the size, of the array, but now I'm not sure :) The
reason is that gcc 3.4.4 compiles at with no errors and warnings.
And the result printed by the program is correct:

sizeof(arr) = size * sizeof(int)

P.S.
MS Visual C++ 7.1 does not compile this code. It gives the following errors:
main.cpp(7) : error C2057: expected constant expression
main.cpp(7) : error C2466: cannot allocate an array of constant size 0
main.cpp(7) : error C2133: 'array' : unknown size
main.cpp(9) : error C2070: 'int []': illegal sizeof operand


If a compiler supports C99 or at least C99's VLA (variable length array)
features, or some similar extension, then it'll work with that compiler.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?


Oct 12 '05 #6

P: n/a
Greg Comeau wrote:

It is not legal C++.
It is not legal C90.
It is legal C99.
If a compiler supports C99 or at least C99's VLA (variable length
array) features, or some similar extension, then it'll work with that
compiler.

Greg is (as always) correct. I seem to recall that the C++ committee
was reviewing VLAs for possible inclusion, but I don't know whether
that's actually the case.

It isn't as important in C++, because one can use std::vector in most
cases where a VLA would be used.

Brian
Oct 12 '05 #7

P: n/a
Default User wrote:
It isn't as important in C++, because one can use std::vector in most
cases where a VLA would be used.
The problem with introducing variable sized arrays, I would guess is
the fact that one may run out of stack more easily. However, allocation
on the stack is re-entrant, does not require overhead concerned with
heap allocation etc. IMHO it would be good if the standards comittee
would introduce a new type - specifically for this purpose. Let arrays
still have fixed sizes - no suprises. If one wants variable sized
arrays, be aware that they are allocated on the stack only... You think
this is possible/plausible?

Regards,

W

Brian


Oct 12 '05 #8

P: n/a
In article <11**********************@g44g2000cwa.googlegroups .com>,
Nan Li <na******@gmail.com> wrote:
There were a lot of discussions at comp.lang.c back in 2003.
http://groups.google.com/group/comp....6113459b9beb6d
That thread started as a question about VLAs but except for the first
few messages, the rest has nothing to do with it.
Also more clear explanation here.
http://developer.apple.com/documenta...le-Length.html

After reading the above, I think it is actually quite a good feature to
have.


Maybe in C. Maybe. But C++ has alther alternatives, probably
better ones.

Oddly, it will be C++ that ends up popularizing C99 features.
I don't see that as good.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 12 '05 #9

P: n/a
werasm wrote:
Default User wrote:
It isn't as important in C++, because one can use std::vector in
most cases where a VLA would be used.


The problem with introducing variable sized arrays, I would guess is
the fact that one may run out of stack more easily. However,
allocation on the stack is re-entrant, does not require overhead
concerned with heap allocation etc. IMHO it would be good if the
standards comittee would introduce a new type - specifically for this
purpose. Let arrays still have fixed sizes - no suprises. If one
wants variable sized arrays, be aware that they are allocated on the
stack only... You think this is possible/plausible?


If they do anything, I suspect it will be the same as C99.

Brian

Oct 12 '05 #10

P: n/a
In article <3r************@individual.net>,
Default User <de***********@yahoo.com> wrote:
werasm wrote:
Default User wrote:
> It isn't as important in C++, because one can use std::vector in
> most cases where a VLA would be used.


The problem with introducing variable sized arrays, I would guess is
the fact that one may run out of stack more easily. However,
allocation on the stack is re-entrant, does not require overhead
concerned with heap allocation etc. IMHO it would be good if the
standards comittee would introduce a new type - specifically for this
purpose. Let arrays still have fixed sizes - no suprises. If one
wants variable sized arrays, be aware that they are allocated on the
stack only... You think this is possible/plausible?


If they do anything, I suspect it will be the same as C99.


There is really no other incentive, except as C99 compatibilty,
at best, so yeah, if possible.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 12 '05 #11

P: n/a
Nan Li wrote:
I really don't like this unnecessary extension. I tried to turn on
some flags to force gcc to treat it as an error. But all I got so far
is an warning with -pedantic. Is there any way to escalate it to
error? Thanks.

int main()
{
int size;
int array[size];
return 0;
}

[nan@xxx test]$ gcc -pedantic ary.c
ary.c: In function `main':
ary.c:4: warning: ISO C89 forbids variable-size array `array'

[nan@xxx test]$ gcc -Wall -ansi -std=c89 ary.c
ary.c: In function `main':
ary.c:4: warning: unused variable `array'


$ g++ -o x x.c -Wall -Wextra -ansi -pedantic
x.c: In function `int main()':
x.c:5: error: ISO C++ forbids variable-size array `array'

<OT reason="we are in comp.lang.c++">
The C standard only specifies that invalid code generates
a diagnostic message. So GCC is conforming when it issues
the warning. In GCC you can escalate the warning with the
-pedantic-errors switch.

Note also that -ansi and -std=c89 mean the same thing,
you only need one or the other.

$ gcc -o x x.c -Wall -Wextra -ansi -pedantic-errors
x.c: In function `main':
x.c:5: error: ISO C90 forbids variable-size array `array'
x.c:5: warning: unused variable `array'

</OT>

Oct 12 '05 #12

P: n/a
Greg Comeau wrote:
In article <3r************@individual.net>,
Default User <de***********@yahoo.com> wrote:

> It isn't as important in C++, because one can use std::vector in
> most cases where a VLA would be used.
If they do anything, I suspect it will be the same as C99.


There is really no other incentive, except as C99 compatibilty,
at best, so yeah, if possible.


Easier on the implementors not to have yet another version of VLAs as
well.
Brian
Oct 12 '05 #13

P: n/a
On 12 Oct 2005 09:13:54 -0700, "Nan Li" <na******@gmail.com> wrote in
comp.lang.c++:

Top-posting is considered rude in this, and many other technical
newsgroups. It makes technical discussions very hard to follow. I
have reformatted your post and added my comments at the end.
Greg Comeau wrote:
In article <di**********@charm.magnus.acs.ohio-state.edu>,
HappyHippy <ka******@mail.ru> wrote:
I'm wondering what you think about this piece of code:

#include<iostream>

int main()
{
int size;
std::cin >> size;
int array[size];

std::cout<<sizeof(int)<<" "<<sizeof(array);
return 0;
}

Is it legal or not (according to C/C++ standard)?


It is not legal C++.
It is not legal C90.
It is legal C99.
A couple of days ago I would say it is not, only constant expression can
be used to specify the size, of the array, but now I'm not sure :) The
reason is that gcc 3.4.4 compiles at with no errors and warnings.
And the result printed by the program is correct:

sizeof(arr) = size * sizeof(int)

P.S.
MS Visual C++ 7.1 does not compile this code. It gives the following errors:
main.cpp(7) : error C2057: expected constant expression
main.cpp(7) : error C2466: cannot allocate an array of constant size 0
main.cpp(7) : error C2133: 'array' : unknown size
main.cpp(9) : error C2070: 'int []': illegal sizeof operand


If a compiler supports C99 or at least C99's VLA (variable length array)
features, or some similar extension, then it'll work with that compiler.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?


There were a lot of discussions at comp.lang.c back in 2003.
http://groups.google.com/group/comp....6113459b9beb6d

Also more clear explanation here.
http://developer.apple.com/documenta...le-Length.html

After reading the above, I think it is actually quite a good feature to
have.

Nan


No, actually it is a dangerous and hideously unsafe feature,
especially is used carelessly. My impression is that it was a hack to
those who insisted that they couldn't live without gcc's (and other
compiler's) alloca(). They couldn't possibly afford the performance
hit of calling malloc(), or new[] in C++.

If the variable value happens to be too large to be accommodated, VLAs
can just terminate your program. They are not required to give you an
error indication such as a NULL pointer or throwing an exception, so
you can decide what to do.

If you want to be sure you can detect an insufficient memory problem
and decide how to deal with it, you can't depend on VLAs, you must use
malloc() or new[].

--
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

Oct 14 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.