Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you never add a constructor/destructor, assignment operators or virtual functions to your structure using malloc/free for allocation is safe.</p> <p>It's frowned upon in c++ circles, but I consider the usage of it okay if you document it in the code.</p> <p>Some comments to your code: </p> <pre><code>struct Packet { unsigned int bitlength; unsigned int data[]; }; </code></pre> <p>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.</p> <pre><code>Packet* CreatePacket(unsigned int length) { Packet *output = (Packet*) malloc((length+1)*sizeof(unsigned int)); output-&gt;bitlength = length; return output; } </code></pre> <p>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:</p> <pre><code>Packet* CreatePacket(unsigned int length) { size_t s = sizeof (Packed) - sizeof (Packed.data); Packet *output = (Packet*) malloc(s + length * sizeof(unsigned int)); output-&gt;bitlength = length; return output; } </code></pre> <p>And write a comment into your packet structure definition that data must be the last member.</p> <p>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.</p> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload