Jack Stepanek Jack Stepanek - 3 months ago 8
C++ Question

Program sometimes doesn't run and gives me debug error

It says :


Debug Error!

R6010
-abort() has been called


I'm not really sure what to do about it. I'm making a program that automatically generates reviews for
SliceThePie
so you don't have to take the time to come up with them.

#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;
}


I was initially running into problems with my computer not having enough memory to run it occasionally, but I think that I've fixed that, as it doesn't happen very often anymore. Now, however, i'm getting the debug error.

Any help would be appreciated!!

Answer

You have an out of bounds access for secondSentence. Its size is 7 but you generate random numbers up to 9:

int secondSentence = rand() % 10

This can be detected by e.g. GCC's sanitizer:

$ 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]'
Comments