gpt4 book ai didi

c - 是否可以在不使用第三方库的情况下在运行时告诉 C 中整数类型有多少字节

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:03:50 24 4
gpt4 key购买 nike

我希望在运行时探测一个 int 在运行时的字节数,而不使用超过 POSIX C。

基本上,我的想法是寻找一种方法来检查 UINT_MAX 的系统限制,而无需让我的二进制文件在编译时依赖该信息。我首先想到尝试 sysconf() 但该变量未指定为可以使用的变量。

编辑 1:为了澄清我正在做的事情是在阅读 od 的 POSIX 1003.1-2017 规范的基础上构建我自己的 od 程序。效用。

特别是根据我对扩展描述中这些行的阅读和解释。

The default number of bytes transformed by output type specifiers d, f, o, u, and x corresponds to the various C-language types as follows. If the c99 compiler is present on the system, these specifiers shall correspond to the sizes used by default in that compiler. Otherwise, these sizes may vary among systems that conform to POSIX.1-2017. For the type specifier characters d, o, u, and x, the default number of bytes shall correspond to the size of the underlying implementation's basic integer type

我读到这意味着默认值是基于运行系统的环境而不是编译器

编辑 2:sizeof 是我在反射(reflection)中寻找的东西 我意识到我最初的想法对于没有 libc 的环境中的静态链接二进制文件没有意义。

谢谢大家。

最佳答案

如果你想知道 int 你的 可执行文件使用的大小,那么你可以使用 sizeof 因为大小在之后不会改变beeing compiled(即使你在另一个处理器上运行它)。

如果你想知道你的程序在这台机器上编译后的int大小,那么最简单的方法可能是像这样编译和运行:

int main() {
printf("%zu\n", sizeof(int));
}

(或者你可以在正在运行的机器上为 UINT_MAX grep include 目录)

如果您想在不同架构之间移动 int,那就完全不同了。

如果没有回答,请阐明您的意图。

编辑:

grep /usr/include/limits.h -e 'UINT_MAX' --color

效果很好。

关于c - 是否可以在不使用第三方库的情况下在运行时告诉 C 中整数类型有多少字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49589158/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com