[C,C++] – 妙用0元素數組實現大小可變結構體

來源:互聯網
上載者:User

來源:http://hi.baidu.com/zhoutianyang/blog/item/1eab2d1f57cc1bcda7866995.html

 

#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);
return0;
}

 

 

看這個結構體的定義:

typedef struct st_type
{
int nCnt;
int item[0];
}type_a;

(有些編譯器會報錯無法編譯可以改成:)

typedef struct st_type
{
int nCnt;
int item[];
}type_a;

    這樣我們就可以定義一個可變長的結構,用sizeof(type_a)得到的只有4,就是sizeof(nCnt)=sizeof(int)那個0個元素的數組沒有佔用空間,而後我們可以進行變長操作了。

 

// C語言版:
type_a *p = (type_a*)malloc(sizeof(type_a)+100*sizeof(int));
// C++語言版:
type_a *p = (type_a*)newchar[sizeof(type_a)+100*sizeof(int)];

 

 

    這樣我們就產生了一個長為100的type_a類型的東西用p->item[n]就能簡單地訪問可變長元素,原理十分簡單,分配了比sizeof (type_a)多的記憶體後int item[];就有了其意義了,它指向的是int nCnt;後面的內容,是沒有記憶體需要的,而在分配時多分配的記憶體就可以由其來操控,是個十分好用的技巧。
而釋放同樣簡單:

 

// C語言版:
free(p);
// C++語言版:
delete []p;

    這個被稱為靈活/彈性數群組成員(fleible array member)C89不支援這種東西,C99把它作為一種特例加入了標準。但是,C99所支援的是incomplete type,而不是zero array,形同int item[0];這種形式是非法的,C99支援的形式是形同int item[];只不過有些編譯器把int item[0];作為非標準擴充來支援,而且在C99發布之前已經有了這種非標準擴充了,C99發布之後,有些編譯器把兩者合而為一。

    下面是C99中的相關內容:
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.

注意區分 C99新增的“可變長數組”:
C89 標準規定,數組大小必須是在編譯時間刻確定的;在C99 中,這個標準項被擴充,可以是運行時刻確定的值。也就是說, 可變長數組和 C++ 本身沒有關係,只要是支援 C99 的就可以使用可變長數組,包括支援 C99 的 C 編譯器。

需要注意的是,可變長數組的維數在數組生存期內是不變的,也就是說,可變長數組不是動態,可變的只是數組的大小。

引進這一特性的目的是為了支援數值處理。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.