I really need to do something like this in my code: char str[var+1]; but i know you can only put a constant between []. So i'm just asking if there is any way of doing what i need.
-
4In C++, why not just use a `std::vector`? In C you can also use `malloc` to allocate memory of a runtime-specified size. – Carcigenicate Apr 23 '20 at 17:59
-
You can `const` qualify `var`: `const int var = some_value;` – machine_1 Apr 23 '20 at 18:01
-
1@machine_1 For a compile time constant you'd want `constexpr` rather than just `const`. – Jesper Juhl Apr 23 '20 at 18:02
-
2Any reason why you cannot use `std::string`? If you can, that will be better than what you are attempting to do. – R Sahu Apr 23 '20 at 18:02
-
since c99, `var` can be a non-constant (see VLA: variable length array) – dvhh Apr 23 '20 at 18:03
-
3@dvhh VLAs are not valid in C++ though. – Jesper Juhl Apr 23 '20 at 18:03
-
4please dont double tag when the question does not actually involve both languages. Already most suggestions in the comments are not applicable in either C or C++ – 463035818_is_not_an_ai Apr 23 '20 at 18:03
-
@JesperJuhl true but as the question is currently also tagged with the `c` tag, I am tempted to give a `c` suggestion (plus citing standard level makes it less ambiguous) – dvhh Apr 23 '20 at 18:06
-
@JesperJuhl: natively, yes but are allowed as an extension. – machine_1 Apr 23 '20 at 18:07
-
@dvhh i really dont know c, but I suppose also in C making `var` `const` alone is not sufficient. – 463035818_is_not_an_ai Apr 23 '20 at 18:07
-
1@machine_1 "allowed as an extension" - only on *some* compilers. It inherently makes your code non portable - which makes it a bad idea in *my* book. – Jesper Juhl Apr 23 '20 at 18:08
-
1@machine_1 depends what you aim for, I would call relying on non-standard extension "naive". Why not use standard C++ when you can? Latest when you need to run the code on some exotic platform with limited compilers you will have a problem – 463035818_is_not_an_ai Apr 23 '20 at 18:09
-
@idclev463035818 as it was pointed out VLA are valid in `C` (c99, and optional in c11) but is not standard in `C++` – dvhh Apr 23 '20 at 18:09
-
1@dvhh oh sorry. You see, I dont know C ;) – 463035818_is_not_an_ai Apr 23 '20 at 18:10
2 Answers
It is only possible to declare a variable of compile time constant size in C++.
However, dynamic arrays can have a dynamic size. Simplest way to create a dynamic array is to use std::vector
, or in case of character string you can use std::string
. Example:
std::string str(var+1, '\0');

- 232,697
- 12
- 197
- 326
Question originaly included the C and C++ tag, this answer is for C
in C, var
can be non-constant for VLA suporting standards (c99 and optional support in c11)
The following is valid in C (see : https://godbolt.org/z/kUockA)
int var=3;
char str[var+1];
However VLA are not defined in the C++ standard (see: https://stackoverflow.com/a/1887178/105104 ) and not recommended to use in C either.
Because VLA are usually allocated on the stack and if the value of var
is not controlled the allocation of str
can fail, and recovery of such failure can be difficult. Additionally they could encourage the creation of highly unsafe code (if one would want to make pointer arithmetic on stack allocated variable).
There was an initiative to make the linux kernel code VLA-free (see : https://www.phoronix.com/scan.php?page=news_item&px=Linux-Kills-The-VLA ):
Using variable-length arrays can add some minor run-time overhead to the code due to needing to determine the size of the array at run-time.
VLAs within structures is not supported by the LLVM Clang compiler and thus an issue for those wanting to build the kernel outside of GCC, Clang only supports the C99-style VLAs.
Arguably most importantly is there can be security implications from VLAs around the kernel's stack usage.