程序有时无法运行并给我调试错误

Program sometimes doesn't run and gives me debug error

本文关键字:调试 错误 运行 程序      更新时间:2023-10-16

上面写着:

调试错误!

R6010-abort()已被调用

我真的不知道该怎么办。我在做一个程序,可以自动为SliceThePie生成评论,这样你就不用花时间去写评论了。

#include "stdafx.h"
#include <Windows.h>
#include <ctime>
#include <cstdlib>
#include <iostream>
#include <stdlib.h>
#include <string>
#include <array>
using namespace std;
void type_text(const std::string& text)
{
    for (size_t i = 0; i < text.size(); ++i){
        cout << text[i] << flush;
        Sleep(30);
    }
}

int generateAnswers(int loop){
    srand(time(NULL));
    int firstSentence = rand() % 8 + 0;
    int descriptors1Sentence2 = rand() % 6 + 0;
    int secondSentence = rand() % 10 + 0;
    int descriptors1Sentence = rand() % 6 + 0;
    int describe1Sentence = rand() % 14 + 0;
    int punc1C = rand() % 2 + 0;
    array<int, 6> returning = { firstSentence, descriptors1Sentence2, secondSentence, descriptors1Sentence, describe1Sentence, punc1C };
    return returning[loop - 1];
}
void randGen(){
    srand(time(NULL));
    //cout << rand() % 10 + 1 << endl;
    string descriptors1[] = { "beat ", "tempo ", "instrumentals ", "synth ", "midi ", "effects "};
    string descriptors2[] = { "mix ", "sound ", "volume ", "timing ", "rhythm ", "composure " , "melody ", "chorus "};
    string describe1[] = { "amazing", "great", "mediocre", "fair", "average", "fantastic", "beautiful", "A+ worthy", "top notch", "top of the line", "one of the best", "needing work", "not the best", "lackluster" };
    string firstSen []= { "Truthfully ", "I believe " , "In my opinion, ", "In my honest opinion, ", "I honestly believe ", "I really think that ", "I think that " , "First off, " };
    string secondSen[] = { "In addition to this, ", "Additionally, ", "Also, ", "Furthermore, ", "Continuing, " , "To continue, ", "In addition to my last point, " };
    string punc1[] = { ".", "!" };
    int firstSentence = generateAnswers(1);
    int descriptors1Sentence2 = generateAnswers(2);
    int secondSentence = generateAnswers(3);
    int descriptors1Sentence = generateAnswers(4);
    int describe1Sentence = generateAnswers(5);
    int punc1C = generateAnswers(6);
    string finFirst = firstSen[firstSentence];
    string finDesc2 = descriptors1[descriptors1Sentence2];
    string finDesc1 = descriptors1[descriptors1Sentence];
    string finDescr1 = describe1[describe1Sentence];
    string punc1S = punc1[punc1C];
    string finSecond = secondSen[secondSentence];
    string Final = finFirst + "the " + finDesc1 + "was " + finDescr1 + punc1S + " " + finSecond + "the " + finDesc2 + "was ";
    cout << "Waiting for review to complete..." << endl;
    cout << "n" << endl;
    type_text(Final);
    cout << "n" << endl;
    cout << "Review complete." << endl;
}
int main() {
    SetConsoleTitle(TEXT("Diloq v1.01"));
    randGen();
    Sleep(5000);
    return 0;
}

我最初遇到的问题是我的电脑没有足够的内存来偶尔运行它,但我想我已经解决了这个问题,因为它不再经常发生了。现在,但是,我得到调试错误。

任何帮助将不胜感激!!

您有secondSentence的越界访问权限。它的大小是7,但是你可以生成随机数直到9:

int secondSentence = rand() % 10

这可以被GCC的杀毒器检测到:

$ g++ -std=c++11 -fsanitize=undefined -Og -g main.cpp && ./a.out
main.cpp:55:48: runtime error: index 8 out of bounds for type 'string [7]'