Skip to content

Instantly share code, notes, and snippets.

@klange

klange/embed.c Secret

Last active July 24, 2022 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klange/4c5b674f12e678e7ea070ead2fb0acdd to your computer and use it in GitHub Desktop.
Save klange/4c5b674f12e678e7ea070ead2fb0acdd to your computer and use it in GitHub Desktop.
/**
* Flexible arrays
*
* Initialize 'foo' as a char array with the contents
* of 'file.dat'. 'sizeof foo' is the size of file.dat.
*/
char foo[] [[embed("file.dat")]];
/**
* Flexible structs
*
* Same as for arrays, though since sizeof() can't do
* what it should here under normal circumstances, don't
* expect it to work in this case either...
*
* Maybe issue a warning if "file.dat" is of insufficient
* size to fill required members of 'struct Bar'.
*
* Struct padding is 1-1, as if 'bar' was set by
* reading the file directly into it.
*/
struct Bar {
size_t size;
char stuff[];
};
struct Bar bar [[embed("file.dat")]];
/**
* Non-flexible objects:
*
* Use sizeof(type) bytes from file.dat, but object
* remains same size as base type.
*/
uint64_t baz [[embed("file.dat")]]; /* sizeof(uint64_t) bytes from file.dat */
char qux[16] [[embed("file.dat")]]; /* (up to?) 16 bytes from file.dat */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment