The results are in! See what nearly 90,000 developers picked as their most loved, dreaded, and desired coding languages and more in the 2019 Developer Survey.

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.

2
votes
3answers
80 views

Can't get array to change to be correct size

So I'm trying to make a new Array of different length in a separate method by passing in an array from the main method but I'm having trouble. Essentially what I'm trying to do is make it so my ...
1
vote
2answers
40 views

Referencing a yet-to-be-mentioned function parameter using the new-style function declarations

Compelled to use the variable length array feature for my auxiliary function that prints square matrices, I defined it as follows: void print_matrix(M, dim) unsigned dim; int M[dim][dim]; { ...
0
votes
0answers
12 views

How can I store different bit lengths in a file?

I have a problem with my code. I want to store numbers from 0 to 127 in bits, but I don't want to waste a number of bits as 0s in the beginning of the number, for some numbers. For example, storing ...
-2
votes
1answer
48 views

Passing pointer and then allocating variable length array to stack

Is it possible to allocate a variable length array to the stack in one function from another function? One way that works is to just allocate the largest possible size up front, but I'm wondering if ...
0
votes
2answers
42 views

What happens when I initialize an array having the size as a variable?

I want to know where is my array stored if it has variable size such as in the code below this is because in my textbook it says that during runtime memory is allocated to the heap to my understanding,...
2
votes
3answers
77 views

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 ...
1
vote
2answers
81 views

Multiple constructors with variable argument parameters

Given these two constructors: SomeClass(int... params) { // Do things } SomeClass(long... otherParams) { // Do other things } What happens when an object foo is instantiated? SomeClass foo = ...
1
vote
2answers
102 views

Is there “implicit” allocation of arrays in c++ by declaration?

I would like to allocate memory for arrays, when their sizes are not known until run time, in C++. I often use constant (compile-time) allocation; but would like to make progress towards "always" ...
0
votes
1answer
52 views

variable-length array in struct with TI compiler in C (socket programming)

I'm writing code that dealing with data received from udp in application layer. This is the function prototype used for receiving data from udp int my_recv_UDP(int s, void* mem, ...
-1
votes
2answers
68 views

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 ...
6
votes
2answers
285 views

Why do I need dynamic memory allocation if I can just create an array? [duplicate]

I was reading about dynamic memory allocation and static memory allocation and found the following about dynamic memory allocation: In the programs seen in previous chapters, all memory needs were ...
0
votes
3answers
45 views

Number of Variables in an array

I have a code; x = np.linspace(0, 12, 200, False, True, None) when I print x, I get; (array([ 0. , 0.06, 0.12, 0.18, 0.24, 0.3 , 0.36, 0.42, 0.48, 0.54, 0.6 , 0.66, 0.72, 0....
3
votes
1answer
66 views

Pointers to VLA's

As you may know, VLA's haves pros and cons and they are optional in C11. I suppose that the main reason to make VLA's optional is: "the stack can blow up": int arr[n]; /* where n = 1024 * 1024 * ...
2
votes
5answers
116 views

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,...
0
votes
0answers
19 views

static an dynamic array declaration [duplicate]

As I know static array used to known size at compile time: int arr[5]; While dynamic array used to unknown size at compile time but know at run-time as: int size; cin >> size; int* arr = new ...
2
votes
1answer
328 views

C++ variable-sized object may not be initialized

I have the following simple source #include <iostream> int main() { int nv; nv = 3; int arr[nv] = { 0, 2, 5 }; return 0; } When compiling with GCC on system 1 I get error: ...
11
votes
3answers
593 views

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

#include <iostream> 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 = ...
15
votes
3answers
733 views

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

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 ...
-1
votes
1answer
47 views

Segmentation fault 11: while trying to input B[1] using cin

I'm having a problem in running the following code. It is giving me a segmentation fault as a runtime error. #include <iostream> using namespace std; int main() { int n; cout << "...
1
vote
1answer
66 views

What is the difference between the parameter c[a][b] and c[][2]

Given the following two declarations, what is the difference? int foo ( int a, int b, int c[a][b] ) { int foo ( int a, int b, int c[][2] ) { I understand the top statement is a "Variable Length ...
-5
votes
2answers
72 views

Creating array with non constant sizes

I'm currently working on an assignment where I must find a way to output the longest common subsequence of two strings. In all of the other places I have found implementations of this code, they all ...
2
votes
1answer
152 views

sizeof operator in conjunction with variable-length array as function arguments

According to GNU's documentation on Arrays of Variable Length, one can use the sizeof operator for determining the size of a variable length array that was passed into a function: You can also use ...
-4
votes
1answer
59 views

Why variable sized arrays cause Wrong Answer on Codechef?

int n;cin>>n; int arr[n]{}; I have a small problem ,why is this decleration of array wrong?I have used it on Codechef several times until recently i got a WA! After this I declared array as, ...
1
vote
2answers
85 views

Do calling conventions prevent variable size return values?

As noted here, when returning variable-size data from a C function, you either: Pass a pointer and max length. Return a flag indicating if max is reached. Return a pointer to dynamically allocated ...
2
votes
2answers
178 views

If a compiler defines __STDC_NO_VLA__, does it still have to support flexible array members?

In C99, flexible array members (of a structure) and variable length arrays were mandatory parts of the standard — conforming C99 compilers (implementations) have to support them both. In C11, an ...
2
votes
2answers
90 views

Find the length of the array that was passed to you in Q#

I have an operation as follows to which the driver needs to send an array of qubits. operation myOp(qubits: Qubit[]) : () { // uses elements from the qubit array } How do I find the ...
1
vote
1answer
61 views

c, runtime buffer size by function argument

I'm writing a function that needs to test some external SPI flash memory. By accident during development, I used this code void __TO_FLASH__ slcTestCache(uint8_t len) { uint16_t wcrc = 0xFFFF, ...
0
votes
4answers
63 views

Define array globally with variable parameter in C

Here's the code: int EdgeCount = 0; int numOfEdges = 0; void addEdge() { // some code numOfEdges++; } int EdgeWeightArray[numOfEdges]; // error I want that global array with variable ...
1
vote
2answers
40 views

accessing variable length array after its memory should have been deallocated

I'm currently studying variable length array and automatic storage. I have the following code that allocate memory for an variable length array myArray inside function vla, and return a pointer to ...
0
votes
5answers
145 views

Arrays created in functions,compile time or run time?

When we create an array on the stack most compilers will want to know the size of the array which will be determined at compile time so generally we can't get a user to enter the size of the array ...
1
vote
2answers
126 views

Are conformant array parameters VLAs?

CERT's Secure Coding Standard includes an item (API05-C) which encourages the use of conformant array parameters, which is a recommendation I've implemented in a lot of my code (hidden behind a macro ...
3
votes
1answer
114 views

Return a malloc’ed matrix while being able to use subscript notation

I have an exercise where I am supposed to use fixed-size arrays and in/out parameters to do stuff on matrices (add, scanf, print, etc.), but I’d like to do it on arbitrary-length matrices and return ...
0
votes
1answer
36 views

Just for kicks: Creating/deep-copying multidimensional arrays in JavaScript and accounting for variable length?

I volunteer teaching coding to young girls to code and one of the more advanced ones was trying to use a 2D array for her JavaScript project, but was struggling with the concept of multidimensional ...
0
votes
0answers
29 views

Nested for loop saving last input not whole input

I'm back again with that C question heh. So I'm trying to get the user to input a whole 2d array (size, values, everything), with VLA arrays (im using the latest compiler). Everything is fine up until ...
0
votes
1answer
93 views

What is an expression of VLA type?

I was trying to understand the working of sizeof operator and I came across this question. Following is the code from that question #include <stdio.h> int main() { int i = 0; int a[i];...
13
votes
4answers
1k views

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 ...
0
votes
3answers
93 views

Can't get length of array/object in an Object of Javascript

box = { curBox: 0, boxes: document.getElementsByClassName('box'), size: this.boxes.length, //this one won't work orSize: Object.keys(this.boxes).length, //as well as this one preBox: ...
-1
votes
4answers
374 views

Is there a VLAs (variable length arrays) support workaround for VS2017?

So simple code like: int n; cin >> n; int s[n], p[2*(n-1)][3]; I have to translate to: int n; cin >> n; vector<int> s(n, 0); vector<vector<int>> p(2 * (n - 1), vector&...
4
votes
4answers
614 views

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

If I write variable length array as a locally, like this: #include <stdio.h> int main() { int i=1; int a[i]; printf("%zu",sizeof(a)); } It working fine in GCC compiler. But, If I ...
1
vote
0answers
507 views

Keras variable length input for regression

Everyone! I am trying to develop a neural network using Keras and TensorFlow, which should be able to take variable length arrays as input and give either some single value (see the toy example below)...
1
vote
3answers
94 views

C struct definition containing 2-dimensional array type

I want to include a variable length two-dimensional array pointer as part of a struct, but the c99 compiler gives me the following error on the Array anArray line: "flexible array member in otherwise ...
0
votes
1answer
155 views

Posting data from variable length 2 dimensional array

I am having some issues with posting data from a 2 dimensional array and echoing them. I have read several topics and tutorials on multidimensional array, but most of them used fixed length of ...
0
votes
3answers
66 views

How to initialize multidimentional array in C Programming

I am getting error when i am running this code int row1=2,col1=2; int mat1[row1][col1]= { {1,5}, {4,6} }; What is wrong with this code?? IDE: CodeBlocks error: variable-sized object ...
0
votes
2answers
194 views

Dynamic Arrays and Static Arrays Compile Time or Runtime? [duplicate]

I understand the basic concept of Static Arrays and dynamic arrays. The main difference between these two are one(Static Array) allocates memory at compile time, and the other at runtime. However, we ...
0
votes
1answer
29 views

Bug in jQuery array (i), when adding a +1 to a negative value

I guess I'm fighting a bug. I build a small carousel in jquery. It's more some boxs stacked on each other, that will change their z-index (jquery.css) when my user clicks the next and prev arrows. So ...
17
votes
1answer
706 views

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: #...
2
votes
1answer
105 views

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 ...
66
votes
5answers
4k views

How does the compiler allocate memory without knowing the size at compile time?

I wrote a C program that accepts integer input from the user, that is used as the size of an integer array, and using that value it declares an array of given size, and I am confirming it by checking ...
0
votes
2answers
33 views

Variable length string duplication in Python

The following short python script: var1 = '7f0000000000000000000000000000000000000000000000000000000000000002' var2 = '01' output = 'evm --code ' + var1 + var1 + var2 + ' run' print(output) Is ...
1
vote
1answer
65 views

Using variable to initialize an array in C++ [duplicate]

As C++ primer said, we can't use variable as the dimension of builtin array, so the following code did not work int length = 3; int array[length] = {0, 1, 2}; the error is error: variable-sized ...