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

Array of variable length in a structure

I've created 2 structures to represent images (a pixel and an image) in C. typedef struct pixel { unsigned char red; unsigned char green; unsigned char blue; }; typedef struct image { int width; int height; struct pixel…
moray95
  • 937
  • 2
  • 9
  • 12
9
votes
4 answers

declaring a variable-length array as a global variable in C

How is it possible to declare a variable-length array as a global variable ? when variable length array is declared in a function before the length is scanned, it compiles but does not run. it gives segmentation fault. when the same declaration…
KawaiKx
  • 9,558
  • 19
  • 72
  • 111
8
votes
4 answers

Why should global array size be an integer constant?

In C++ I tried declaring a global array of some size. I got the error: array bound is not an integer constant before ‘]’ token But when I declared an array of the same type in the main() function it is working fine. Why is there different…
8
votes
8 answers

Non-const declaration of array

I have been teaching myself programming for couple of years, and I was sure that if you need array declaration of a variable number you need to use malloc or new. Today I found that this compiles under g++ version 4.4.4, without warnings or…
8
votes
1 answer

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) { typedef char const (*DATA_PTR)[a][b][c]; …
diapir
  • 2,872
  • 1
  • 19
  • 26
8
votes
2 answers

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 had no explicit *alloc calls, and no calls to…
AShelly
  • 34,686
  • 15
  • 91
  • 152
8
votes
1 answer

How to convert C variable-length array code to Rust?

I know Rust doesn't support variable-length arrays, but that leaves me wondering what to replace them with, given that: I don't want to allocate and deallocate a tiny Vec in a loop The borrow checker doesn't let me move the code outside the…
Kornel
  • 97,764
  • 37
  • 219
  • 309
8
votes
2 answers

Are variable length arrays there in c++?

I had always thought that variable length arrays were not allowed in c++(Refer :Why aren't variable-length arrays part of the C++ standard?) .But than why does this code compile and work? #include using namespace std; int main () { …
7
votes
2 answers

What is the type of `1 ? 0 : (int(*)[f()])0`?

Probably, there is a contradiction is the C standard for VM types used in conditional operator. Assume: int f(void) { return 42; } Now, what is the type of a following expression? 1 ? 0 : (int(*)[f()]) 0 It's value must be equal to NULL but I am…
tstanisl
  • 13,520
  • 2
  • 25
  • 40
7
votes
2 answers

How many memory pages do C compilers on desktop OSes use to detect stack overflows?

This question is related to but different from this one about variable length arrays in C99. The answers point out that one danger with allocating variable length arrays (or just large arrays of a fixed size) in the stack is that the allocation may…
Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
7
votes
1 answer

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 * 1024 */ but what about pointer to VLA's? int m,…
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
7
votes
2 answers

Why can I goto into the scope of a alloca:d variable, but not a variable length array?

See this test program: #include #include int main(int argc, char *argv[]) { if (argc < 2) goto end; char s[strlen(argv[1]) + 1]; strcpy(s, argv[1]); printf("s=%s\n", s); end: return 0; } It fails to compile…
Tor Klingberg
  • 4,790
  • 6
  • 41
  • 51
7
votes
4 answers

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 other words, my mini batch would be a sequence of…
Marco Ancona
  • 2,073
  • 3
  • 22
  • 37
7
votes
1 answer

Is it valid to initiate the size part of a VLA in the same sequencepoint as the VLA is declared?

In this post, the OP contains code where there is a lot wrong with, but 1 line made me especially curios, since I wasn't able to look anything up, disallowing it. This is the specific line: int n = 100000, arr[n]; Is the order of declaration and…
dhein
  • 6,431
  • 4
  • 42
  • 74
7
votes
1 answer

Runtime sized arrays and pointer-decay

I was testing some of the tools in the type_traits header over the new C++14 runtime sized arrays, consider the code below: int g[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; template void print(T &t) { std::cout << "Type id: " <<…
PaperBirdMaster
  • 12,806
  • 9
  • 48
  • 94