Questions tagged [variable-length-array]

A variable length array is an array in C99 and other languages whose size is unknown at compile time; instead, it's determined at runtime.

329 questions
13
votes
2 answers

Passing a multidimensional array of variable size

I'm trying to understand what "best practice" (or really any practice) is for passing a multidimensional array to a function in c is. Certainly this depends on the application, so lets consider writing a function to print a 2D array of variable…
12
votes
2 answers

How is the size of a variable length array computed at runtime in C99?

In C89, the length of an array is known at compile time. But in C99, with variable length arrays, the length of an array may be unknown before runtime. So how does it get computed? And why couldn't the length of a dynamically allocated array be…
minh.hieu
  • 123
  • 5
12
votes
4 answers

What technical disadvantages do C99-style VLAs have?

I heard from many people that variable length array, introduced in C99, are terrible. Some guys on IRC said a minute ago « I don't think C++ will get VLA's, strousoup made some very negative comments about them ». What are the reasons why those…
qdii
  • 11,387
  • 7
  • 54
  • 107
11
votes
5 answers

Can we have a struct element of type Variable length array?

Can we declare a structure element of variable length? The condition is as follows: typedef struct { uint8_t No_Of_Employees; uint8_t Employee_Names[No_Of_Employees][15]; }st_employees;
user12345
  • 319
  • 2
  • 3
  • 9
10
votes
2 answers

Variably-modified types compatibility and its security implications

I'm going through a surge of interest in C99's variably-modified type system. This question was inspired by this one. Checking the code from this question, I discovered something interesting. Consider this code: int myFunc(int, int, int,…
Mints97
  • 5,492
  • 4
  • 30
  • 65
10
votes
5 answers

Variable length arrays (VLA) in C and C++

Possible Duplicate: Variably modified array at file scope I have some concepts about the VLA and its behavior that I need to clarify. AFIK since C99 it's possible to declare VLA into local scopes: int main(int argc, char **argv) { // function…
PaperBirdMaster
  • 11,602
  • 9
  • 41
  • 84
10
votes
1 answer

Undocumented GCC Extension: VLA in struct

While reading the Clang documentation, I came across the following intriguing tidbit: [1] clang does not support the gcc extension that allows variable-length arrays in structures. This is for a few reasons: one, it is tricky to implement, two, the…
void-pointer
  • 12,317
  • 11
  • 40
  • 60
9
votes
1 answer

C standard regarding sizeof overflowing size_t

Is this undefined behavior? The relevant parts of the standard don't say much. size_t n = SIZE_MAX / sizeof(double) + 1; size_t m = sizeof(double[n]);
ov2k
  • 263
  • 1
  • 7
9
votes
3 answers

Why Are Zero Length VLAs UB?

The 2011 standard explicitly states... 6.7.6.2 Array declarators If the size is an expression that is not an integer constant expression: if it occurs in a declaration at function prototype scope, it is treated as if it were replaced by *;…
Jason
  • 3,677
  • 12
  • 24
9
votes
4 answers

declaring a variable-length array as a global variable in C

How is it possible to declare a variable-length array as a global variable ? when variable length array is declared in a function before the length is scanned, it compiles but does not run. it gives segmentation fault. when the same declaration…
KawaiKx
  • 8,125
  • 15
  • 64
  • 91
8
votes
4 answers

Why should global array size be an integer constant?

In C++ I tried declaring a global array of some size. I got the error: array bound is not an integer constant before ‘]’ token But when I declared an array of the same type in the main() function it is working fine. Why is there different…
8
votes
1 answer

Cast "pointer to const" to "pointer to const VLA"

In this snippet, a pointer to VLA is used for easier access to a big lookup table : #pragma GCC diagnostic warning "-Wcast-qual" char lookup(int a, int b, int c, char const *raw, int x, int y, int z) { typedef char const (*DATA_PTR)[a][b][c]; …
diapir
  • 2,493
  • 1
  • 15
  • 24
8
votes
1 answer

How to convert C variable-length array code to Rust?

I know Rust doesn't support variable-length arrays, but that leaves me wondering what to replace them with, given that: I don't want to allocate and deallocate a tiny Vec in a loop The borrow checker doesn't let me move the code outside the…
Kornel
  • 91,239
  • 30
  • 200
  • 278
8
votes
3 answers

Dynamic array allocation on stack in C

I just did a experiment yesterday, and find something confusing: #include int main() { int j; scanf("%d",&j); const int i = j; int arr[i]; return 0; } The number j is read from keyboard and it’s used to allocate the…
user3769509
  • 81
  • 1
  • 1
  • 3
8
votes
2 answers

Are variable length arrays there in c++?

I had always thought that variable length arrays were not allowed in c++(Refer :Why aren't variable-length arrays part of the C++ standard?) .But than why does this code compile and work? #include using namespace std; int main () { …
1 2
3
21 22