variable as an array size

H

HappyHippy

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
 
K

Karl Heinz Buchegger

HappyHippy said:
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.
 
N

Nan Li

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

Nan Li

Karl said:
it uses an gcc extension.


As for now, you are correct.


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

Greg Comeau

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

Nan Li

There were a lot of discussions at comp.lang.c back in 2003.
http://groups.google.com/group/comp...ength+array+gcc&rnum=1&hl=en#b16113459b9beb6d

Also more clear explanation here.
http://developer.apple.com/documentation/DeveloperTools/gcc-4.0.0/gcc/Variable-Length.html

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

Nan


Greg said:
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?
 
D

Default User

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
 
W

werasm

Default said:
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
 
G

Greg Comeau


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/documentation/DeveloperTools/gcc-4.0.0/gcc/Variable-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.
 
D

Default User

werasm said:
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
 
O

Old Wolf

Nan said:
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>
 
J

Jack Klein

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 said:
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...ength+array+gcc&rnum=1&hl=en#b16113459b9beb6d

Also more clear explanation here.
http://developer.apple.com/documentation/DeveloperTools/gcc-4.0.0/gcc/Variable-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[].
 

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

No members online now.

Forum statistics

Threads
474,264
Messages
2,571,066
Members
48,770
Latest member
ElysaD

Latest Threads

Top