C: Recommended style for dynamically sized structs

后端 未结 4 1735
轻奢々
轻奢々 2020-12-31 14:41

I need to transfer packets through the internet whose length should be dynamic.

struct packet
{
  int id;
  int filename_len;
  char filename[];
};


        
相关标签:
4条回答
  • 2020-12-31 15:03

    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.

    0 讨论(0)
  • 2020-12-31 15:10

    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.

    0 讨论(0)
  • 2020-12-31 15:18

    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.

    0 讨论(0)
  • 2020-12-31 15:20

    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.

    0 讨论(0)
提交回复
热议问题