Skip to main content
Filter by
Sorted by
Tagged with
6 votes
1 answer
241 views

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 ...
Peter Sutton's user avatar
  • 1,295
3 votes
5 answers
194 views

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[];...
Donal Fellows's user avatar
3 votes
2 answers
178 views

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 ...
stefan's user avatar
  • 43
2 votes
1 answer
68 views

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; ...
Marek P's user avatar
  • 21
2 votes
2 answers
170 views

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 ...
Quantasm's user avatar
4 votes
2 answers
136 views

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 ...
Paul J. Lucas's user avatar
0 votes
3 answers
117 views

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 ...
return true's user avatar
  • 7,926
1 vote
0 answers
91 views

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 ...
ptan9o's user avatar
  • 304
0 votes
1 answer
327 views

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 ...
Yubikiri773's user avatar
0 votes
2 answers
137 views

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 ...
Dmytro Ovdiienko's user avatar
0 votes
1 answer
113 views

#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 =...
EthanLee's user avatar
0 votes
2 answers
80 views

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(...
testing09's user avatar
  • 155
1 vote
1 answer
125 views

typedef struct nestedStruct { int a; int b; char* name; } nestedStruct; typedef myStruct { int count; nestedStruct myNestedStruct[]; } I need to define flexible structure which ...
Anonymous's user avatar
  • 751
0 votes
0 answers
116 views

#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 ...
q0987's user avatar
  • 36.2k
0 votes
0 answers
118 views

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 ...
deltakid0's user avatar
1 vote
2 answers
94 views

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[] }; ...
g3t0r's user avatar
  • 13
2 votes
0 answers
174 views

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__((...
Alexis Wilke's user avatar
  • 21.2k
2 votes
2 answers
281 views

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; ...
BobDeTunis's user avatar
0 votes
2 answers
720 views

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"}, {...
David D.'s user avatar
2 votes
2 answers
93 views

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",...
Ravi's user avatar
  • 51
0 votes
2 answers
683 views

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 ...
linguini's user avatar
-1 votes
4 answers
125 views

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. #...
thejohnofbeingjohn's user avatar
9 votes
1 answer
283 views

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....
Pascal Cuoq's user avatar
  • 80.6k
2 votes
1 answer
292 views

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 ...
Angelo Amadei's user avatar
3 votes
1 answer
178 views

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 ...
user877329's user avatar
  • 6,296
0 votes
1 answer
630 views

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 ...
Zeta-Squared's user avatar
1 vote
2 answers
173 views

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 ...
Tony Yuen's user avatar
1 vote
2 answers
240 views

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 ...
amont's user avatar
  • 101
0 votes
0 answers
36 views

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", ...
Mike's user avatar
  • 103
1 vote
1 answer
246 views

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 ...
Dave M.'s user avatar
  • 1,537
1 vote
2 answers
1k views

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 { ...
mmxjohnson's user avatar
3 votes
1 answer
133 views

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 ...
pmor's user avatar
  • 6,757
1 vote
4 answers
1k views

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 ...
morimn's user avatar
  • 605
8 votes
1 answer
7k views

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 ...
Jin Kwon's user avatar
  • 22.4k
2 votes
3 answers
183 views

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 ...
étale-cohomology's user avatar
10 votes
5 answers
2k views

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 ...
Jérôme Pouiller's user avatar
1 vote
0 answers
1k views

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 } }; ...
Fayeure's user avatar
  • 1,483
1 vote
1 answer
180 views

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 ...
HenkJanSnot's user avatar
0 votes
1 answer
173 views

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; ...
Rain's user avatar
  • 105
4 votes
4 answers
1k views

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 ...
Łukasz Przeniosło's user avatar
2 votes
1 answer
2k views

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::...
Helloer's user avatar
  • 467
0 votes
0 answers
353 views

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 ...
Dan's user avatar
  • 39
0 votes
1 answer
425 views

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 ...
Tech333's user avatar
  • 41
3 votes
4 answers
944 views

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 ...
RobertS supports Monica Cellio's user avatar
0 votes
0 answers
87 views

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 ...
poloDD's user avatar
  • 71
2 votes
2 answers
612 views

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 ...
Jimmy T.'s user avatar
  • 4,199
0 votes
1 answer
106 views

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 ...
Akshdeep Singh's user avatar
0 votes
0 answers
500 views

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 ...
stev's user avatar
  • 93
0 votes
1 answer
168 views

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 { ...
St.Antario's user avatar
  • 27.7k
5 votes
2 answers
6k views

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}; }; ...
tohaz's user avatar
  • 197