variable-length-array

Segmentation fault when jumping to goto over VLA array

余生长醉 提交于 2021-01-27 06:40:33
问题 The following example demonstrates the issue: #include <cstdio> int main() { unsigned int remaining=1; goto loop; while(remaining) { unsigned char tmp[remaining]; printf("&tmp: %p\n",tmp); loop: remaining = 512;//or something else; } } Initially, the initialization of "remaining" variable was a bit long and I used goto to initialize it on one line. However, now this example gives segmentation fault on the printf line. It looks like the array is not initialized properly. Even gdb cannot print

Segmentation fault when jumping to goto over VLA array

血红的双手。 提交于 2021-01-27 06:39:40
问题 The following example demonstrates the issue: #include <cstdio> int main() { unsigned int remaining=1; goto loop; while(remaining) { unsigned char tmp[remaining]; printf("&tmp: %p\n",tmp); loop: remaining = 512;//or something else; } } Initially, the initialization of "remaining" variable was a bit long and I used goto to initialize it on one line. However, now this example gives segmentation fault on the printf line. It looks like the array is not initialized properly. Even gdb cannot print

Matrices as function parameters in C89

二次信任 提交于 2020-02-02 02:21:38
问题 For most of my undergrad C programming course, we studied C99 and our lecturer never bothered to teach us the main differences between C99 and previous versions. We have recently been informed that there's a possibility we'll be asked to implement solutions using C89 during our next exam, instead. My question regards the use of variable-length multidimensional arrays with regards to declaration and usage inside a function. In C99, I can have a function like this: void func(int cols, int mat[]

Undocumented GCC Extension: VLA in struct

纵然是瞬间 提交于 2020-01-19 05:42:23
问题 While reading the Clang documentation, I came across the following intriguing tidbit: [1] clang does not support the gcc extension that allows variable-length arrays in structures. This is for a few reasons: one, it is tricky to implement, two, the extension is completely undocumented, and three, the extension appears to be rarely used. Note that clang does support flexible array members (arrays with a zero or unspecified size at the end of a structure). How can this extension be used? My

Achieving the equivalent of a variable-length (local) array in CUDA

喜夏-厌秋 提交于 2020-01-14 14:36:14
问题 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 (which also depends on the input, at run-time, although before launch-time) - it can't be a fixed-size array. On the other hand, I can't write __global__ foo(short x) { int my_local_mem_array[x]; } (which is valid but problematic C99, but not valid C++ even on the host side.) How can I achieve the

VLA's memory available under gcc

帅比萌擦擦* 提交于 2020-01-06 08:43:11
问题 As malloc returns NULL, is there any way to detect that there is insufficient memory on the stack using VLA's? 回答1: There is nothing in C to guarantee the success of declaring a VLA or checking for failure regarding memory usage. This is the same for any declaration of an automatic object, VLA or not. 回答2: You can hope for a crash, but the worst case scenario is that things will seem to work and you'll end up writing to some other memory. At least gcc by default doesn't generate code that

Variable Length Array with length 0?

笑着哭i 提交于 2020-01-03 17:54:08
问题 In C, an array normally isn't allowed to have size 0 (unless I use the one or other compiler-side extension). OTOH, there are VLAs whose length might turn out to be 0. Are they allowed? I am talking about the following code: void send_stuff() { char data[4 * !!flag1 + 2 * !!flag2]; uint8_t cursor = 0; if (flag1) { // fill 4 bytes of data into &data[cursor] cursor += 4; } if (flag2) { // fill 2 bytes of data into &data[cursor] cursor += 2; } } The result is a data array with a length of 0, 2,

variable-length std::array like

孤者浪人 提交于 2019-12-30 02:45:08
问题 As my usually used C++ compilers allow variable-length arrays (eg. 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 reallocates on need. I like to have a stack allocated array with size defined at runtime. Is there any std -template that may feature this? Maybe using std::vector with a fixed maximal size? 回答1: There are two proposals currently being worked on to bring

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

大憨熊 提交于 2019-12-29 05:25:12
问题 This question already has answers here : Array of variable length in struct (3 answers) Closed last year . 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; 回答1: If coding in C99 or C11, you might want to use flexible array members (you don't give an explicit dimension, but you should have a convention about it at runtime in your head). typedef struct {

Variable length arrays (VLA) in C and C++

此生再无相见时 提交于 2019-12-28 05:40:08
问题 Possible Duplicate: Variably modified array at file scope I have some concepts about the VLA and its behavior that I need to clarify. AFIK since C99 it's possible to declare VLA into local scopes: int main(int argc, char **argv) { // function 'main' scope int size = 100; int array[size]; return 0; } But it is forbidden in global scopes: const int global_size = 100; int global_array[global_size]; // forbidden in C99, allowed in C++ int main(int argc, char **argv) { int local_size = 100; int