Struck hack is used to allocate more memory than the initial need of the structure itself, so you can reference the part outside the array so that you stay inside the actually allocated memory.
Here's how it works.
struct Foo { // .. size_t size; int data[1]; }; const size_t SIZE = 100; Foo *p = (Foo*) malloc(sizeof(Foo) + sizeof(int) * (SIZE - 1)); p->size = SIZE; for (int i = 0; i < p->size; ++i) (p->data)[i] = i;
Question:
Is it possible to use only one integer, and not an array of size one? If feasible, why will a version of an array of size one become more popular?
struct Foo {
source share