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
24
votes
2 answers

How does GCC implement variable-length arrays?

How does GCC implement Variable-length arrays (VLAs)? Are such arrays essentially pointers to the dynamically allocated storage such as returned by alloca? The other alternative I could think of, is that such an array is allocated as last variable…
box
  • 2,866
  • 2
  • 22
  • 32
23
votes
3 answers

Variable two dimensional array printing "subscript of pointer to incomplete type" when accessed

I am declaring a two dimensional array as such: char arr[10][10]; arr[0][0] = 'X'; Now I print in debugger; (lldb) po arr[0][0] 'X' Awesome!! No problem. Now I am declaring a two dimensional array as such: int col = 10; int row = 10; char…
etayluz
  • 14,171
  • 16
  • 86
  • 132
23
votes
2 answers

variable-length std::array like

As my usually used C++ compilers allow variable-length arrays (eg. arrays depending on runtime size), I wonder if there is something like std::array with variable size? Of course std::vectoris of variable size, but it allocates on heap, and…
dronus
  • 9,208
  • 8
  • 45
  • 72
22
votes
4 answers

How can I initialize an array without knowing it size?

I have a situation, where I have to apply a criteria on an input array and reuturn another array as output which will have smaller size based upon the filtering criteria. Now problem is I do not know the size of filtered results, so I can not…
Space Rocker
  • 767
  • 3
  • 9
  • 25
22
votes
3 answers

Variable length array in the middle of struct - why this C code is valid for gcc

There is some strange code using VLA (Variable Length Arrays) which is treated as Valid C (C99, C11) by gcc 4.6: $ cat a.c int main(int argc,char**argv) { struct args_t{ int a; int params[argc]; // << Wat? …
osgx
  • 80,853
  • 42
  • 303
  • 470
21
votes
1 answer

VLAs and side-effect in sizeof's operand

I know that sizeof never evaluates its operand, except in the specific case where said operand is a VLA. Or, I thought I knew. void g(int n) { printf("g(%d)\n", n); } int main(void) { int i = 12; char arr[i]; // VLA (void)sizeof…
Quentin
  • 58,778
  • 7
  • 120
  • 175
20
votes
4 answers

Dynamic arrays in C without malloc?

I've always wondered how I could get away with this: int main(int argc, char **argv) { printf("%p %s %d\n", &argv[1], argv[1], strlen(argv[1])); char copy[strlen(argv[1]) + 1]; strcpy(copy, argv[1]); printf("%p %s %d\n", ©, copy,…
lt0511
  • 203
  • 1
  • 2
  • 4
20
votes
3 answers

Initializing variable length array

On initializing a Variable length array compiler gives an error message: [Error] variable-sized object may not be initialized Code snippet: int n; printf("Enter size of magic square: "); scanf("%d",&n); int board[n][n] = {0}; How should…
haccks
  • 97,141
  • 23
  • 153
  • 244
17
votes
1 answer

What is the type of a pointer to a variable-length array in C?

Here's a short C program that prompts the user for a number, creates a variable-length array of ints of that size, and then uses pointer arithmetic to step over the elements that were allocated: #include int main() { /* Read a size…
templatetypedef
  • 328,018
  • 92
  • 813
  • 992
15
votes
1 answer

Is it safe to use variable-length arrays?

I have a concern about variable-length arrays. When I want to allocate an array dynamically, I'll get null, if it is not possible to allocate enough memory and I can respond to this properly in my program. With a variable length array I don't get…
gruszczy
  • 37,239
  • 27
  • 119
  • 167
15
votes
3 answers

Why is it allowed to declare an automatic array with size depending on user input?

I'm using MinGW to compile for C++11 and I found out that this doesn't throw an error: int S; cin>>S; char array[S]; While this does ("storage size of 'array' isn't known"): char array[]; To me, the size is also unknown in the first case, as it…
Floella
  • 1,121
  • 14
  • 37
14
votes
2 answers

Variable Length Array overhead in C++?

Looking at this question: Why does a C/C++ compiler need know the size of an array at compile time ? it came to me that compiler implementers should have had some times to get their feet wet now (it's part of C99 standard, that's 10 years ago) and…
Matthieu M.
  • 251,718
  • 39
  • 369
  • 642
14
votes
1 answer

Does "int size = 10;" yield a constant expression?

The following code compiles under gcc 4.8 and Clang 3.2: int main() { int size = 10; int arr[size]; } 8.3.4/1 of the C++ Standard says that the size of an array must be an integral constant expression, which size does not seem to be. Is this a…
13
votes
3 answers

Incorrect values when initializing a 2D array to 0 in gcc

#include using namespace std; int main() { int rows = 10; int cols = 9; int opt[rows][cols] = {0}; for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { std::cout <<…
dev_nut
  • 2,216
  • 3
  • 25
  • 38
13
votes
4 answers

Sizeof operator with variable-length array type

According to cppreference: If the type of expression is a variable-length array type, expression is evaluated and the size of the array it evaluates to is calculated at run time. It means: if the type of expression is a VLA type, then…
msc
  • 30,333
  • 19
  • 96
  • 184
1
2
3
21 22