The compiler should insert the null terminator if it's not in the embedded file.
The compiler should insert the null terminator if it's not in the embedded file.
It's stuff like this that leaves us writing C to rely on implementation defined behavior. Under specification that leaves easy holes to fill will be filled by the compiler and we will rely on them. Just like type punning.
Though yes I agree lots of features get bogged down trying to handle everything. But in this case not adding it creates even more complexity. You can store strings natively in C. You can’t include binary blobs in C in a platform independent way so you have hacks that explode compile times for everyone using that software.
The ability to add vendor specific attributes also allows for those use cases to evolve naturally while still solving the core problem of embedding binary data.
edit: why is it desirable to concatenate files with #embed? Does that not seem out of scope if not contrived?
const char foo[] = {
#embed <file.txt>
, 0
};That said for binary embeds you almost always need the length embedded as well, which has been the case for every tool I've used to embed files in object code. You usually get something like
const size_t My_FILE_LENGTH = ... ;
const uint8_t MY_FILE[] = { ... };Like, are you proposing that the ideal semantics for `#embed "foo"` if foo contained 0x10 0x20 0x30 0x40 to be to expand to `16, 32, 48, 64, 0`? That seems more annoying than the opposite, given that:
> That said for binary embeds you almost always need the length embedded as well, which has been the case for every tool I've used to embed files in object code. You usually get something like
TFA demonstrated it by relying on sizeof() of arrays doing the right thing:
static_assert((sizeof(sound_signature) / sizeof(*sound_signature)) >= 4,
"There should be at least 4 elements in this array.");
You'd need to change this to subtract one every time, which sounds more annoying when embedding binary resources than adding the zero to strings would be.