flexible-array-member

Flexible array members can lead to undefined behavior?

浪尽此生 提交于 2019-12-02 18:50:30
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 strictly conforming program? Is the offset of the flexible array member required to be at the end of the struct? The questions apply to both C99 (TC3) and C11 (TC1) . #include <stdio.h> #include <stdlib.h> #include <stddef.h> int main(void) { struct s { size_t len; char pad; int array[]; }; struct s *s = malloc(sizeof *s + sizeof *s->array); printf("sizeof *s: %zu\n", sizeof *s); printf("offsetof(struct s,

How does an array of structures with flexible array members behave?

主宰稳场 提交于 2019-12-01 17:51:48
问题 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 says: The sizeof operator on such a struct is required to give the offset of the flexible array member. On my machine this corresponds to 8 bytes ( sizeof(size_t) ). But what happens, when I do the following: Obviously the array cannot hold the data of vector v0 , since it's only 3*8 bytes = 24 bytes wide.

Flexible Array Member (Zero Length Array) [duplicate]

随声附和 提交于 2019-11-30 09:03:54
问题 This question already has answers here : Array of zero length (5 answers) Closed 5 years ago . In reference to GCC's Zero Length Array explanation: This is particularly useful in the case when a struct is a header for a variable-length object. This is exactly my case. Furthermore, I am concerned with the alignment of my structs in the heap. In this case, I still really do not understand what's useful about zero length arrays. How are they related to this particular situation? EDIT: Is it that

Is it legal to implement inheritance in C by casting pointers between one struct that is a subset of another rather than first member?

不打扰是莪最后的温柔 提交于 2019-11-29 22:21:50
问题 Now I know I can implement inheritance by casting the pointer to a struct to the type of the first member of this struct . However, purely as a learning experience, I started wondering whether it is possible to implement inheritance in a slightly different way. Is this code legal? #include <stdio.h> #include <stdlib.h> struct base { double some; char space_for_subclasses[]; }; struct derived { double some; int value; }; int main(void) { struct base *b = malloc(sizeof(struct derived)); b->some

flexible array member in a nested struct

孤街浪徒 提交于 2019-11-29 14:32:37
Is it valid C code to have flexible array members inside nested structs? So is my sample code below guarenteed to work as expected with a sane compiler? #include <stdio.h> #include <stdlib.h> struct d { char c; int ns[]; }; struct c { struct d d; }; struct b { struct c c; }; struct a { int n; struct b b; }; int main() { const int n = 10; struct a *pa = malloc(sizeof(*pa) + n * sizeof(pa->b.c.d.ns[0])); pa->n = n; pa->b.c.d.c = 1; for (int i = 0; i < n; ++i) { pa->b.c.d.ns[i] = i; } for (int i = 0; i < n; ++i) { printf("%d\n", pa->b.c.d.ns[i] + pa->b.c.d.c); } free(pa); } It's not valid per the

Flexible Array Member (Zero Length Array) [duplicate]

最后都变了- 提交于 2019-11-29 11:13:40
This question already has an answer here: Array of zero length 5 answers In reference to GCC's Zero Length Array explanation: This is particularly useful in the case when a struct is a header for a variable-length object. This is exactly my case. Furthermore, I am concerned with the alignment of my structs in the heap. In this case, I still really do not understand what's useful about zero length arrays. How are they related to this particular situation? EDIT: Is it that I can put as much "data" as I want in there? Basically it allows you to have a structure with a variable length array at the

Are flexible array members really necessary?

孤人 提交于 2019-11-29 01:57:34
A struct with a flexible array member, apparently, is not intended to be declared, but rather used in conjunction with a pointer to that struct. When declaring a flexible array member, there must be at least one other member, and the flexible array member must be the last member in that struct. Let's say I have one that looks like this: struct example{ int n; int flm[]; } Then to use it, I'll have to declare a pointer and use malloc to reserve memory for the structure's contents. struct example *ptr = malloc(sizeof(struct example) + 5*sizeof(int)); That is, if I want my flm[] array to hold

Allocating struct with flexible array member

半世苍凉 提交于 2019-11-28 11:22:25
This is C99 code: typedef struct expr_t { int n_children; foo data; // Maybe whatever type with unknown alignment struct expr_t *children[]; } expr_t; Now, how do I allocate memory ? expr_t *e = malloc (sizeof (expr_t) + n * sizeof (expr_t *)); or expr_t *e = malloc (offsetof (expr_t, children) + n * sizeof (expr_t *)); ? Is sizeof even guaranteed to work on an type with flexible array member (GCC accepts it) ? CrazyCasta expr_t *e = malloc (sizeof (expr_t) + n * sizeof (expr_t *)); is well defined in C99. From the C99 specification 6.7.2.1.16: As a special case, the last element of a

flexible array member in a nested struct

寵の児 提交于 2019-11-28 08:21:41
问题 Is it valid C code to have flexible array members inside nested structs? So is my sample code below guarenteed to work as expected with a sane compiler? #include <stdio.h> #include <stdlib.h> struct d { char c; int ns[]; }; struct c { struct d d; }; struct b { struct c c; }; struct a { int n; struct b b; }; int main() { const int n = 10; struct a *pa = malloc(sizeof(*pa) + n * sizeof(pa->b.c.d.ns[0])); pa->n = n; pa->b.c.d.c = 1; for (int i = 0; i < n; ++i) { pa->b.c.d.ns[i] = i; } for (int i

Are flexible array members really necessary?

為{幸葍}努か 提交于 2019-11-27 16:10:42
问题 A struct with a flexible array member, apparently, is not intended to be declared, but rather used in conjunction with a pointer to that struct. When declaring a flexible array member, there must be at least one other member, and the flexible array member must be the last member in that struct. Let's say I have one that looks like this: struct example{ int n; int flm[]; } Then to use it, I'll have to declare a pointer and use malloc to reserve memory for the structure's contents. struct