flexible-array-member

How to access array of flexible arrays in cache friendly manner?

两盒软妹~` 提交于 2019-12-23 19:05:38
问题 I have records with flexible array member typedef struct record { unsigned foo; signed bar; double number[]; } record; I have multiple records with the same amount of numbers so I can arrange them in array. I would like to allocate them into one continuous memory space. const unsigned numbers = ...; const unsigned records = ...; const size_t record_size = sizeof(record) + numbers*sizeof(double); record *prec = malloc(records*record_size); So now I know record_size and I can access it but what

Flexible array member in C-structure

半世苍凉 提交于 2019-12-17 06:16:09
问题 Quoting from the C-std section 6.7.2.1, struct s { int n; double d[]; }; This is a valid structure declaration. I am looking for some practical use of this kind of syntax. To be precise, how is this construct any more or less powerful than keeping a double* as the 2nd element? Or is this another case of 'you-can-do-it-in-multiple-ways'? Arpan 回答1: The C FAQ answers precisely this question. The quick answer is that this structure will include the double array inside the structure rather than a

How to send array by value to function in c?

天涯浪子 提交于 2019-12-11 08:22:53
问题 I tried something like : typedef struct vec{ int sz; int v[]; } ff; int sum(struct vec z){ int o=0,i; for(i=0;i<z.sz;i++) o+=z.v[i]; return o; } int main(){ int test[]={10,1,2,3,4,5,6,7,8,9,10}; return sum((struct vec)test); } But this example code can't compile. How to send array by value (not ref throw pointer) to function? 回答1: In your example, you will need to specify the exact size of the array in the definition of struct vec , for example int v[10]. Also your initialization can be

Internal mechanism of sizeof in C?

走远了吗. 提交于 2019-12-10 00:34:51
问题 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))); return 0; } //struct len:8, with or without buf my question is why does buf not occupy any space and why is the size of the int type still 4 on a 64-bit CPU? here is the output from gcc -v : Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4

What are the real benefits of flexible array member?

旧城冷巷雨未停 提交于 2019-12-09 17:25:39
问题 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 Array Struct Members in C as well? (Blame me if I didn't solve my problem from the possible duplicate questions above) What is the real difference between the following two implementations: struct h1 { size_t len; unsigned char *data; }; struct h2 { size_t len; unsigned char data[]; }; I know the size

Size of a struct with flexible array member

Deadly 提交于 2019-12-08 20:19:45
问题 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++? 回答1: The compiler will ignore the flexible array member as it were not there. C11-§6.7.2.1 (p18) [...] In most situations, the flexible array member is ignored. In particular, the size of the structure is as if the flexible array member were omitted except that it may have more trailing padding than the omission would imply [...].

How to push and pop a void pointer in C

时光总嘲笑我的痴心妄想 提交于 2019-12-08 15:59:32
问题 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(collection->items[collection->len]); *val = collection->items[collection->len--]; } void mypush(void* val) { int len = collection->len++; collection->items[len] = val; puts(collection->items[len]); } int main() { puts("Start"); collection = malloc( sizeof *collection + (sizeof collection->items[0] * 1000) ); collection-

Internal mechanism of sizeof in C?

喜欢而已 提交于 2019-12-04 22:12:36
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))); return 0; } //struct len:8, with or without buf my question is why does buf not occupy any space and why is the size of the int type still 4 on a 64-bit CPU? here is the output from gcc -v : Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1 Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn) Target: x86_64-apple-darwin14.4

Struct hack equivalent in C++

此生再无相见时 提交于 2019-12-03 07:43:53
问题 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 standardized way of using it with [] . Unfortunately, C++ provides no such construct, and (at least with Clang 3.4 ), compiling a struct with either [0] or [] will yield a compilation warning with --std=c++11 -pedantic : $ cat test.cpp struct hack { char filler; int things[0]; }; $ clang++ --std=c++11

Struct hack equivalent in C++

限于喜欢 提交于 2019-12-02 20:28:29
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 standardized way of using it with [] . Unfortunately, C++ provides no such construct, and (at least with Clang 3.4 ), compiling a struct with either [0] or [] will yield a compilation warning with --std=c++11 -pedantic : $ cat test.cpp struct hack { char filler; int things[0]; }; $ clang++ --std=c++11 -pedantic test.cpp \test.cpp:3:14: warning: zero size arrays are an extension [-Wzero-length-array] int