Click here to Skip to main content
15,888,113 members
Articles / Programming Languages / C
Tip/Trick

C Struct Hack

Rate me:
Please Sign up or sign in to vote.
3.15/5 (9 votes)
2 Oct 2016CPOL1 min read 32.5K   8   14
This tip introduces struct hack and compares it with several implementation alternatives.

This tip introduces struct hack and compares it with several implementation alternatives. A typical C struct hack is a struct whose last element is an array of size one.

C++
struct Foo
{
  //..
  size_t size;
  // Better to use char array to be more portable, as discussed in comments.
  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;

The trick is to allocate more memory than sizeof (Foo), and make a Foo* point to it. The memory allocated is filled with a Foo object at the beginning, followed by an array of “dynamic” number of integers. You just reference the out-of-bounds part of the array such that you stay inside the memory actually allocated. That is, you can visit p->data[0] as well as p->data[1] and so on up until you hit the end of the memory you allocated. That said, you implement a flexible array member in C.

Why Not Use a Pointer?

C++
struct Foo
{
  // ..
  size_t size;
  // data = (int*)malloc(sizeof(int) * 10);
  int *data;
};

The advantage of using an array is that you don’t have to allocate the memory elsewhere and make the pointer point to that. Thus there is no extra memory management. Furthermore, accesses to the memory will hit the memory cache (much) more likely because dynamically allocated block is contiguous.

What About an Array of Size Zero?

You can't. Defining an array of constant size zero is illegal.

C++
struct Foo
{
  // ..
  size_t size;
  // Error: cannot allocate an array of constant size zero
  char data[0];
};

What About an int?

Yes, you can. But then you have to write more complex expression to access array elements. p->data[i] is more convenient and readable than (&p->data)[i], isn’t it?

C++
struct Foo
{
  size_t size;
  int data;
};

// 1. operator-> has a higher precedence than operator&
// 2. it's illegal to say "p->data[i]" because array subscript
// operator[] can only be used with a pointer among all other
// build-in types. User-defined type can overload this operator
// though.
for (int i = 0; i < p->size; ++i) ((&p->data)[i]) = i;

Flexible Array Member

C99 has a new language feature called “flexible array member”. It’s quite similar to the struct hack except an empty bracket [].

C++
struct Foo
{
  size_t size;
  int data[]; // FLA
};

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead National Instruments
China China
Senior software engineer at National Instruments, to implement various Ethernet-based industrial protocols, e.g., EtherCAT. Favorite languages are C/C++ and Python. For fun, I like watching films (sci-fi, motion), walking, and various reading.

Comments and Discussions

 
QuestionAnyone using such hacks deserves what they get! Pin
Daniel Pfeffer6-Oct-16 0:12
professionalDaniel Pfeffer6-Oct-16 0:12 
AnswerRe: Anyone using such hacks deserves what they get! Pin
Eric Z (Jing)6-Oct-16 3:06
Eric Z (Jing)6-Oct-16 3:06 
QuestionMuch cleaner way to do this without the danger of byte alignment Pin
leon de boer1-Oct-16 2:20
leon de boer1-Oct-16 2:20 
AnswerRe: Much cleaner way to do this without the danger of byte alignment Pin
Eric Z (Jing)1-Oct-16 5:38
Eric Z (Jing)1-Oct-16 5:38 
GeneralRe: Much cleaner way to do this without the danger of byte alignment Pin
leon de boer2-Oct-16 16:42
leon de boer2-Oct-16 16:42 
It will and can happen on any and EVERY processor, if the pack size of the compiler isn't 1 and
your data block doesn't lie on an even pack boundary.

It's clear all you have really played with is 8 bit micros because your code will fail utterly
on almost everything but an 8 micro and is completely unportable to anything but an 8 bit micro
OR a processor that accepts a pack size of 1 which these days is very rare with the rise of RISC
processors.

The code I gave you does >>> exactly the same thing <<< as yours and is completely portable and
will work safely with all packing and alignment.

If you like you can even hide the self pointer inside the private data so the interface is an
exact match to the data, it just means you must provide a dispose function because you don't have
access to the self pointer to dispose it. The exposed self pointer just means if you really must
you can play with the hidden private data BUT we always advise against it favouring provide a
function to do what is required keeping the private structure hidden.

So given the correct way does EXACTLY what your attempted hack does and is completely portable
and always safe, you would question why you would ever do the private data struct your way. I
defy you to give me one advantage your code has? Your way isn't even easier to read on the struct
interface, all the complexity in the correct way is hidden inside the create and if I want I can setup
truely dynamic data size hidden behind the static interface.
In vino veritas


modified 2-Oct-16 22:54pm.

GeneralRe: Much cleaner way to do this without the danger of byte alignment Pin
Eric Z (Jing)2-Oct-16 19:31
Eric Z (Jing)2-Oct-16 19:31 
GeneralRe: Much cleaner way to do this without the danger of byte alignment Pin
leon de boer5-Oct-16 1:01
leon de boer5-Oct-16 1:01 
GeneralRe: Much cleaner way to do this without the danger of byte alignment Pin
Eric Z (Jing)5-Oct-16 4:05
Eric Z (Jing)5-Oct-16 4:05 
GeneralRe: Much cleaner way to do this without the danger of byte alignment Pin
Rick York6-Oct-16 4:46
mveRick York6-Oct-16 4:46 
GeneralRe: Much cleaner way to do this without the danger of byte alignment Pin
Eric Z (Jing)6-Oct-16 4:51
Eric Z (Jing)6-Oct-16 4:51 
QuestionI miss the need for such a hack Pin
Ivor O'Connor26-Dec-13 8:17
Ivor O'Connor26-Dec-13 8:17 
AnswerRe: I miss the need for such a hack Pin
Eric Z (Jing)26-Dec-13 15:03
Eric Z (Jing)26-Dec-13 15:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.