char数据[0] [英] char data[0]

查看:83
本文介绍了char数据[0]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


这是我在书中看到的一个问题


typedef struct mall_li_header_ {

int refcnt;

uchar pool;

uchar flag;

ushort magic_no;

char data [0 ];


} mall_li_header_t;


这里有什么数据[0]的用途?

回答我从同事那里得到的回答


这是很多游戏程序员使用的一个非常古老(常见)的想法
来自昔日的


完整性的答案 - 常用的是:分配任意的

大小 - 超出结构大小的任何东西都可以被引用为

" data" 。


到底这是真的?????

解决方案

< a href =mailto:aa ***** @ gmail.com> aa ***** @ gmail.com 写道:


这是我在书中看到的问题


(snip struct hack)


这里有什么数据[0]的使用?

我从同事那里得到的答案为



(剪辑)


你的同事所说的与常见问题解答相似内容:
http://c-faq.com/ struct / structhack.html


-

C. Benson Manica |我*应该*知道我在说什么 - 如果我

cbmanica(at)gmail.com |不,我需要知道。火焰欢迎。


2006年10月10日11:56:12 -0700, aa *****@gmail.com 在comp.lang.c中写道:


大家好,


这是我在书中看到的一个问题


typedef struct mall_li_header_ {

int refcnt;

uchar pool;

uchar flag;

ushort magic_no;

char数据[0];


} mall_li_header_t ;


这里有什么数据[0]的使用?

我从同事那里得到的回答是


这是很多游戏程序员使用的非常古老(常见)的想法
来自y的
完美的答案 - 常见的用途是:分配任意的

大小 - 超出结构大小的任何东西都可以被引用为

数据。


到底这是真的?????



实际上是为了测试损坏的编译器,因为从来没有允许任何大小为0的
数组声明

标准版C.


-

Jack Klein

主页: http://JK-Technology.Com

常见问题

comp.lang.c http://c-faq.com/

comp.lang.c ++ http:// www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu.edu/~a...FAQ -acllc.html


Jack Klein写道:

aa ***** @ gmail.com 写道:


typedef struct mall_li_header_ {

char data [0];



它实际上用于测试损坏的编译器,因为从来没有允许任何大小为0的
数组声明

标准版C.



编译器可以提供不改变的扩展名

任何符合计划的行为。


Hi all,

this is a question which i saw in a book

typedef struct mall_li_header_ {
int refcnt;
uchar pool;
uchar flag;
ushort magic_no;
char data[0];

} mall_li_header_t;

What is the use of data[0] here ?
to which answer i found from my colleagues as

this is a very old (and common) idea used by a lot of game programmers
from yesteryears.
The answer for completeness sake - common use is : allocate arbitrary
size - anything above the size of the struct can be referenced as
"data".

to what extent this is true?????

解决方案

aa*****@gmail.com wrote:

this is a question which i saw in a book

(snip struct hack)

What is the use of data[0] here ?
to which answer i found from my colleagues as

(snip)

What your colleagues said is similar to the FAQ''s content:
http://c-faq.com/struct/structhack.html

--
C. Benson Manica | I *should* know what I''m talking about - if I
cbmanica(at)gmail.com | don''t, I need to know. Flames welcome.


On 10 Oct 2006 11:56:12 -0700, aa*****@gmail.com wrote in comp.lang.c:

Hi all,

this is a question which i saw in a book

typedef struct mall_li_header_ {
int refcnt;
uchar pool;
uchar flag;
ushort magic_no;
char data[0];

} mall_li_header_t;

What is the use of data[0] here ?
to which answer i found from my colleagues as

this is a very old (and common) idea used by a lot of game programmers
from yesteryears.
The answer for completeness sake - common use is : allocate arbitrary
size - anything above the size of the struct can be referenced as
"data".

to what extent this is true?????

What it is actually for is testing for broken compilers, since an
array declaration with a size of 0 has never been allowed in any
version of standard C.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


Jack Klein wrote:

aa*****@gmail.com wrote:


typedef struct mall_li_header_ {
char data[0];


What it is actually for is testing for broken compilers, since an
array declaration with a size of 0 has never been allowed in any
version of standard C.

Compilers are allowed to offer extensions that don''t alter the
behaviour of any conforming program.


这篇关于char数据[0]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆