在C(用gcc),我曾經有同着一些額外信息的數組一些數據結構:運行時大小的數組
struct song {
uint tempo;
uint key;
note play[0]; // or play[] depending on compiler flavour
};
IIRC,這是被稱爲「柔性陣列「(http://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html#Zero-Length) 然後,我可以在運行時爲malloc(sizeof(song)+N*sizeof(note))
分配一首N個音符的歌曲。 如果我不打算在這個時候使用向量,在g ++中支持的程度如何,也不要在song
中引入無用的note*
指針?
」也不會在歌曲中引入無用的音符*指針「 - 您逗我了。順便說一句'malloc'是C,'new'是C++ – 2013-02-17 13:12:36
http://stackoverflow.com/questions/9573748/variable-length-struct-nonstandard-in-c11?lq=1 – user93353 2013-02-17 13:12:37
@EdHeal:不正確。 'malloc'既是C又是C++。 – 2013-02-17 13:19:09