我们可以改变C中size_t的大小吗?
我们可以改变 C 中 size_t
的大小吗?
Can we change the size of size_t
in C?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我们可以改变 C 中 size_t
的大小吗?
Can we change the size of size_t
in C?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
不。但是你为什么要这么做呢?
No. But why would you even want to do it?
size_t
不是宏。它是合适的无符号整数类型的typedef
。size_t
在
(和其他标头)中定义。它可能是
typedef unsigned long long size_t;
并且您真的甚至不应该考虑更改它。 标准库按照标准库的定义使用它。如果您更改它,因为您无法更改标准库,您将收到各种错误,因为您的程序使用与标准库不同的 size_t 大小。您不能再调用malloc()
、strncpy()
、snprintf()
等size_t
is not a macro. It is atypedef
for a suitable unsigned integer type.size_t
is defined in<stddef.h>
(and other headers).It probably is
typedef unsigned long long size_t;
and you really should not even think about changing it. The Standard Library uses it as defined by the Standard Library. If you change it, as you cannot change the Standard Library, you'll get all kinds of errors because your program uses a different size for size_t than the Standard Library. You can no longer callmalloc()
,strncpy()
,snprintf()
, ...如果你想 fork Linux 或 NetBSD,那么“是”
虽然你可以重新定义宏,但这可能是一个 typedef。
如果您正在定义一个环境,那么根据您的喜好指定
size_t
是完全合理的。然后,您将负责符合代码需要size_t
的所有 C99 标准函数。所以,这取决于你的情况。如果您正在为现有平台开发应用程序,那么答案是否。
但是,如果您要使用一个或多个编译器定义原始环境,那么答案是是,但您的工作已经完成。您需要使用
size_t
API 元素实现所有库例程,该元素可以使用新的size_t
typedef 与其余代码一起编译。因此,如果您分叉 NetBSD 或 Linux(也许是为了嵌入式系统),那么就选择它。否则,你很可能会发现它“不值得付出努力”。If you want to fork Linux or NetBSD, then "Yes"
Although you can redefine macros this one is probably a typedef.
If you are defining an environment then it's perfectly reasonable to specify
size_t
as you like. You will then be responsible for all the C99 standard functions for which conforming code expectssize_t
.So, it depends on your situation. If you are developing an application for an existing platform, then the answer is no.
But if you are defining an original environment with one or more compilers, then the answer is yes, but you have your work cut out for you. You will need an implementation of all the library routines with an API element of
size_t
which can be compiled with the rest of your code with the newsize_t
typedef. So, if you fork NetBSD or Linux, perhaps for an embedded system, then go for it. Otherwise, you may well find it "not worth the effort".