Variable Sized Struct C++

前端 未结 10 1094
忘掉有多难
忘掉有多难 2020-11-30 04:48

Is this the best way to make a variable sized struct in C++? I don\'t want to use vector because the length doesn\'t change after initialization.

struct Pack         


        
10条回答
  •  醉梦人生
    2020-11-30 05:45

    If you never add a constructor/destructor, assignment operators or virtual functions to your structure using malloc/free for allocation is safe.

    It's frowned upon in c++ circles, but I consider the usage of it okay if you document it in the code.

    Some comments to your code:

    struct Packet
    {
        unsigned int bitlength;
        unsigned int data[];
    };
    

    If I remember right declaring an array without a length is non-standard. It works on most compilers but may give you a warning. If you want to be compliant declare your array of length 1.

    Packet* CreatePacket(unsigned int length)
    {
        Packet *output = (Packet*) malloc((length+1)*sizeof(unsigned int));
        output->bitlength = length;
        return output;
    }
    

    This works, but you don't take the size of the structure into account. The code will break once you add new members to your structure. Better do it this way:

    Packet* CreatePacket(unsigned int length)
    {
        size_t s = sizeof (Packed) - sizeof (Packed.data);
        Packet *output = (Packet*) malloc(s + length * sizeof(unsigned int));
        output->bitlength = length;
        return output;
    }
    

    And write a comment into your packet structure definition that data must be the last member.

    Btw - allocating the structure and the data with a single allocation is a good thing. You halve the number of allocations that way, and you improve the locality of data as well. This can improve the performance quite a bit if you allocate lots of packages.

    Unfortunately c++ does not provide a good mechanism to do this, so you often end up with such malloc/free hacks in real world applications.

提交回复
热议问题