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
3
votes
1 answer

Large VLA overflow

Based on a comment of someone in another thread: VLAs introduce more problems than they solve, because you never know if the declaration is going to crash for x being too large for the stack. This code will overflow because sizeof(a) is too…
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
3
votes
1 answer

Using VLAs in a C++11 environment

I got C-Code that already exists and that makes use of C99-style VLAs. Like this one: int foo(int n, double l[n][n], double a[n][n]); I'd like to include the headers in my C++11 project. As C++ doesn't allow these kind of constructs I'm using…
Scindix
  • 1,254
  • 2
  • 15
  • 32
3
votes
2 answers

Achieving the equivalent of a variable-length (local) array in CUDA

I have some code which uses local memory (I might have used registers, but I need dynamic addressing). Since the amount of memory I use depends on the input and on the number of threads in the block (which also depends on the input, at run-time,…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
3
votes
2 answers

Initializing array in cpp and padding with zeros

I'm new a c++, switched from matlab to run simulations faster. I want to initialize an array and have it padded with zeros. # include # include # include using namespace std; int main() { int nSteps = 10000; …
jarhead
  • 1,821
  • 4
  • 26
  • 46
3
votes
2 answers

Can I use a C Variable Length Array in C++03 and C++11?

C has a really cool feature called variable length arrays. Its available in C90 and above, and it allows deferring the size of the array until runtime. See GCC's manual 6.19 Arrays of Variable Length. I'm working in C++. At std=c++11, I'm catching a…
jww
  • 97,681
  • 90
  • 411
  • 885
3
votes
4 answers

Char with a variable length

Is it possible to define a char with a variable length? I have a char "name" (member of a struct named "person") with a length of 25 but I want it to be a variable length between the values 1 and 25, because I want to generate random strings of that…
Zombie
  • 321
  • 3
  • 6
  • 16
3
votes
1 answer

Is initializing a VLA with braces a GCC bug or extension?

With the following code in mind: int main() { int n = 3; int arr[n] = { 1, 2, 3 }; } GCC properly errors out in C99 mode error: variable-sized object may not be initialized and clang gives the same error in C++ mode. However in C++ mode,…
user3920237
3
votes
2 answers

How does the compiler resolve the address of variable declared after a variable-length array?

Suppose I have the following function, which makes use of a variable-length array: void func(int size) { int var1; int arr[size]; int var2; ... } How does the compiler determine the address of var2? The only way that I can think of…
barak manos
  • 29,648
  • 10
  • 62
  • 114
3
votes
1 answer

Assert the allocation of a variable-length array

I apologize for the possible duplicate (have not been able to find an answer to that): Do we need to ensure that the allocation of a variable-length array has completed successfully? For example: void func(int size) { int arr[size]; if (arr…
barak manos
  • 29,648
  • 10
  • 62
  • 114
3
votes
3 answers

Dynamically allocated array of structures in C

I just wanted to know if the following works. I have a struct called foo that is defined as the following: struct foo { char name[255]; int amount; }; During runtime, I need to create an array of the above structures whose size is dependent…
user3059347
  • 529
  • 1
  • 9
  • 15
3
votes
4 answers

Allocation for multidimension array, partially of variable length, in C++

Let's say I have a multidimension array, which in C99 I could write like this: #define SIZE1 10 int size2; [...] int myArray[SIZE1][size2]; Although supported by several compilers, this is not strictly C++, and won't be included until C++14. To…
Antonio
  • 19,451
  • 13
  • 99
  • 197
3
votes
6 answers

What's the advantage of malloc?

What is the advantage of allocating a memory for some data. Instead we could use an array of them. Like int *lis; lis = (int*) malloc ( sizeof( int ) * n ); /* Initialize LIS values for all indexes */ for ( i = 0; i < n; i++ ) lis[i] = 1; we…
2
votes
3 answers

Is the sizeof operator evaluated differently for VLAs by gcc?

g++ allows Variable Length Arrays (VLA) as an extension. The results of sizeof operator on VLAs are interesting: int main () { char size = 20, a[10], b[size]; cout<<"sizeof(a) = "<
iammilind
  • 68,093
  • 33
  • 169
  • 336
2
votes
2 answers

Asking user to input the sizes of 2D array C program

I'm coding a C program that asks the user to input two variables that represent the 2d array sizes and then print the array, I don't know where the problem is! any help? `#include int main(void) { int n, x, i, j; int…
2
votes
1 answer

How do variable length arrays support numeric processing?

I am reading C The Complete Reference 4th edition by Herbert Schildt. In Chapter 4 - Arrays and Strings, it mentions "One major reason for the addition of variable-length arrays to C99 is to support numeric processing". My question is: What is…
Saad
  • 87
  • 6