C: Recommended style for dynamically sized structs
I suggest to use char filename[1]
and include a terminating 0-byte. This way, you can malloc()
the correct structure size and avoid one-off errors like this:
ptr = malloc(sizeof(struct packet)+filename_len);
strncpy(&ptr->filename, filename, filename_len);
But the receiver must to know that it needs to read filename_len+1
bytes.
Classic issue. You can simply deal with it (and note that sizeof(foo) may be off by more than one if the compiler rounds the structure size up, which is (I believe) allowed), or you can do something like this:
struct packetheader {
int id;
int filename_len;
};
struct packet {
struct packetheader h;
char filename[1];
};
This is annoying (you have to use h.id, etc), but it works. Usually I just deal with it being one, but the above might be marginally more portable.
Indeed zero-length arrays are not part of the standard. But what you have in your code snippet is a flexible array which is part of the ISO C99 standard. If it is possible for you to use C99, I'd use a flexible array, if not jesup's suggestion is probably the best.
I think you should look at some existing examples of dynamically sized structures for guidance here. The best example I know of are the TOKEN APIs in Win32. They use the macro ANYSIZE_ARRAY which just resolves down to 1. Raymond Chen did an extensive blog article detailing exactly why they are done this way
- https://devblogs.microsoft.com/oldnewthing/20040826-00/?p=38043
As for operations such as sizeof failing. This will fail no matter what solution you choose for a dynamically sized struct. sizeof is a compile time operation and you will be re-sizing the structure at runtime. It simply cannot work.