C++问题汇总

C++主函数和汇编语言Assembly Language的具体关系是什么?

Some x86 programs I've seen begin execution like this:

.globl _start
_start:

GCC generated programs begin in a main function that looks something like this:

    .LFE0:
    .size   five, .-five
    .globl  main
    .type   main, @function
main:

Does an assembly program begin executing at main, _start, or either? Are there other special labels that mean "start here"? My platform is Linux, for reference.

Answer from stackoverflow https://stackoverflow.com/questions/53549660/how-does-an-x86-assembly-program-know-where-to-start-execution:

It is up to the linker to generate the entry point address as part of the final executable. That can depend on the platform, but if this is Linux the default entry point the linker uses is _start although this can be overridden. When compiling code that uses the C runtime, there is a _start label in the crt code that acts as an entry point that eventually calls main

主函数的return是如何使用的?

In C++, the main function can return an integer value, typically used to indicate the success or failure of the program. By convention:

  • Returning 0 indicates success.
  • Returning a non-zero value indicates an error or abnormal termination.

Here's a simple example:

#include <iostream>

int main() {
    int choice;

    std::cout << "Choose an option:\n";
    std::cout << "1. Continue\n";
    std::cout << "2. Exit with error\n";
    std::cin >> choice;

    if (choice == 1) {
        std::cout << "Program continues...\n";
        // ... other code ...

        return 0;  // Indicates successful termination
    } else if (choice == 2) {
        std::cout << "Exiting with an error code...\n";
        return 1;  // Indicates an error
    } else {
        std::cout << "Invalid choice!\n";
        return -1; // Another error code
    }
}

When you run this program, you can choose an option. If you choose option 1, the program will return 0, indicating a successful termination. If you choose option 2, the program will return 1, indicating an error. Any other choice will return -1.

If you're using Linux, you can check the return value of your program in the terminal (on Unix-like systems) by running the program and then using the echo $? command. This will display the return value of the last executed command, which in this case would be your program.

If you're using Windows 10, you can check the return value of a program using the Command Prompt.

Here's how you can do it:

  1. First, compile your C++ program. Let's say you've named it my_program.cpp. Using the g++ compiler, you'd compile it like this:
g++ my_program.cpp -o my_program.exe
  1. After compiling, run your program:
my_program.exe
  1. Once your program has finished executing, you can check its return value using the echo command with the %ERRORLEVEL% environment variable:
echo %ERRORLEVEL%

This will display the return value of your program. For example, if your program returned 1, the command will display 1.

Remember, in the context of the previous example:

  • A return value of 0 indicates success.
  • A return value of 1 indicates an error (from option 2).
  • A return value of -1 indicates another error (from an invalid choice).

两个short相乘

In C++, when two short values are multiplied, the usual arithmetic conversions apply. In this case, both short values undergo what's called "usual arithmetic promotion", which means they are promoted to int (if int can represent all values of type short, which is typically the case on most platforms). The multiplication is then performed with the two values as int types. The result will also be of type int.

So, for the expression:

short a = some_value;
short b = another_value;
auto result = a * b;

The multiplication a * b yields a result of type int. If you want the result to be stored in a short, you'll need to cast it explicitly:

short result = static_cast<short>(a * b);

However, be cautious: if the multiplication result exceeds the range of short, then this cast will result in a truncated or wrapped-around value.

posted @ 2023-09-26 08:05  ChrainY  阅读(8)  评论(0编辑  收藏  举报