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

stack space for a vector that its size is given at runtime? (C code)

Supose this C code: int main(){ int n; scanf("%d\n", &n); int a[n]; int i; for (i = 0; i
isma
  • 143
  • 1
  • 6
2
votes
5 answers

Can I use an integer variable to define an arrays length?

Can I use a variable to define an array's size? int test = 12; int testarr[test]; Would this work, I don't want to change the size of the array after initialization. The int test's value isn't known at compile time.
2
votes
2 answers

Point of initializing a pointer to an array with another array

This pointer to an array is initialized with another array. What is it trying to accomplish? float range_[] = {0, 180}; const float* range[] = {range_}; I'm trying to consolidate them into a class, and C++ is finicky about putting variable length…
Oren Bell
  • 460
  • 1
  • 5
  • 13
2
votes
5 answers

What is the best way to make 2 dimensional array in C

I want to make a 2 dimensional array in C. I know 1 way to make it like this. #include void my_func(int **arr) { printf("test2: %d\n", arr[0][1]); } int main(void) { const int row = 3; const int col = 4; …
2
votes
3 answers

How are Variable Length Array instructions generated?

Variable length arrays are supported in C: int main(){ int num = 5; int arr[num]; return 0; } I understand that arr is allocated during runtime. How is this accomplished? Does it call a C runtime function to allocate the byes? As the…
user13373366
2
votes
1 answer

Passing 2D array to a funciton in c & c++

I tried same program for C & C++ i.e. passing 2D array to a function. The following program is working in C, but not working in C++, Please explain why so ? IN C #include void pass(int n, int arr[][n]) // or void pass(int n, int…
2
votes
2 answers

Why does this OpenMP code work on Linux, but not Windows?

Edit: solved! Windows limits the stack size to where my buffer does not fit; linux does not (additionaly I was accessing memory outside of my array... oops). Using gcc, you can set the stack size like so: gcc -Wl --stack,N [your other flags n stuff]…
AnnoyinC
  • 426
  • 4
  • 17
2
votes
2 answers

VLA prototype and multidimensional array argument

I created a C99 VLA function as such : void create_polygon(int n, int faces[][n]); I want to call this function in another function where I would allocate my two-dimensional array : void parse_faces() { int faces[3][6]; create_polygon(6,…
explogx
  • 1,159
  • 13
  • 28
2
votes
5 answers

myArray[N] where N = 1,000,000 returns an error whereas myArray[1,000,000] doesn't

File extension: .cpp I have the following code: int main() { int N; cin >> N; int myArray[N]; return 0; } I get an error when I'm trying to run that program if I input N as 1,000,000. However, when I set myArray[N] to myArray[1000000],…
Richard
  • 7,037
  • 2
  • 23
  • 76
2
votes
1 answer

C99 How to cast simple pointer to a multidimensional-array of variable length?

In my function bodies reduce() and initialize(...) I want to work with the array globalArray as a three-dimensional one, where the dimensions are not known until runtime. How do I cast the globalArray pointer to a variable-length array (pointer) and…
NoahR
  • 1,417
  • 3
  • 18
  • 33
2
votes
2 answers

Return variable length array in Numpy C-extension?

I have made some Numpy C-extensions before with great help from this site, but as far as I can see the returned parameters are all fixed length. Is there any way to have a Numpy C-extension return a variable length numpy array instead?
c00kiemonster
  • 22,241
  • 34
  • 95
  • 133
2
votes
2 answers

How does C99 handle being unable to create a variable length array at run time?

I program embedded systems. One golden rule is that we never call malloc(); all data must be statically allocated at compile time. Hence, I am not really familiar with Variable Length Arrays, which were introduced with C99. The concept seems clear…
Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551
2
votes
1 answer

variable length multidimensional arrays

I did this program for a homework and it crashes without any error when run. Also after correcting it, any suggestions to increase the efficiency of my coding approach are appreciated. First I declared m,n,p,q as global variables an I passed only…
2
votes
6 answers

How to handle an dynamic array?

I want to show up the names of the members of an array "path" in my console. console.log("start: ", path[0].name, "second: ", path[1].name, "third: ", path[2]name, ....) But the problem is, that my array always changes it's size (clicking…
Derick Kolln
  • 633
  • 7
  • 17
2
votes
2 answers

What is the cost of creating large local arrays on the stack in C and C++?

Let's say I have a function called from within a tight loop, that allocates a few large POD arrays (no constructors) on the stack in one scenario, vs. I allocate the arrays dynamically once and reuse them in each iteration. Do local arrays add…
mojuba
  • 11,842
  • 9
  • 51
  • 72