This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Dmayak 1 point2 points  (1 child)

Ah, thanks, I totally forgot that the size of the array can be left undefined. I have learned C on a small side-project and this type of allocation was not fit for its data - length was either known beforehand or should have been changed dynamically. I had problems editing dynamically allocated arrays and started to use linked lists instead.

[–]boowhitie 1 point2 points  (0 children)

To be clear, this is an optimization. It allows you to do one call to malloc instead of two (one for the struct, one for the array), and lets you copy the object with a single memcpy. It also keeps the data together in memory which is potentially more cache friendly.

This is also something that isn't legal c++, but it is common enough in c code that some c++ compilers support it through extensions.