魔法结构 [英] Magic structs

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

问题描述

我想了解ustr( http://www.and.org) / ustr / design

有效。它是C的字符串处理库,它将字符串存储在

后面的struct中,称为magic struct


struct Ustr
{

unsigned char data [1];

/ * 0b_wxzy_nnoo =>

*

* w =已分配

* x =有大小

* y =向上舍入分配(关闭==确切

*分配)

* z =内存错误

* nn = refn

* oo = lenn

*

* 0b_0000_0000 ="",const,no alloc failed,no mem

* err,refn = 0,lenn = 0 * /

};


在我看来,一个结构Ustr只占用1字节的内存,所以

它怎么能包含一个简单的指向char的指针???发生了什么

在这里?

解决方案

姓名和地址withheld写道:


我想了解ustr( http://www.and.org/ustr/design

有效。它是C的字符串处理库,它将字符串存储在

后面的struct中,称为magic struct


struct Ustr
{

unsigned char data [1];

/ * 0b_wxzy_nnoo =>

*

* w =已分配

* x =有大小

* y =向上舍入分配(关闭==确切

*分配)

* z =内存错误

* nn = refn

* oo = lenn

*

* 0b_0000_0000 ="",const,no alloc failed,no mem

* err,refn = 0,lenn = 0 * /

};


在我看来,一个结构Ustr只占用1字节的内存,所以

它怎么能包含一个简单的指向char的指针???发生了什么

在这里?



搜索关于结构黑客主题的Google网上论坛存档


< blockquote>名称和地址版主说:


我试图了解ustr( http://www.and.org/ustr/design

有效。它是C的字符串处理库,它将字符串存储在

以下结构中,称为魔术结构


struct Ustr
{

unsigned char data [1];



< comment snipped>


};


在我看来,结构Ustr只占用1字节的内存,

所以它怎么能包含一个简单的指向char的指针???



它不能,除非指针只有一个字节宽(这是可能的但是很少见的是b / b)。
< blockquote class =post_quotes>
这里发生了什么?



这里发生破碎的代码。


-

Richard Heathfield< http: //www.cpax.org.uk>

电子邮件:-www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日




姓名和地址被扣留 < do ** @ spam.mewrote in message

news:sl ******************** @ nospam.invalid ...


>我试图了解ustr( http://www.and.org/ustr/design

有效。它是C的字符串处理库,它将字符串存储在

后面的struct中,称为magic struct


struct Ustr
{

unsigned char data [1];

/ * 0b_wxzy_nnoo =>

*

* w =已分配

* x =有大小

* y =向上舍入分配(关闭==确切

*分配)

* z =内存错误

* nn = refn

* oo = lenn

*

* 0b_0000_0000 ="",const,no alloc failed,no mem

* err,refn = 0,lenn = 0 * /

};


在我看来,一个结构Ustr只占用1字节的内存,所以

它怎么能包含一个简单的指向char的指针???发生了什么

这里?



struct Ustr x;


x.data实际上是指向单个char的指针。 sturct不需要

包含地址,编译器可以从struct的地址计算出数据数组的地址

。由于只有一个项目,因此它必须与结构的地址相同。

我们现在可以通过实现来玩愚蠢的游戏来获得超过一个字符串中的
字符。例如,如果结构位于

未初始化的内存块的顶部,则最后一个成员(如果是数组)可以扩展为
。这与实现是一种无根据的笨拙,并且

不能在你自己的代码中推荐,但对于像

a字符串库那样基本的东西,可能有一个它的情况。也许吧。


-

免费游戏和编程好东西。
http://www.personal.leeds.ac.uk/~bgy1mm


I am trying to understand how ustr (http://www.and.org/ustr/design)
works. Its a string-handling library for C, and it stores strings in the
following struct, called a "magic struct"

struct Ustr
{
unsigned char data[1];
/* 0b_wxzy_nnoo =>
*
* w = allocated
* x = has size
* y = round up allocations (off == exact
* allocations)
* z = memory error
* nn = refn
* oo = lenn
*
* 0b_0000_0000 = "", const, no alloc fail, no mem
* err, refn=0, lenn=0 */
};

It seems to me that a struct Ustr will only occupy 1-byte of memory, so
how can it contain even a simple pointer to char??? What''s happening
here?

解决方案

Name and address withheld wrote:

I am trying to understand how ustr (http://www.and.org/ustr/design)
works. Its a string-handling library for C, and it stores strings in the
following struct, called a "magic struct"

struct Ustr
{
unsigned char data[1];
/* 0b_wxzy_nnoo =>
*
* w = allocated
* x = has size
* y = round up allocations (off == exact
* allocations)
* z = memory error
* nn = refn
* oo = lenn
*
* 0b_0000_0000 = "", const, no alloc fail, no mem
* err, refn=0, lenn=0 */
};

It seems to me that a struct Ustr will only occupy 1-byte of memory, so
how can it contain even a simple pointer to char??? What''s happening
here?

Search the Google Groups archive for this group on the topic ''struct hack''


Name and address withheld said:

I am trying to understand how ustr (http://www.and.org/ustr/design)
works. Its a string-handling library for C, and it stores strings in
the following struct, called a "magic struct"

struct Ustr
{
unsigned char data[1];

<comment snipped>

};

It seems to me that a struct Ustr will only occupy 1-byte of memory,
so how can it contain even a simple pointer to char???

It can''t, unless pointers are only one byte wide (which is possible but
rare).

What''s happening here?

Broken code is happening here.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999



"Name and address withheld" <do**@spam.mewrote in message
news:sl********************@nospam.invalid...

>I am trying to understand how ustr (http://www.and.org/ustr/design)
works. Its a string-handling library for C, and it stores strings in the
following struct, called a "magic struct"

struct Ustr
{
unsigned char data[1];
/* 0b_wxzy_nnoo =>
*
* w = allocated
* x = has size
* y = round up allocations (off == exact
* allocations)
* z = memory error
* nn = refn
* oo = lenn
*
* 0b_0000_0000 = "", const, no alloc fail, no mem
* err, refn=0, lenn=0 */
};

It seems to me that a struct Ustr will only occupy 1-byte of memory, so
how can it contain even a simple pointer to char??? What''s happening
here?

struct Ustr x;

x.data is effectively a pointer to a single char. the sturct doesn''t need to
contain the address, the compiler can work out the address of the data array
from the address of the struct. Since there is only one item, it must in
fact be the same as the address of the struct.
We can now play silly games with the implementation to get more than one
character in the string. For instance if the struct sits at the top of an
uninitialised block of memory the last member, if it is an array, can be
extended. This is an unwarranted chumminess with the implementation, and
isn''t to be recomended in your own code, but for something as fundamental as
a string library there is maybe a case for it. Just maybe.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


这篇关于魔法结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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