Thursday, 15 August 2013

Run-time sized array in a c++ struct -



Run-time sized array in a c++ struct -

in c (with gcc), used have info structures array information:

struct song { uint tempo; uint key; note play[0]; // or play[] depending on compiler flavour };

iirc, that's dubbed "flexible array" (http://gcc.gnu.org/onlinedocs/gcc/zero-length.html#zero-length) allocate song of n notes malloc(sizeof(song)+n*sizeof(note)) @ run-time. extent supported in g++, if don't intend utilize vectors time, nor introduce useless note* pointer in song ?

even in c, undefined , non-portable. gcc knowingly lets away it, other implementations may not. because accessing array beyond bounds.

this "trick" exactly valid in c++ in c. is, sense free utilize in gcc gcc documentation says it's supported, it'll never "well-defined, valid c++".

c++ arrays struct g++ flexible-array-member

No comments:

Post a Comment