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.
0
votes
2answers
33 views
Array[0] changing when entering for loop, can't figure out why
I'm doing a challenge on HackerRank got the method figured out but there's a slight error I cannot figure out. Further information if needed is https://www.hackerrank.com/challenges/sparse-arrays
...
-2
votes
1answer
15 views
VLA works on gcc-4.9.2 but not on gcc-6.2.0
A very simple code to test VLA on gcc works fine on gcc-4.2.9 (Raspbian), but not on gcc-6.2.0 (Ubuntu). I am surprised. Though it compile without error, the output is not proper.
Code:
int len, ...
1
vote
0answers
30 views
Referencing an arrayLen in structure Coldfusion
How do I reference an arrayLen? The code is returning an error saying the variable is not set when I clearly set it in the beginning of the structure? Do I have to specify something for the key?
<...
0
votes
4answers
69 views
Returning a variable length array from a function
The following function "increment" add 1 to a number represented as an array.
int* increment(int array[], int size, int *sizeLen)
{
int temp[size+1];
int carry = 0;
carry = (array[size-...
4
votes
4answers
203 views
Can i make a dynamic array this way?
So my code is this :
int a, b;
printf("Give dimensions\n");
scanf("%d %d", &a, &b);
double array1[a][b];
printf("Give values\n");
int i, j;
for(i=0; i<a; i++)
{
for(j=0; j<b; j++)
...
0
votes
1answer
67 views
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 ...
2
votes
1answer
39 views
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 ...
7
votes
1answer
77 views
Cast “pointer to const” to “pointer to const VLA”
In this snippet, a pointer to VLA is used for easier access to a big lookup table :
#pragma GCC diagnostic warning "-Wcast-qual"
char
lookup(int a, int b, int c, char const *raw, int x, int y, int z)...
-1
votes
1answer
31 views
TypeError: cannot read property 'length' of undefined (defined by function parameter)
I'm trying to make the simple code below works, but always got the following error: TypeError: cannot read property 'length' of undefined.
function multiplyAll(arr) {
var product = 1;
if ...
2
votes
6answers
45 views
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 ...
0
votes
2answers
49 views
How to create variable length array on heap? [duplicate]
I used C's variable length array to implement an algorithm:
int matrix[rows][cols];
I managed to test that this does fail for ridiculous dimensions. Is there a way to allocate this matrix on heap ...
1
vote
1answer
140 views
Variable Length Arrays in C++14?
n3639 proposed the adoption of c99's variable-length-arrays into C++14 (at least for the first dimension.)
But the latest I've been able to find lists n3639 as:
Features in the first CD of C++14, ...
1
vote
2answers
66 views
Why does operator new can't construct multidimensional array of non-constant size?
I can write operator new for one-dimensional array as follows:
int n{3};
new int[n];
It allocates at least sizeof(int) * n bytes. But when I want to create two and more dimensional array, only first ...
1
vote
2answers
79 views
MATLAB: Loop through the values of a list from 'who' function
I have a long list of variables in my workspace.
First, I'm finding the potential variables I could be interested in using the who function. Next, I'd like to loop through this list to find the size ...
0
votes
2answers
71 views
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 ...
1
vote
0answers
49 views
Can I cast to VLA?
I know that variable array length is a compiler's extension, but am I allowed to cast to VLA?
Next example compiles fine with next command:
g++ -Wall -Wextra cast.cpp -ansi
Code follows:
#include ...
0
votes
2answers
141 views
Variable-Length Arrays in c programming [duplicate]
I want to create an variable-length array.
Here is my code:
#include <stdio.h>
int main (void)
{
int n,i, response;
while(true){
printf("Enter the number of responses:");
...
0
votes
2answers
75 views
printf changes my output
I'm trying to solve this challenge on Hackerrank.
I've reached a problem where I cannot proceed, but I can't see where I've gone wrong - and I'm hoping someone here can help.
My current solution is ...
-2
votes
3answers
47 views
How to find the position of an item of a list in python?
I have two lists. The first list has a specific length and the second list has unstable length. I want to assign the items from two lists.
I use colour_index = sel_pos%(len(colours)). The sel_pos is ...
69
votes
2answers
2k views
What does “[*]” (star modifier) mean in C? [duplicate]
While trying to implement a C11 parser (for educational purposes), I found that in C11 (p. 470) but also in C99 (p. 412) (thanks Johannes!), the direct declarator is defined as:
(6.7.6) direct-...
0
votes
2answers
61 views
Is creating array with a variable number of elements possible?
Whenever I need to create an array with a number of elements not known until execution time I do this.
int n, i;
printf("Number of elements: ");
scanf("%d", &n);
int myArray[n];
for(i = 0; i < ...
1
vote
1answer
340 views
Working with variable-length text in Tensorflow
I am building a Tensorflow model to perform inference on text phrases.
For sake of simplicity, assume I need a classifier with fixed number of output classes but a variable-length text in input. In ...
13
votes
5answers
504 views
Convert Python sequence to NumPy array, filling missing values
The implicit conversion of a Python sequence of variable-length lists into a NumPy array cause the array to be of type object.
v = [[1], [1, 2]]
np.array(v)
>>> array([[1], [1, 2]], dtype=...
1
vote
2answers
65 views
Initializing structure with variable length array in C
Does anyone know if there is a way to initialize a structure containing a variable length array without initializing the array first in a separate variable (and without using malloc)?
My structure ...
7
votes
1answer
124 views
Is it valid to initiate the size part of a VLA in the same sequencepoint as the VLA is declared?
In this post: http://stackoverflow.com/questions/38326930/cannot-read-full-100000-integer-values-from-a-file-in-c
The OP contains code where there is a lot wrong with, but 1 line made me especially ...
1
vote
3answers
73 views
code order with variable length array
In C99, is there a big different between these two?:
int main() {
int n , m;
scanf("%d %d", &n, &m);
int X[n][m];
X[n-1][m-1] = 5;
printf("%d", X[n-1][m-1]);
}
and:
...
2
votes
0answers
144 views
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++ ...
-1
votes
3answers
70 views
If len Statement
I type in seven digits to be able to work out a gtin-8 product code. But if I type in more than seven digits, the if len statement is meant to recognise that I have typed in more than seven digits, ...
-1
votes
1answer
67 views
Passing a two dimensional VLA into a function
I am working currently on an assignment, which requires me to use two dimensional arrays whose size is given by the user input.
Normally I tried to avoid VLAs as good as I can, since I know there are ...
-2
votes
3answers
66 views
Is this actually dynamic memory allocation? [duplicate]
I have this code
int e;
scanf("%d",&e);
int ar[e];
is this dynamic allocation? It really looks like it is as I can allocate memory at run time.
I used this to get input from user for the ...
-1
votes
3answers
51 views
From where do the initial values of the variable length array (VLA) in [c] come?
example c code:
#include <stdio.h>
int main (int argc, char** args) {
int k = 8;
int sendbuffer[k]; // VLA
for (int n = 0; n < k; n++) {
printf("sendbuffer[%i]: %i \n", ...
0
votes
6answers
129 views
When array size determined at run time , is it allocating from stack or heap?
In the below code,is the arr[n] is allocated from stack or heap?
I am confused since in general the array size is determined at compile time. How the below code working ?
#include<stdio.h>
int ...
0
votes
1answer
18 views
Variable length array storage space in c/c++
Below is my simple code snippet.
#include <iostream>
using namespace std;
bool testAllocArray(const unsigned int length)
{
char array[length]; //--------------------------(1)
return true;...
-1
votes
2answers
32 views
How to fix this function so that it returns concatenation of two strings? [duplicate]
I am trying to write a custom function in C that will concatenate two strings. So far, I have come up with:
char *strcat406(char *str1, char *str2) {
int str1length = 0;
int str2length = 0;
...
37
votes
3answers
2k views
C++ replacement for C99 VLAs (goal: preserve performance)
I am porting some C99 code that makes heavy use of variable length arrays (VLA) to C++.
I replaced the VLAs (stack allocation) with an array class that allocates memory on the heap. The performance ...
4
votes
1answer
52 views
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 ...
3
votes
2answers
91 views
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 (...
0
votes
3answers
72 views
variable length two dimensional array c++
Why is this code compiling successfully, without any warning:
int m,n;
cin >> m >> n;
int arr[m][n];
[compiled with MinGW with Code::Blocks]
Shouldn't atleast one of m or n must be ...
17
votes
3answers
851 views
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 ...
0
votes
3answers
62 views
How to define array size based upon number of inputs?
As far as my knowledge, (im a beginner in c) you can define the size of an array provided the user knows how many inputs the user is going to provide. But, how can i define the size of an array based ...
11
votes
2answers
226 views
Passing a multidimensional array of variable size
I'm trying to understand what "best practice" (or really any practice) is for passing a multidimensional array to a function in c is. Certainly this depends on the application, so lets consider ...
1
vote
3answers
100 views
The unknown size of array
In Java, I am trying to read files and then I want to put them in an array. But when I declare an array, an error occurs because of unknown length. Here's an example:
Object unsortedInt[];
...
1
vote
1answer
85 views
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 ...
1
vote
2answers
104 views
C++1z(?) runtime sized array as a return value
Can we not do anything like this?
auto f(int n, char *arr[]) -> decltype(char *[n]) /* doesn't work */ {
char *tmp[n]; // RSA, the size depends on argc
for (int i = 0; i < n; ++i) tmp[i]...
9
votes
3answers
170 views
Why Are Zero Length VLAs UB?
The 2011 standard explicitly states...
6.7.6.2 Array declarators
If the size is an expression that is not an integer constant expression: if it occurs in a
declaration at function ...
4
votes
1answer
57 views
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 ...
0
votes
2answers
83 views
only read the length of variable-length array without reading the elements in hdf5
Is it possible to do this?
I have an array containing Nvl HDF5-variable-length-arrays already written to a hdf5 file. I can read in the entire data by (the following snippet is in c++, but answers ...
-2
votes
1answer
251 views
C compile error Variable-sized object may not be initialized with char * ptr[buflen]
Yes I did read these two posts.
C compile error: "Variable-sized object may not be initialized"
C error "variable-sized object may not be initialized"
My case is a bit different ...
3
votes
2answers
128 views
Is it required that a C Variable Length Array is allocated from the stack?
After removing all the calls to malloc and calloc from our code for an embedded system, I was surprised to find that malloc was still being linked in. The call graph pointed me to a function which ...
-4
votes
2answers
77 views
Where does variable length array/alloca allocate in stack
I am really curious about how alloca() function works and therefore, I have written a simple test program as follows:
int test() {
int a = 0;
int e;
char tmp2[a]; //alloca
int d;
...