flexible-array-member Questions

2

Solved

GCC G++ 9 This code: class foo { int bar[] = {111, 123}; }; produces an error about initializer for flexible array. But this one: class foo { int bar[2] = {111, 123}; }; compiles as n...
Pedology asked 8/1, 2020 at 21:45

1

Solved

The fact that a struct with a flexible array member is a type with which a variable can be declared and to which sizeof can be applied leads to an unusual behavior in the following program. file fa...
Infantine asked 26/8, 2022 at 7:33

1

Solved

I'm writing array list in C. I defined an implementation specific struct against a general header. struct array_list_env { void **array; // pointer to the array size_t capacity; // length of the ...
Convergent asked 13/8, 2021 at 8:43

5

Let's say we have a struct ending with a flexible array member: struct foo { size_t len; uint8_t data[]; }; How to allocate this struct on the stack (ie. memory is automatically released at the ...
Valdes asked 29/6, 2021 at 9:18

10

Solved

In C99, you can declare a flexible array member of a struct as such: struct blah { int foo[]; }; However, when someone here at work tried to compile some code using clang in C++, that syntax di...
Percuss asked 10/12, 2010 at 19:55

2

I tried to convert something from using the struct hack to using a flexible array member, only to run into the following error message: error: invalid use of structure with flexible array member...
Neddy asked 8/6, 2014 at 17:28

3

Solved

I have declared a flexible array member in union, like this: #include <stdio.h> union ut { int i; int a[]; // flexible array member }; int main(void) { union ut s; return 0; } and co...
Darnelldarner asked 15/9, 2017 at 6:49

3

Solved

In C, the code char *c = "Hello world!"; stores Hello world!\0 in rodata and initializes c with a pointer to it. How can I do this with something other than a string? Specifically, I am trying ...
Blastosphere asked 26/9, 2019 at 1:17

1

I would like to implement this C code which uses a flexible array member (sometimes called the struct hack) in Rust: struct test { int key; int dataSize; int data[]; }; struct test* t = mallo...
Vetavetch asked 23/6, 2018 at 22:47

4

Solved

I have this working code: #import <stdlib.h> #import <stdio.h> typedef struct myarray { int len; void* items[]; } MYARRAY; MYARRAY *collection; void mypop(void** val) { puts(coll...
Betel asked 23/3, 2019 at 4:7

2

Solved

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 i...

5

Solved

I am working on refactoring some old code and have found few structs containing zero length arrays (below). Warnings depressed by pragma, of course, but I've failed to create by "new" structures co...
Alexandrina asked 17/11, 2008 at 6:54

7

I recently read that using flexible array members in C was poor software engineering practice. However, that statement was not backed by any argument. Is this an accepted fact? (Flexible array mem...
Col asked 29/10, 2008 at 14:21

1

Solved

Given struct Foo { uint32_t a; uint32_t b[]; }; What is sizeof(Foo)? Is it implementation-defined or undefined behaviour? Does the answer differ for C vs C++?
Breena asked 19/7, 2017 at 14:43

3

Solved

By using flexible array members (FAMs) within structure types, are we exposing our programs to the possibility of undefined behavior? Is it possible for a program to use FAMs and still be a strict...

2

Solved

Frankly, is such a code valid or does it produce UB? #include <stdio.h> #include <stdlib.h> #include <string.h> struct __attribute__((__packed__)) weird_struct { int some; uns...

4

Solved

I want to declare a struct with a flexible array member in it, and then use sizeof() on it. The prototype is: typedef struct { uint16_t length; uint8_t array[][2]; } FLEXIBLE_t; I then declare...
Ellette asked 29/1, 2017 at 16:23

5

Solved

In the Linux kernel code I found the following thing which I can not understand. struct bts_action { u16 type; u16 size; u8 data[0]; } __attribute__ ((packed)); The code is here: http://lxr...
Huberthuberto asked 1/2, 2013 at 9:42

2

Solved

As the title states I was wondering how arrays of C-structs with a flexible array member behaves. Here is an example: struct vector { size_t length; double array[]; }; The Wikipedia article sa...
Pulverize asked 20/3, 2016 at 19:36

1

Solved

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 ...
Georginegeorglana asked 29/11, 2015 at 2:30

6

Solved

The struct hack where you have an array of length 0 as the last member of a struct from C90 and C99 is well known, and with the introduction of flexible array members in C99, we even got a standard...
Bimonthly asked 29/11, 2013 at 16:47

4

Solved

I use sizeof to get size of a struct in C, but the result I got is unexpected. struct sdshdr { int len; int free; char buf[]; }; int main(){ printf("struct len:%d\n",(sizeof(struct sdshdr)))...
Genny asked 9/7, 2015 at 13:19

1

Solved

I have written the following basic code for a menu: typedef struct Menu { char* title; unsigned num_submenus; struct Menu *submenu[]; } Menu; Menu sub1 = {"Submenu 1", 0, {NULL}}; Menu sub2 = ...
Sachi asked 9/1, 2015 at 0:33

2

Is the following supposed to be valid for the declaration of table_type, specifically the e[]: struct table_type { unsigned int8 a; unsigned int8 b; unsigned int8 c; unsigned int8 d; un...
Behistun asked 8/7, 2014 at 19:21

3

Solved

After reading some posts related to flexible array member, I am still not fully understand why we need such a feature. Possible Duplicate: Flexible array members in C - bad? Is this a Flexible...
Prevot asked 1/12, 2013 at 8:24

© 2022 - 2024 — McMap. All rights reserved.