Click here to Skip to main content
15,888,221 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 
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 
Clearly you haven't played with many compilers there are multiple ways to get data misalignment

If you have visual studio put the following above your code
#pragma pack(4)

Now try your code .. tell me what happens and do you understand what that command does?
If you don't it aligns every instruction in the structure to a 4 byte boundary .. so a char in the foo struct now occupies 4 bytes.

#pragma pack(4)
struct foo {
  char a;  // this will be a char aligned to offset 0 in the structure
  char b;  // this will be a char aligned to offset 4 in the structure
}

The problem is the packing command can be issued from anywhere it doesn't have to be in your code section and YOUR CODE DROPS DEAD.

Most 16 or 32 bit RISC C compilers will set the packing to 2 or 4 depending what is faster on the RISC but the user often swings it
around at the top of there project.

To use your code I would need to test the packing on some dummy structure and then adjust the padding accordingly.
You somehow think you can do it manually but you can't it has to be automatic because we go back to the situation
the packing can be changed ANYWHERE at ANYTIME in the code.

The code I gave you will work ALWAYS NO EXCEPTION on any C compiler it's IMPOSSIBLE FOR IT NOT TO WORK because the C language guarantees it. The C compiler knows the current packing at the moment it compiles and the structure to that alignment.

If you can get my code not to work on alignment or any situation you have found a bugged compiler .. it is 100% guaranteed to work the C language dictates it. Your challenge is to find a compiler my code doesn't work on with any setting you like.

Now clearly you are dug into that idea and no one is going to convince you So I will leave you to it and all I can do is downvote the article and really implore people to never do it as there are much safer ways. Those safer ways also have some cute features you can use but that is another story.
In vino veritas

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.