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.

413 questions
5
votes
1 answer

What are the differences between Variable Length Arrays and Flexible Array Member?

I've seen in the ISO C99 committee draft that structs can have an incomplete an array with unspecified size its end, known as Flexible Array Member. On the other hand C99 also has Variable Length Arrays, which allow declaring arrays with size not…
villasv
  • 6,304
  • 2
  • 44
  • 78
5
votes
1 answer

Multi-dimensional arrays: Variable length row?

It is possible to do variable length columns such as: private int k[][] = new int[3][]; for(int i = 0; i < k.length; i++) { k[i] = new int[i+1]; } I was wondering if it was possible to do variable length rows, if you know the length of a…
Volatile
  • 677
  • 2
  • 8
  • 17
4
votes
1 answer

Variable length arrays C99 not supported in C

In Visual Studio 2005, I'm trying to compile a .c file: int i = 6; int a[i]; It doesn't work, so which standard does my compiler follow?
user103214
  • 3,478
  • 6
  • 26
  • 37
4
votes
2 answers

Is it possible to use `std::copy` to copy values from a variable-sized array to a container?

Following is a MergeSort implementation. My problem is that the compiler complains that std::begin cannot be applied on a variable-sized array temp in order to further use std:copy. I am using C++17 and gcc 8.3. template
Sherry
  • 253
  • 1
  • 7
4
votes
3 answers

Variable length array parameter size expression with side effects

This question arose from a remark Eric Postpischil made in another thread. I have a hard time understanding the use of variable length arrays (VLAs) as function parameters: The array size is not checked. The array size is not recoverable from the…
4
votes
1 answer

C++ variable-sized object may not be initialized

I have the following simple source #include int main() { int nv; nv = 3; int arr[nv] = { 0, 2, 5 }; return 0; } When compiling with GCC on system 1 I get error: variable-sized object ‘arr’ may not be initialized. When…
4
votes
4 answers

Why doesn't work Variable length array as a globally?

If I write variable length array as a locally, like this: #include int main() { int i=1; int a[i]; printf("%zu",sizeof(a)); } It working fine in GCC compiler. But, If I write Variable length array as a globally, like…
msc
  • 33,420
  • 29
  • 119
  • 214
4
votes
2 answers

Dynamic memory allocation in C++?

Why does this work? #include int main() { std::cout << "Enter a number: "; int arraySize; std::cin >> arraySize; int array[arraySize]; for(int element : array) { …
Alex
  • 63
  • 6
4
votes
1 answer

Finding variable-length arrays in code

When the size of my input is big enough, segmentation fault sprouts where variable-length arrays are used in a project. I want to remove them all, how to make GCC display every declaration it finds of a variable-length array? I have tried using…
lvella
  • 12,754
  • 11
  • 54
  • 106
4
votes
1 answer

Trouble with VLA's in C

I'm running Xcode 7.1 on Mac OS X 10.11. I'm trying to declare a VLA array in C but I can't do it. The second I use a variable in the array declaration, it's moot. The array doesn't get created. I've dug around in the compiler settings, tried…
David Tamrazov
  • 567
  • 1
  • 5
  • 16
4
votes
2 answers

reference to an array of size determined at run-time

I tried to find this but can't find any. I know I can create a reference to an array variable: int x[10] = {}; int (&y)[10] = x; However, in the case that the array size is not known at compile time, like in the following code: const int n = atoi(…
yu quan
  • 161
  • 1
  • 1
  • 14
4
votes
2 answers

How to declare a variable length array in Visual Studio C89 following other code

I understand in VS all variables must be declared at the top of a block, but if I want a VLA, ie. if I wanted to do something like this: int result = runalgorithm(); int vla[result]; the code above is invalid, because vla must be declared at the…
dave
  • 41
  • 1
  • 3
4
votes
1 answer

Segmentation fault when jumping to goto over VLA array

The following example demonstrates the issue: #include int main() { unsigned int remaining=1; goto loop; while(remaining) { unsigned char tmp[remaining]; printf("&tmp:…
Nuclear
  • 1,316
  • 1
  • 11
  • 17
4
votes
2 answers

using restrict qualifier with C99 variable length arrays (VLAs)

I am exploring how different implementations of simple loops in C99 auto-vectorize based upon the function signature. Here is my code: /* #define PRAGMA_SIMD _Pragma("simd") */ #define PRAGMA_SIMD #ifdef __INTEL_COMPILER #define ASSUME_ALIGNED(a)…
4
votes
2 answers

C++ allows me to allocate an array on run time as opposed to giving an error

I read that array size needs to be known at compile-time. However, when I do this, it compiles and runs just fine without giving any errors...how come? #include int main() { int size; std::cout << "Enter size: "; std::cin >>…
Southee
  • 69
  • 3