146 questions
6
votes
1
answer
241
views
How do I create and use a stack-allocated struct ending with a C99 flexible array member in C++ without undefined behaviour?
I'm calling a 3rd-party C99 library from modern C++. The API contains a struct that ends with a flexible array member. To allocate the struct on the stack I think I need a byte buffer then cast it to ...
3
votes
5
answers
194
views
Using offsetof to get allocation size
I have code that's got declarations like this:
typedef struct {
int a;
int b;
double c;
double d;
} Element;
typedef struct {
size_t used;
size_t capacity;
Element items[];...
3
votes
2
answers
178
views
Flexible array member issues with alignment and strict aliasing
struct Node {
Node *left;
Node *right;
int height;
char data[];
};
This is how I used to define my data structure nodes, I find it very useful because I can embed the data directly in the ...
2
votes
1
answer
68
views
What's the JNA mapping for 'unsigned char data[1]'?
I'm trying to write JNA wrappers for LibRaw and I'm stumbling over one particular C structure:
typedef struct
{
enum LibRaw_image_formats type;
ushort height, width, colors, bits;
...
2
votes
2
answers
170
views
Error: non-static initialization of a flexible array member in Some Cases but Not Others
I'm using the latest version of g++ to compile some test code involving an array of struct pointers where the structs contain an array. Compilation fails when I try to initialize the array of struct ...
4
votes
2
answers
136
views
Treat array as if it were a flexible array member? Undefined behavior?
Ideally, I want to have something like this:
struct S {
int other_member; // not relevant to question
// ...
union {
void *ptr;
char buf[];
};
};
That is, a struct with either a ...
0
votes
3
answers
117
views
Using array literal in macro
I have a struct like this:
typedef struct {
int num_files;
audio_file files[];
} message_files;
and I want to create a macro so I don't have to specify the number of files of a statically known ...
1
vote
0
answers
91
views
Why can't I get around 'error: flexible array member' with fixed size union? [duplicate]
Can someone explain why this doesn't work? Below is a simple example of what I'm trying to do. I don't need an explanation on error: flexible array member. I understand that if int g[] is not at the ...
0
votes
1
answer
327
views
How to resolve the "unknown array size in delete" error when using a class with a flexible array member (FAM) and a non-trivial destructor? [duplicate]
struct A {
~A () {}
};
struct S {
S() : i(0) { }
~S() {}
int i;
// This fails:
//
// A a[];
A a[0];
};
int main()
{
struct A aaa;
return 0;
}
Note that if the zero-sized ...
0
votes
2
answers
137
views
Calculate the size of the non-standard-layout structure with open array
It seems it is a trivial task, but I cannot find the way to do it properly without UBs and still have a compile time validation.
I need to implement the linked list of different types. The problem is ...
0
votes
1
answer
113
views
Why is the dst pointer changed when using memcpy function for flexible array in struct of this case?
#include <cstring>
#include <cstdio>
#include <cstdlib>
struct Block {
int32_t size;
u_int8_t data[0];
};
int main(int argc, char **argv) {
Block block;
Block *ptr =...
0
votes
2
answers
80
views
Why can't I reassign flexible array member?
I have the following struct with a flexible array member. However, the code does not compile:
#define INITIAL_CAPACITY 5
typedef struct Object {
int size;
int elements[];
} Object;
int main(...
1
vote
1
answer
125
views
Initialize nested variable array members
typedef struct nestedStruct
{
int a;
int b;
char* name;
} nestedStruct;
typedef myStruct
{
int count;
nestedStruct myNestedStruct[];
}
I need to define flexible structure which ...
0
votes
0
answers
116
views
sizeof(char[]) in a struct member with no size specified [duplicate]
#include <iostream>
struct inotify_event {
int wd;
uint32_t mask;
uint32_t cookie;
uint32_t len;
char name[];
};
struct inotify_event_no_name {
int wd;
uint32_t mask;
uint32_t ...
0
votes
0
answers
118
views
Why does GMP use a pointer at the end of struct instead of zero sized array or Flexible Array Member?
Most of the sites I've been reading state that a zero-sized array, single element array or Flexible Array Members is the way to go when a dynamic array is needed, but the definition of an integer in ...
1
vote
2
answers
94
views
Allocate array of structs with flexible array member, each member of the same isze
I'd like to ask about your opinion for a code I've figured out.
Context
I'm implementing a queue as a ring buffer. Each queue member is a struct below:
struct member {
int size;
char data[]
};
...
2
votes
0
answers
174
views
How do you avoid flexible array errors when you want to extend a structure with another in C/C++?
I found this structure definition used to handle detection of process changes in Linux:
struct __attribute__((aligned(NLMSG_ALIGNTO))) event_message {
nlmsghdr f_nl_hdr;
struct __attribute__((...
2
votes
2
answers
281
views
How to correctly use flexible array member?
Here is a type I declared:
(I declared t_sphere, t_cylinder and t_triangle too)
typedef struct s_intersection{
double t1;
double t2;
int id;
union {
t_sphere sph;
...
0
votes
2
answers
720
views
Is there a standard way to statically initialize flexible array members in C?
I need a statically created data structure in memory comprised of a table of string vectors, effectively:
typedef struct {
char *argv[];
} Entry;
const Entry Table[] = {
{"a"},
{...
2
votes
2
answers
93
views
Using sizeof on Flexible member array's first element before memory allocation is Undefined Behaviour?
If we have structure with flexible member array like :-
struct test{
int n;
int b[];
};
Then even before malloc is done, If we try to print like :-
struct test t;
printf("%lu",...
0
votes
2
answers
683
views
Assigning values to flexible array member of struct in C
I have a struct representing a shape in C with a flexible array member of vertices. I have a function which should return a square Shape. I have tried to do this several ways, and I keep getting ...
-1
votes
4
answers
125
views
Function to generate struct instances in c
How do I write a function that generates an instance of a previously defined struct every time it's called?
I'm sure since it's an easy problem no context is needed but here is what I have now.
#...
9
votes
1
answer
283
views
Is this C program with two struct definitions, involving a flexible array member, defined?
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 fam1....
2
votes
1
answer
292
views
Why single (flexible array) member struct declaration is only supported by [0] and not []?
Basically:
struct foo_1
{
int flexible_guy[0]; // this works
};
struct foo_2
{
int flexible_guy[]; // this doesn't (error: flexible array member in a struct with no named members)
};
Is this ...
3
votes
1
answer
178
views
Detect that a struct contains a flexible array member
Say that I have a struct like this
struct foo
{
int n;
int values[];
};
Is it possible to detect the flexible array member using SFINAE? At least, I can construct a class template that cannot ...
0
votes
1
answer
630
views
Understanding malloc and flexible array members
I am curious of how malloc() actually allocates memory. I am reading C programming by K.N.King for reference. In particular, chapter 17. Initially in the chapter void *malloc(size_t size) is described ...
1
vote
2
answers
173
views
C flexible array define with another type instead of malloc
the general usage of flexible array is to use malloc to define the flexible array. I'm trying to explore defining the flexible array with another struct. An example
typedef struct {
uint64_t ...
1
vote
2
answers
240
views
the difference between struct with flexible arrays members and struct with pointer members
I'm quit confused with the difference between flexible arrays and pointer as struct members. Someone suggested, struct with pointers need malloc twice. However, consider the following code:
struct ...
0
votes
0
answers
36
views
Initializer cannot be specified for a flexible array member [duplicate]
I'm trying to initialize a const flexible array as a member variable in a class, something like:
class MyListOfStrings
{
const std::string myStrings[] =
{
"String1",
...
1
vote
1
answer
246
views
What is a good pattern for array template compatibility?
I would like to create objects having a variable-length array of elements, and have them be compatible in a base/derived-class sense. In C, one could put an indeterminate array at the end of a struct ...
1
vote
2
answers
1k
views
how can i have a struct have an unknown size array member?
im trying to make a sort of minesweeper clone in c, but i cant figure out how to create a variable length array member of a struct and initialize it.
my struct definitions
typedef struct Cell
{
...
3
votes
1
answer
133
views
What is the rationale for "structure with flexible array member shall not be a member of a structure"?
C11, 6.7.2.1 Structure and union specifiers, Constraints, 3 (emphasis added):
A structure or union shall not contain a member with incomplete or function type (hence,
a structure shall not contain an ...
1
vote
4
answers
1k
views
How to get around zero-sized arrays?
In a library I'm working with I found a structure like this:
typedef struct {
uint16_t id;
uint16_t len;
union {
uint8_t data[0];
uint16_t data16[0];
uint32_t ...
8
votes
1
answer
7k
views
What is a flexible array member in a struct?
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 ...
2
votes
3
answers
183
views
Defererencing a pointer to a struct with a Flexible Array Member doesn't "copy" the FAM?
Defererencing a pointer to a struct with a Flexible Array Member (FAM) doesn't "copy" the FAM?
That seems to be the behavior, according to this program, which creates as instance of a struct ...
10
votes
5
answers
2k
views
How to allocate a struct with a flexible array member on the stack
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 ...
1
vote
0
answers
1k
views
Initialization of flexible array member is not allowed
I found a GNU C documentation on flexible arrays, and they say that you can initialize them like that:
struct foo { int x; int y[]; };
struct bar { struct foo z; };
struct foo a = { 1, { 2, 3, 4 } }; ...
1
vote
1
answer
180
views
Incomplete pointer array to struct. Stack smashing detected
My apologies in advance for the bad-looking code. It's just a proof of concept.
The purpose of the program is to fill the "datapacket", but by individually accessible pointers of the ...
0
votes
1
answer
173
views
How to initialize structure having unint_t as flexible array member? [duplicate]
I define a struct, have a one-member the type is uint8_t, this member store mac address.
Programming on Arduino IDE.
Struct:
typedef struct devInfo{
uint8_t address[];
unsigned int count;
...
4
votes
4
answers
1k
views
Flexible array member without having to be the last one
I am trying to figure out whether there is a workaround in C to have a flexible array member in a struct(s), that is not the last one. For example, this yields compilation error:
typedef struct ...
2
votes
1
answer
2k
views
std::make_unique with placement new
I'm trying to define a Data object that contains its size, followed by size bytes of data.
Something like:
struct Data {
size_t size;
char data[1];
static void* operator new( std::...
0
votes
0
answers
353
views
What is causing the warning "invalid use of structure with flexible array member" in C?
Okay so I've read every post about this but still don't get where's the issue. I'm coding in C.
I have this struct:
typedef struct{
int num;
char * palo[];
}tCarta;
When I try to compile, I ...
0
votes
1
answer
425
views
Python Ctypes 0 sized array?
I have look on many places for this, and have tried using cast, but had no success.
I have a struct in c like:
struct t{
int x;
struct y[0];
};
How would I resize the array of struct y? I tried the ...
3
votes
4
answers
944
views
Why is this initialization of a structure with a flexible array member invalid but valid with an fixed size array member?
The C standard states (emphasize mine):
21 EXAMPLE 2 After the declaration:
struct s { int n; double d[]; };
the structure struct s has a flexible array member d. [...]
22 Following the above ...
0
votes
0
answers
87
views
Why don't I need to allocate memory for a flexible array member inside a static structure?
Given this code :
typedef struct
{
int len;
char array[];
} bar_t;
typedef struct {
int foo;
bar_t bar;
} g_data_t;
static g_data_t g_data;
#define LEN 10
static void ...
2
votes
2
answers
612
views
Flexible array member in class with polymorphism
In C99 you can have something like
struct foo
{
int a;
int data[];
};
And then allocate with foo* f=(foo*)malloc(sizeof(foo)+n) to have a struct where the length of the array is n.
Can one ...
0
votes
1
answer
106
views
Why more memory (than being required) is allocated to a struct with flexible array member in cfg80211 scan request?
I am learning linux wifi drivers, and was exploring the code in cfg80211 subsytem for a scan request.
I can't understand why the following struct is allocated more memory than required.
Or, I can't ...
0
votes
0
answers
500
views
How to get the correct size of a const struct containing a const array in C
I am getting a wrong size of a struct containing a const char array.
Example (Arduino code):
#include <Streaming.h>
struct my_struct_t {
uint8_t len;
const char str[];
};
// Macro ...
0
votes
1
answer
168
views
Comparing 2 pointers to a struct with a flexible array member
QUESTION: Is it legitimate to compare 2 pointers with flexible array members for more then (less then) and equality?
Here is how the struct inotify_event is declared in Linux:
struct inotify_event {
...
5
votes
2
answers
6k
views
Flexible array as a class member
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};
};
...