可以在编译时完成吗? [英] Can it be done at compile time?

查看:87
本文介绍了可以在编译时完成吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello NG,

我在结构中安排数据,例如

{

成员...

uint16_t crc;

更多会员,也许......

}

然后我需要保存它们,包括crc,包括crc,非volatile *

内存或文件,视情况而定。

类型T所需的数据大小是offsetof(struct T,crc)+

sizeof(uint16_t)。

(即使没有更多成员,这取决于对齐方式,可能比sizeof(struct T)短b



当我在编译时尝试测试我没有超过NV内存或文件的最大大小时,我遇到了麻烦。

对于涉及sizeof的表达式,我学习了例程:

#define SIZE ...相应总结...

extern char dummy [(SIZE< = SIZE_MAX)?1:-1];

这不会偏移但是因为它在内部使用指针类型

而不是整数表达式。

在编译时是否有任何创造性的方法来测试大小限制?


BTW,我相信可以随意假设成员在crc之前和

更多成员在追随它。它是否正确? (哦,/假设/是

便携式,但你知道我的意思:)


谢谢

- Ark

解决方案



Ark写道:


Hello NG,

我在结构中安排数据,比如

{

成员......

uint16_t crc;

更多会员,也许......

}

然后我需要保存它们,包括crc,包括非易失性

内存或文件,视情况而定。

类型T需要的数据大小是offsetof(struct T,crc)+

sizeof(uint16_t) 。

(即使没有更多成员,这取决于对齐方式,可能比sizeof(struct T)短了



我没有得到你的问题....但是假设它类似于


1.你想要一个结构中的数据

2.您希望所述数据包含所述数据的CRC ....


只需编写一个从makefile调用的脚本,该脚本生成一个.c文件

,其中包含一个结构。


Tom


" Ark" < ak ***** @ macroexpressions.com写在留言中

新闻:0I ************************* *****@comcast.com。 ..


这不会偏移但是因为它在内部使用指针类型而且

不是整数表达式 ;。



那么你的实现就破了。 C标准要求offsetof为
整数常量表达式:


7.17通用定义< stddef.h>

[ ...]

3宏是


NULL


扩展为实现定义的空指针不变;和$ / $

offsetof(类型,成员指示符)


扩展为类型为size_t的整数常量表达式,

的值是结构成员的偏移量(以字节为单位)(从成员指定者指定为

),从其结构的开头(由

指定)类型)。 [...]


[后续限制为comp.lang.c]


Wojtek Lerch写道:


" Ark" < ak ***** @ macroexpressions.com写在留言中

新闻:0I ************************* *****@comcast.com。 ..


>这不会偏移但是因为它在内部使用指针类型而且
不是整数表达式。



那么你的实现就破了。 C标准要求offsetof为
整数常量表达式:


7.17通用定义< stddef.h>

[ ...]

3宏是


NULL


扩展为实现定义的空指针不变;和$ / $

offsetof(类型,成员指示符)


扩展为类型为size_t的整数常量表达式,

的值是结构成员的偏移量(以字节为单位)(从成员指定者指定为

),从其结构的开头(由

指定)类型)。 [...]


[后续行为仅限于comp.lang.c]



嗯,大多数它的时间是什么时候

#define offsetof(type,member)((size_t)&((type *)0) - > member))

或为此而做的事情。

这对阵列尺寸来说还不够好。


谢谢,


方舟


Hello NG,
I arrange data in structs like
{
members...
uint16_t crc;
more members, maybe...
}
Then I need to save them, up to and including crc, in non-volatile
memory or a file, as the case may be.
The data size I need for type T is offsetof(struct T, crc) +
sizeof(uint16_t).
(Even if there are no more-members, this, depending on alignment, may be
shorter than sizeof(struct T).

The trouble comes when I try to test, at compile time, that I haven''t
exceeded the max size of NV memory or of the file.
For expressions involving sizeof, I learned the routine:
#define SIZE ...sum up accordingly...
extern char dummy[(SIZE<=SIZE_MAX)?1:-1];
This won''t take offsetof though ''cause it uses pointer type internally
and is not an "integer expression".
Is there any creative way to test the size limit at compile time?

BTW, I believe it is portable to assume that members are before crc and
more-members are after it. Is this correct? (Oh, /assumption/ is
portable, but you know what I mean :)

Thanks
- Ark

解决方案


Ark wrote:

Hello NG,
I arrange data in structs like
{
members...
uint16_t crc;
more members, maybe...
}
Then I need to save them, up to and including crc, in non-volatile
memory or a file, as the case may be.
The data size I need for type T is offsetof(struct T, crc) +
sizeof(uint16_t).
(Even if there are no more-members, this, depending on alignment, may be
shorter than sizeof(struct T).

I don''t get your question.... but assuming it''s something like

1. You want data in a structure
2. You want said data to include a CRC of said data....

Just write a script called from your makefile that generates a .c file
that has a structure in it.

Tom


"Ark" <ak*****@macroexpressions.comwrote in message
news:0I******************************@comcast.com. ..

This won''t take offsetof though ''cause it uses pointer type internally and
is not an "integer expression".

Then your implementation is broken. The C standard requires offsetof to be
an integer constant expression:

7.17 Common definitions <stddef.h>
[...]
3 The macros are

NULL

which expands to an implementation-defined null pointer constant; and

offsetof(type, member-designator)

which expands to an integer constant expression that has type size_t, the
value of which is the offset in bytes, to the structure member (designated
by member-designator), from the beginning of its structure (designated by
type). [...]

[Followups restricted to comp.lang.c]


Wojtek Lerch wrote:

"Ark" <ak*****@macroexpressions.comwrote in message
news:0I******************************@comcast.com. ..

>This won''t take offsetof though ''cause it uses pointer type internally and
is not an "integer expression".


Then your implementation is broken. The C standard requires offsetof to be
an integer constant expression:

7.17 Common definitions <stddef.h>
[...]
3 The macros are

NULL

which expands to an implementation-defined null pointer constant; and

offsetof(type, member-designator)

which expands to an integer constant expression that has type size_t, the
value of which is the offset in bytes, to the structure member (designated
by member-designator), from the beginning of its structure (designated by
type). [...]

[Followups restricted to comp.lang.c]

Well, most of the time it is like
#define offsetof(type, member) ((size_t)&((type *)0)->member))
or something to that end.
That ain''t good enough for array dimension.

Thanks,

Ark


这篇关于可以在编译时完成吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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