C++ 如何防止违反内存保护?

C++ How do I prevent Memory Protection Violation?

我遇到了 内存冲突问题 如果达到 else if(argc == 2)

就会发生

我正在尝试编写一个没有类似错误的漂亮脚本,其他任何东西都像一个魅力...

这是一个代码片段:

//...
// POWER ON
        if(strcmp(argv[2], "on") == 0)
        {
            // GPIO On
            pin_on();
            // Open the serial port READ-WRITE
            int serial_port = open("/dev/ttyS5", O_RDWR);
            // Load serial port settings
            settings(serial_port);
            // POWER ON Message
            unsigned char msg[] = { '\x2A', '\x20', '\x30', '\x20',
                                    '\x49', '\x52', '\x20', '\x30',
                                    '\x30', '\x31', '\x0D' };
            // Send message to device
            write(serial_port, msg, sizeof(msg));
            // Time for device to process
            std::system("sleep 0.25");
            // Close the serial port
            close(serial_port);
            // GPIO Off
            pin_off();
            return 0;   // success
        }
        // POWER OFF
        else if(strcmp(argv[2], "off") == 0)
        {
            // GPIO On
            pin_on();
            // Open the serial port READ-WRITE
            int serial_port = open("/dev/ttyS5", O_RDWR);
            // Load serial port settings
            settings(serial_port);
            // POWER OFF Message
            unsigned char msg[] = { '\x2A', '\x20', '\x30', '\x20',
                                    '\x49', '\x52', '\x20', '\x30',
                                    '\x30', '\x32', '\x0D' };
            // Send message to device
            write(serial_port, msg, sizeof(msg));
            // Time for device to process
            std::system("sleep 0.25");
            // Close the serial port
            close(serial_port);
            // GPIO Off
            pin_off();
            return 0;   // success
        }
        else if(argc == 2)
        {
            std::cout << "No argument passed!" << std::endl;
            std::cout << "Pattern: acer power <on/off>" << std::endl;
            return 1;   // failure
        }
        else if(argc > 3)
        {
            std::cout << "Too many arguments passed!" << std::endl;
            std::cout << "Pattern: acer power <on/off>" << std::endl;
            return 1;   // failure
        }
        else
        {
            std::cout << "Wrong argument! Passed: " << std::string(argv[2]) << std::endl;
            std::cout << "Pattern: acer power <on/off>" << std::endl;
            return 1;   // failure
        }
//...

编辑:除了 argc 和 argv[] 值程序没有分配任何变量

编辑 2.:程序由 2 个参数动作和 3 个参数动作构成,就像我提到的这个代码我发布的那样。

我听从了@Yksisarvinen 的建议并重新排序了代码,现在它可以完美运行了:

//...
        if(argc == 3)
        {
            // POWER ON
            if(strcmp(argv[2], "on") == 0)
            {
                // GPIO On
                pin_on();
                // Open the serial port READ-WRITE
                int serial_port = open("/dev/ttyS5", O_RDWR);
                // Load serial port settings
                settings(serial_port);
                // POWER ON Message
                unsigned char msg[] = { '\x2A', '\x20', '\x30', '\x20',
                                        '\x49', '\x52', '\x20', '\x30',
                                        '\x30', '\x31', '\x0D' };
                // Send message to device
                write(serial_port, msg, sizeof(msg));
                // Time for device to process
                std::system("sleep 0.25");
                // Close the serial port
                close(serial_port);
                // GPIO Off
                pin_off();
                return 0;   // success
            }
            // POWER OFF
            else if(strcmp(argv[2], "off") == 0)
            {
                // GPIO On
                pin_on();
                // Open the serial port READ-WRITE
                int serial_port = open("/dev/ttyS5", O_RDWR);
                // Load serial port settings
                settings(serial_port);
                // POWER OFF Message
                unsigned char msg[] = { '\x2A', '\x20', '\x30', '\x20',
                                        '\x49', '\x52', '\x20', '\x30',
                                        '\x30', '\x32', '\x0D' };
                // Send message to device
                write(serial_port, msg, sizeof(msg));
                // Time for device to process
                std::system("sleep 0.25");
                // Close the serial port
                close(serial_port);
                // GPIO Off
                pin_off();
                return 0;   // success
            }
            else
            {
                std::cout << "Wrong argument! Passed: " << std::string(argv[2]) << std::endl;
                std::cout << "Pattern: acer power <on/off>" << std::endl;
                return 1;   // failure
            }
        }
        else if(argc == 2)
        {
            std::cout << "No argument passed!" << std::endl;
            std::cout << "Pattern: acer power <on/off>" << std::endl;
            return 1;   // failure
        }
        else
        {
            std::cout << "Too many arguments passed!" << std::endl;
            std::cout << "Pattern: acer power <on/off>" << std::endl;
            return 1;   // failure
        }
//...

如果这个条件

else if(argc == 2)

计算结果为 true 则意味着 argv[argc] 等于 nullptr。因此,在这种情况下使用表达式 argv[2](即空指针)例如调用 strcmp 会导致未定义的行为。索引的有效范围是 [0, 1].

来自 C++ 14 标准(3.6.1 主函数)

  1. ...If argc is nonzero these arguments shall be supplied in argv[0] through argv[argc-1] as pointers to the initial characters of null-terminated multibyte strings (ntmbs s) (17.5.2.1.4.2) and argv[0] shall be the pointer to the initial character of a ntmbs that represents the name used to invoke the program or "". The value of argc shall be non-negative. The value of argv[argc] shall be 0.