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
32
votes
8 answers

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=object) Trying to force another type will cause an…
Marco Ancona
  • 2,073
  • 3
  • 22
  • 37
28
votes
3 answers

Variable-length std::array like

As my usually used C++ compilers allow variable-length arrays (e.g. arrays depending on runtime size), I wonder if there is something like std::array with variable size? Of course std::vector is of variable size, but it allocates on heap, and…
dronus
  • 10,774
  • 8
  • 54
  • 80
25
votes
3 answers

Is the operand of `sizeof` evaluated with a VLA?

An argument in the comments section of this answer prompted me to ask this question. In the following code, bar points to a variable length array, so the sizeof is determined at runtime instead of compile time. int foo = 100; double…
PC Luddite
  • 5,883
  • 6
  • 23
  • 39
24
votes
3 answers

Variable length array in the middle of struct - why this C code is valid for gcc

There is some strange code using VLA (Variable Length Arrays) which is treated as Valid C (C99, C11) by gcc 4.6: $ cat a.c int main(int argc,char**argv) { struct args_t{ int a; int params[argc]; // << Wat? …
osgx
  • 90,338
  • 53
  • 357
  • 513
23
votes
5 answers

How can I initialize an array without knowing its size?

I have a situation, where I have to apply a criteria on an input array and reuturn another array as output which will have smaller size based upon the filtering criteria. Now problem is I do not know the size of filtered results, so I can not…
Space Rocker
  • 787
  • 3
  • 11
  • 25
23
votes
3 answers

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 array as such: int col = 10; int row = 10; char…
etayluz
  • 15,920
  • 23
  • 106
  • 151
21
votes
4 answers

Dynamic arrays in C without malloc?

I've always wondered how I could get away with this: int main(int argc, char **argv) { printf("%p %s %d\n", &argv[1], argv[1], strlen(argv[1])); char copy[strlen(argv[1]) + 1]; strcpy(copy, argv[1]); printf("%p %s %d\n", ©, copy,…
lt0511
  • 213
  • 1
  • 2
  • 4
21
votes
1 answer

VLAs and side-effect in sizeof's operand

I know that sizeof never evaluates its operand, except in the specific case where said operand is a VLA. Or, I thought I knew. void g(int n) { printf("g(%d)\n", n); } int main(void) { int i = 12; char arr[i]; // VLA (void)sizeof…
Quentin
  • 62,093
  • 7
  • 131
  • 191
21
votes
3 answers

Initializing variable length array

On initializing a Variable length array compiler gives an error message: [Error] variable-sized object may not be initialized Code snippet: int n; printf("Enter size of magic square: "); scanf("%d",&n); int board[n][n] = {0}; How should…
haccks
  • 104,019
  • 25
  • 176
  • 264
18
votes
1 answer

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: #include int main() { /* Read a size…
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
17
votes
1 answer

Is it safe to use variable-length arrays?

I have a concern about variable-length arrays. When I want to allocate an array dynamically, I'll get null, if it is not possible to allocate enough memory and I can respond to this properly in my program. With a variable length array I don't get…
gruszczy
  • 40,948
  • 31
  • 128
  • 181
15
votes
3 answers

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

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 me, the size is also unknown in the first case, as it…
Floella
  • 1,279
  • 1
  • 22
  • 41
14
votes
2 answers

Why is there a number 22 in GCC's implementation of a VLA (variable-length array)?

int read_val(); long read_and_process(int n) { long vals[n]; for (int i = 0; i < n; i++) vals[i] = read_val(); return vals[n-1]; } The assembly language code compiled by x86-64 GCC 5.4 is: read_and_process(int): pushq …
lastans7
  • 143
  • 5
14
votes
2 answers

Variable Length Array overhead in C++?

Looking at this question: Why does a C/C++ compiler need know the size of an array at compile time ? it came to me that compiler implementers should have had some times to get their feet wet now (it's part of C99 standard, that's 10 years ago) and…
Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
14
votes
5 answers

Can we have a struct element of type Variable length array?

Can we declare a structure element of variable length? The condition is as follows: typedef struct { uint8_t No_Of_Employees; uint8_t Employee_Names[No_Of_Employees][15]; }st_employees;
user12345
  • 357
  • 2
  • 3
  • 11
1
2
3
27 28