| #include <stdio.h> #include <string.h> #include <stdlib.h> struct aa{? ??? int a; ??? int b; }; struct bb{? ??? struct aa test[0]; }; int main(void) { ??? struct bb *p=(struct bb*)malloc(sizeof(struct bb)+sizeof(struct aa)*100); ??? p->test[0].a=10; ??? p->test[0].b=20; ??? printf("%d,%d\n",p->test[0].a,p->test[0].b); ??? return 0; } 看這個(gè)結(jié)構(gòu)體的定義: typedef struct st_type { ???? int nCnt; ???? int item[0]; }type_a; (有些編譯器會(huì)報(bào)錯(cuò)無(wú)法編譯可以改成:) typedef struct st_type { ???? int nCnt; ???? int item[]; }type_a; ??? 這樣我們就可以定義一個(gè)可變長(zhǎng)的結(jié)構(gòu),用sizeof(type_a)得到的只有4,就是sizeof(nCnt)=sizeof(int)那個(gè)0個(gè)元素的數(shù)組沒(méi)有占用空間,而后我們可以進(jìn)行變長(zhǎng)操作了。 ??????? C語(yǔ)言版:??????? type_a *p = (type_a*)malloc(sizeof(type_a)+100*sizeof(int)); ??????? C++語(yǔ)言版:??????? type_a *p = (type_a*)new char[sizeof(type_a)+100*sizeof(int)]; ??? 這樣我們就產(chǎn)生了一個(gè)長(zhǎng)為100的type_a類(lèi)型的東西用p->item[n]就能簡(jiǎn)單地訪(fǎng)問(wèn)可變長(zhǎng)元素,原理十分簡(jiǎn)單,分配了比sizeof(type_a)多的內(nèi)存后int item[];就有了其意義了,它指向的是int nCnt;后面的內(nèi)容,是沒(méi)有內(nèi)存需要的,而在分配時(shí)多分配的內(nèi)存就可以由其來(lái)操控,是個(gè)十分好用的技巧。 而釋放同樣簡(jiǎn)單: ??????? C語(yǔ)言版:free(p); ??????? C++語(yǔ)言版:delete []p; ??? 這個(gè)被稱(chēng)為靈活/彈性數(shù)組成員(fleible array member)C89不支持這種東西,C99把它作為一種特例加入了標(biāo)準(zhǔn)。但是,C99所支持的是incomplete type,而不是zero array,形同int item[0];這種形式是非法的,C99支持的形式是形同int item[];只不過(guò)有些編譯器把int item[0];作為非標(biāo)準(zhǔn)擴(kuò)展來(lái)支持,而且在C99發(fā)布之前已經(jīng)有了這種非標(biāo)準(zhǔn)擴(kuò)展了,C99發(fā)布之后,有些編譯器把兩者合而為一。 ??? 下面是C99中的相關(guān)內(nèi)容: 6.7.2.1 Structure and union specifiers ??? As a special case, the last element of a structure with more than one named member may have an incomplete array type; this is called a flexible array member. With two exceptions, the flexible array member is ignored. First, the size of the structure shall be equal to the offset of the last element of an otherwise identical structure that replaces the flexible array member with an array of unspecified length.106) Second, when a . (or ->) operator has a left operand that is (a pointer to) a structure with a flexible array member and the right operand names that member, it behaves as if that member were replaced with the longest array (with the same element type) that would not make the structure larger than the object being accessed; the offset of the array shall remain that of the flexible array member, even if this would differ from that of the replacement array. If this array would have no elements, it behaves as if it had one element but the behavior is undefined if any attempt is made to access that element or to generate a pointer one past it. 注意區(qū)分 C99新增的“可變長(zhǎng)數(shù)組”: C89 標(biāo)準(zhǔn)規(guī)定,數(shù)組大小必須是在編譯時(shí)刻確定的;在C99 中,這個(gè)標(biāo)準(zhǔn)項(xiàng)被擴(kuò)展,可以是運(yùn)行時(shí)刻確定的值。也就是說(shuō), 可變長(zhǎng)數(shù)組和 C++ 本身沒(méi)有關(guān)系,只要是支持 C99 的就可以使用可變長(zhǎng)數(shù)組,包括支持 C99 的 C 編譯器。 需要注意的是,可變長(zhǎng)數(shù)組的維數(shù)在數(shù)組生存期內(nèi)是不變的,也就是說(shuō),可變長(zhǎng)數(shù)組不是動(dòng)態(tài)的,可變的只是數(shù)組的大小。 引進(jìn)這一特性的目的是為了支持?jǐn)?shù)值處理。 |