Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Create array at runtime

26 views
Skip to first unread message

Joseph Hesse

unread,
Sep 29, 2017, 11:37:34 AM9/29/17
to
I am surprised that the following compiles with g++.

void f(int x)
{
int a[x];
}

I thought that the size of such an array had to be known at compile time
and couldn't be created at run time? Is this a new feature?
Thank you,
Joe

Rick C. Hodgin

unread,
Sep 29, 2017, 11:42:27 AM9/29/17
to
GCC has allowed VLA's in C++ for a while:

https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html

--
Thank you, | Indianapolis, Indiana | God is love -- 1 John 4:7-9
Rick C. Hodgin | http://www.libsf.org/ | http://tinyurl.com/yaogvqhj
-------------------------------------------------------------------------
Software: LSA/C, Debi, RDC, CAlive, ES/2, VJr, VFrP, Logician, C90/99
Hardware:
Aroxda Desktop CPU -- 40-bit multi-core 80386 with many extensions
Arxita Embedded CPU -- Low power, low pin count 80386 w/128 registers
Arlina Compute FPGA -- Scalable compute cores, large GPIO pin count

red floyd

unread,
Sep 29, 2017, 4:47:14 PM9/29/17
to
On 9/29/2017 10:12 AM, Stefan Ram wrote:
> Joseph Hesse <jo...@gmail.com> writes:
>> I am surprised that the following compiles with g++.
>> void f(int x)
>> {
>> int a[x];
>> }
>
> This might be a compiler-specific extension.
>
> Maybe you would like to try:
>
> -std=c++14 -pedantic -pedantic-errors

Isn't that a C99-ism?


bitrex

unread,
Sep 29, 2017, 6:16:05 PM9/29/17
to
On 09/29/2017 01:12 PM, Stefan Ram wrote:
> Joseph Hesse <jo...@gmail.com> writes:
>> I am surprised that the following compiles with g++.
>> void f(int x)
>> {
>> int a[x];
>> }
>
> This might be a compiler-specific extension.
>
> Maybe you would like to try:
>
> -std=c++14 -pedantic -pedantic-errors

It compiles OK with -std=C++98 for all versions of gcc for x86-64 over
4.4.7; the -pedantic flag gives the warning "ISO C++ forbids variable
length arrays." Clang 3.0 and above for x86-64 it also compiles but
warns "variable length arrays are a C99 feature, accepted as extension"
with -std=C++98 -pedantic; with -std=C++14 -pedantic for clang 3.5 and
above it just reports "variable length arrays are a C99 feature"

0 new messages