C++ Logo

std-discussion

Advanced search

Re: dynamic size of a fixed array - compiles well on gcc, why?

From: Bernhard Manfred Gruber <bernhardmgruber_at_[hidden]>
Date: Mon, 15 Jun 2020 09:23:36 +0200
Hi Hollowone PL,

what you discovered is actually a feature. It's called VLAs (Variable
Length Arrays).

Strictly speaking, VLAs are not a C++ feature but were introduced in the
C99 standard and then made optional in C11.
Your compiler is supporting them, because it supports C as well to C++.

Cheers,
Bernhard

On Mon, 15 Jun 2020 at 09:12, Hollowone PL via Std-Discussion <
std-discussion_at_[hidden]> wrote:

>
> Hello everybody,
>
> I'm writing as I'm curious about one accidental discovery, due to bug that
> compiles well and works well on gcc.
>
> I declare fixed size array like I used to for years:
>
> int my_array[] = { 0, 1, 2, 3, 4};
>
> then I found code that accidentally allocates size of such array with a
> variable that is defined during runtime
>
> int some_size = value_retrieved_from_somewhere_else;
> int my_array[some_size];
> my_array[3] = 123;
>
> and this code compiles and works well under gcc's c++ even though
> some_size is not constexpr.
>
> Obvious bug in the compiler or some feature I'm not aware of?
>
> -
> /h1
> --
> Std-Discussion mailing list
> Std-Discussion_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion
>

Received on 2020-06-15 02:26:54