woaidongmao

文章均收录自他人博客,但不喜标题前加-[转贴],因其丑陋,见谅!~
随笔 - 1469, 文章 - 0, 评论 - 661, 引用 - 0
数据加载中……

下标是空的数组

最近看到这种代码:
struct mytype
{
int cnt;
int data[0];//
这个,是什么啊!0个元素的数组????编译器居然通过了!!!什么东西啊??
};
---------------------------------------------------------------

int data[0];//
定义一个数组0个元素,编译没有错,但是这个不能用于输入,因为是没有元素的数组
---------------------------------------------------------------

所以当输入的时候,就等于越界了。
---------------------------------------------------------------

楼主可以算算一下数组的大小:

sizeof(mytype.data);//
这样可能有的编译器会报错,有的可能等于0
---------------------------------------------------------------

这是一种trick,用来做变长数组
---------------------------------------------------------------

这个叫做灵活数组成员

看我的文章

结构体变长的妙用——0个元素的数组
有时我们需要产生一个结构体,实现了一种可变长度的结构。如何来实现呢?
看这个结构体的定义:
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*)new char[sizeof(type_a)+100*sizeof(int)];
这样我们就产生了一个长为100type_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.
例如在VC++6里使用两者之一都能通过编译并且完成操作,而会产生warning C4200: nonstandard extension used : zero-sized array in struct/union的警告消息。
而在DEVCPP里两者同样可以使用,并且不会有警告消息。

---------------------------------------------------------------

data
是用来做变长数组的,
不过一般都是 长度为 ...
---------------------------------------------------------------

一般这样用

先定义一个指向该struct的指针p
然后
p=malloc(sizeof( mytype)+ user_length);
p->cnt = user_length;

这样就相当于有了一个可变长的数组,其长度保存在p->cnt当中。


---------------------------------------------------------------

变长数组,不过这种实现变长数组的方法比较晦涩
---------------------------------------------------------------

呵呵.你要学的东西多了..
看看linux 源码,特别tcp/ip,网络相关的.很多这样的应用..
---------------------------------------------------------------

变长数组!!
没有什么特别的,和平常的内存分配一个样,
我们以前使用
Type* t = (Type*)malloc(sizeof(Type));
这个只是分配刚好存放Type的地方,其实我们也可以分配比Type类型少的内存,
比如:
typedef struct st_type
{
int nCnt;
int item[4];
}type_a;
--------------------
type_a * a=(type_a*)malloc(4);//
刚好分配了type_a::nCnt的空间而已!
那也肯定可以操作,只是item的空间没有存在而已,比如:
a->nCnt = 2;
cout<<a->nCnt<<endl;

------------------------------------------
当然也可以分配多啊,剩下的东西怎么处理看程序员的操作啊!
其实
typedef struct st_type
{
int nCnt;
int item[0];
}type_a;
type_a* a = (type_a*)malloc(104);
的意思中item只是给编译器一个地址的起始,多了这个内存就是多余了拉!但多余的内存和我们

posted on 2010-04-01 16:42 肥仔 阅读(929) 评论(0)  编辑 收藏 引用 所属分类: C++ 基础


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理