为啥它停止并以退出代码 11 结束?

Posted

技术标签:

【中文标题】为啥它停止并以退出代码 11 结束?【英文标题】:Why it stops and finished with exit code 11?为什么它停止并以退出代码 11 结束? 【发布时间】:2019-08-22 04:04:58 【问题描述】:

我不知道为什么它会停在那里并以退出代码 11 结束。它应该一直运行到我发出命令为止。

#include <iostream>
#include <string>
#include <fstream>
using namespace std;


void record(string name, string phoneNum, int count);

// main
int main() 
    cout << " Welcome to use the Phone Contact Systerm " << endl;
    string name;
    string phoneNum;
    int count = 0;
    string signToStop;
    cout << " Please enter name and phone number " << endl;
    while ( cin >> name >> phoneNum)
        cout << " If you want to start the program, enter start "         << endl;
        cout << " If you want to quit the program, enter quit " <<     endl;
        cin >> signToStop;
        if (signToStop == "start")
            record(name, phoneNum, count);
            cout << " Please enter name and phone number " << endl;
        
        else if ( signToStop == "quit" )
            break;
        
        cout << count << endl;
        count++;


    


// record all name info into Name set and record all phone numbers         into PhoneNum set
void record(string name, string phoneNum, int count)
    string Name[] = ;
    string PhoneNum[] = ;
    Name[count] = name;
    PhoneNum[count] = phoneNum;

    // now start to record all the info into .txt document

    ofstream phoneFile;
    phoneFile.open("contact.txt");
    phoneFile << name << "  " << phoneNum << endl;

结果是:

 Welcome to use the Phone Contact Systerm 

 Please enter name and phone number 

Molly 5307609829

 If you want to start the program, enter start 

 If you want to quit the program, enter quit 

start

 Please enter name and phone number 

0

Lilyi 44080809829

 If you want to start the program, enter start 

 If you want to quit the program, enter quit 

start

Process finished with exit code 11

【问题讨论】:

您的record() 函数没有为其本地数组分配足够的(即任何)内存,因此访问这些数组的任何元素都会越界并导致未定义的行为。该函数到底想做什么? 【参考方案1】:

问题出在这部分:

void record(string name, string phoneNum, int count)
    string Name[] = ;
    string PhoneNum[] = ;
    Name[count] = name;
    PhoneNum[count] = phoneNum;

    //...

这在 C++ 中很糟糕,因为 string Name[] = ; 和其他类似的人不会做你认为他们做的事情。他们创建了一个空的字符串数组。由于变量length arrays are not a thing in C++,这将创建一个buffer overflow,它是is undefined behavior。 That's bad.

改用std::vector

void record(string name, string phoneNum)
    std::vector<std::string> Name;
    std::vector<std::string> PhoneNum;
    Name.push_back(name);
    PhoneNum.push_back(phoneNum);

    //...



附:您的程序中还有另一个错误。也就是说,NamePhoneNum 在每次函数退出时都会被销毁。如果这是有意的,那很好。如果您希望保留一个正在运行的记录列表,那就不好了。您可以使用static variable 来解决此问题:

void record(string name, string phoneNum)
    static std::vector<std::string> Name;
    static std::vector<std::string> PhoneNum;
    //...


【讨论】:

【参考方案2】:

退出代码 11 不是特定于 C++ 标准的任何内容。但是,在 Linux 上,该代码通常用于表示分段错误。在我的脑海中,除了您在写入文件后从不关闭文件这一事实之外,我没有看到任何明显的错误。

【讨论】:

以上是关于为啥它停止并以退出代码 11 结束?的主要内容,如果未能解决你的问题,请参考以下文章

为啥程序结束后失败

进程在 PyCharm 中以退出代码 137 结束

关于vb退出程序的问题

试图将 python 嵌入到 Visual Studio 2010 C++ 文件中,并以代码 1 退出

如何在现代 Linux 上的 Perl 中运行程序(超时)并知道它是如何结束的(信号、退出代码等)?

使用等于运算符时遇到问题。以退出代码 11 结束 [关闭]