C++程序返回int类型,为什么return -1返回255?

2022-01-11 00:00:00 operating-system return-value c integer c++

请注意,我正在运行一台 linux 机器,尽管我认为在 windows(或其他)机器上的结果没有什么不同.

Note that I am running a linux machine, although I don't think the result is different on a windows (or other) machine.

这是一个简单的问题 - C++ 程序通常返回 32 位 int.如果我return -1,然后从终端打印结果,结果是255.

This is a simple question - C++ programs usually return a 32 bit int. If I return -1, and then print the result from the terminal, the result is 255.

这是为什么?我觉得链接这是我应该知道的,或者很多年前就应该注意到的――我以前从未真正使用过返回码,也从未想过.

Why is this? I feel link this is something I should know, or should have noticed many years ago - I never really used return codes before now or thought about it before.

测试 C++ 程序:

int main()
{
    return -1;
}

编译:

g++ main.cpp -o a.out

运行:

./a.out

检查结果:

echo $?

结果:

255

我本来希望看到 2^32 - 1.

I would have expected to see 2^32 - 1.

为什么结果255不是-1甚至是4294967295.(2^32 - 1)

Why is the result 255 not -1 or even 4294967295. (2^32 - 1)

推荐答案

因为(不是所有)操作系统使用整个返回值.在这种情况下,它被截断为低 8 位.由操作系统(以及其他相关组件,例如 shell)来使用"和保留"这个值,这就是所谓的实现细节",换句话说,C 和 C++ 标准没有说明什么返回值的有用性或意义是 [除了下面的段落] - 只是从 C 的角度来看它是一个 int - C 程序可以在忽略该值的环境中启动,截断、扩展或乘以 432,它仍然是有效的 C 或 C++ 环境.

Because (not all) OS's use the whole return value. In this case, it's truncated to the low 8 bits. It's up to the OS (and other related components, such as the shell) to "use" and "retain" this value, and it's what's called an "implementation detail", in other words, the C and C++ standards do not dicate what the usefulness or meaning of the return value is [aside from the paragraph below] - just that from the C perspective it is an int - the C program may be started in an environment where that value is ignored, truncated, extended or multiplied by 432 and it's still a valid C or C++ environment.

C 标准规定值 0EXIT_SUCCESS (据我理解,值应该为零)和值 EXIT_FAILURE(一些非零值)应分别视为成功和失败.但是,所有其他值都是实现定义的",因此在执行发生的操作系统/环境的规则下.

The C standard says that the value 0 or EXIT_SUCCESS (which should have the value zero as I understand it) and the value EXIT_FAILURE (some non-zero value) should be considered success and failure respectively. However, all other values are "implementation defined" and thus under the rules of the OS/environment that the execution happens in.

请注意,当 shell(或任何运行您的程序)时,它不会直接跳转到您的 main,而是首先执行一些其他功能来初始化您的 所需的东西主函数.一旦 main 返回/退出,通常也会有一些代码在您的程序之后执行.具体如何工作取决于几件事:

Note that when the shell (or whatever runs your program), it does not jump straight to your main, but some other functionality is performed first to initialize things that are needed by your main function. And once main has returned/exited, there is usually some code that executes AFTER your program as well. Exactly how this works is dependent on several things:

  • 谁编写了编译器和/或运行时库
  • 它是为什么操作系统设计的
  • 它是什么处理器架构
  • 可能是什么 shell/runtime/OS 功能启动了该进程

C 和 C++ 标准没有定义这些东西,因为这样做可能会影响可以/将要运行 C 和/或 C++ 应用程序的实际平台,而 C 和 C++ 的目标是具有包容性" -换句话说,尽量不要限制语言支持的环境、处理器等.

The C and C++ standard doesn't define these things, as doing so would potentially affect the actual platforms that can/will run C and/or C++ applications, and the goal of C and C++ is to "be inclusive" - in other words, try to not limit the environment, processor, etc that the languages support.

相关文章