Jump to content

VS2013 Bad memory allocation error C++

Go to solution Solved by 2FA,
for (int i = 4; winnings >= highScore[i] && i >= 0; i++)
		{
			Rank = i;
		}

If I'm reading that correctly, this starts at 4 and keeps increasing which doesn't seem correct. For example, highscore[5] doesn't exist.

I'm getting this error 58e9767fc74e2_badallocmemlocation.JPG.4a23c199fd165b741c7031dc05b03849.JPG it points to this part of the code(it says its the line with the closing curly bracket)

for (int i = 4; i != Rank; i--)
{
highScore[i] = highScore[i - 1];
highScoreName[i] = highScoreName[i - 1];
}

 

Here's the rest of it

//Includes the libraries
#include <iostream>
#include <string>
#include <fstream>

//Uses the standard namespace
using namespace std;

//Declares the global variables
int guess;
int winnings;

//Defines the question class
class Question
{
	private:
		string questionText;
		string answerOne;
		string answerTwo;
		string answerThree;
		string answerFour;
		int correctAnswer;
		int prizeAmount; //This determines how much the question is worth
	public:
		void setValues(string q, string a1, string a2, string a3, string a4, int ca, int pa);

		void askQuestion();
};

void main()
{
	//Declares the local variables
	int highScore[5];
	string highScoreName[5];
	int Rank;

	//Initialize a highscore at 0
	highScore[4] = 0;

	//Inputs the highscores from a text file
	ifstream input_HighScores;
	input_HighScores.open("HighScores.txt");
	
	
	for (int i = 0; i < 5; i++)
	{
		input_HighScores >> highScore[i];
		input_HighScores >> highScoreName[i];
	}
	input_HighScores.close();
	if (highScore[4] == 0)
	{
		//Initialize the local variables
		highScore[0] = 20000;
		highScore[1] = 15000;
		highScore[2] = 10000;
		highScore[3] = 5000;
		highScore[4] = 2500;
		highScoreName[0] = "Computer";
		highScoreName[1] = "Computer";
		highScoreName[2] = "Computer";
		highScoreName[3] = "Computer";
		highScoreName[4] = "Computer";
	}

	

	//Shows the title screen
	cout << "*****************" << endl;
	cout << "*               *" << endl;
	cout << "* The Game Show *" << endl;
	cout << "*               *" << endl;
	cout << "*      by       *" << endl;
	cout << "*               *" << endl;
	cout << "*               *" << endl;
	cout << "*               *" << endl;
	cout << "*****************" << endl;
	cout << endl;

	//Creates the instances of the Question class
	Question q1;
	Question q2;
	Question q3;
	Question q4;
	Question q5;
	Question q6;
	Question q7;
	Question q8;
	Question q9;
	Question q10;

	//Sets the values of the question instances
	q1.setValues("What does cout do?", "Eject a CD", "Send Text to the printer", "Print text to the screen", "Play a sound", 3, 2500);
	q2.setValues("What are the two sections in a class?", "Public and Local.", "Global and Local.", "Global and Private.", "Public and Private.", 4, 5000);
	q3.setValues("What does cin do?", "Gets input from a file.", "Gets input from the user.", "Gets input from the computer.", "Gets the letter c.", 2, 2500);
	q4.setValues("When do you use while?", "When you want to loop code while a condition is met.", "When you want to loop code until a condition is met.", "When you want to make a class.", "When you want to put data into a file.", 1, 3000);
	


	//Asks the questions
	q1.askQuestion();
	q2.askQuestion();
	q3.askQuestion();
	q4.askQuestion();

	if (winnings >= highScore[4])
	{
		//Gets the users rank
		for (int i = 4; winnings >= highScore[i] && i >= 0; i++)
		{
			Rank = i;
		}

		//Rearrange the highscore list
		for (int i = 4; i != Rank; i--)
		{
			highScore[i] = highScore[i - 1];
			highScoreName[i] = highScoreName[i - 1];
		}

		cout << "You got a high score" << endl;
		cout << "Enter a name" << endl;
		cin >> highScoreName[Rank];
		highScore[Rank] = winnings;
	}

	//Shows the highscore list
	cout << "High Scores." << endl;
	cout << endl;
	for (int i = 0; i < 5; i++)
	{
		cout << highScore[i] << " " << highScoreName[i] << endl;
	}

	//Outputs the Highscore data to a text file
	ofstream output_HighScores;
	output_HighScores.open("HighScores.txt");

	for (int i = 0; i < 5; i++)
	{
		output_HighScores << highScore[i] << endl;
		output_HighScores << highScoreName[i] << endl;
	}
	output_HighScores.close();
	system("PAUSE");
}

//Stores the values for the Question class variables
void Question::setValues(string q, string a1, string a2, string a3, string a4, int ca, int pa)
{
	questionText = q;
	answerOne = a1;
	answerTwo = a2;
	answerThree = a3;
	answerFour = a4;
	correctAnswer = ca;
	prizeAmount = pa;
}

void Question::askQuestion()
{
	//Makes the question
	cout << endl;
	cout << questionText << endl;
	cout << "1." << answerOne << endl;
	cout << "2." << answerTwo << endl;
	cout << "3." << answerThree << endl;
	cout << "4." << answerFour << endl;
	cout << endl;

	//Asks for the guess
	cout << "What is your answer?" << endl;
	cin >> guess;

	//Adds prizeAmount if the user is correct
	if (guess == correctAnswer)
	{
		cout << endl;
		cout << "You are correct." << endl;
		winnings = winnings + prizeAmount;
		cout << "You just won $" << prizeAmount << endl;
		cout << "Your total winnings is now $" << winnings << endl;
		cout << endl;
	}
	else 
	{
		cout << endl;
		cout << "You are incorrect." << endl;
		cout << "Your total winnings is $" << winnings << endl;
		cout << endl;
	}
}

 

Spoiler

My system is the Dell Inspiron 15 5559 Microsoft Signature Edition

                         The Austrailian king of LTT said that I'm awesome and a funny guy. the greatest psu list known to man DDR3 ram guide

                                                                                                               i got 477 posts in my first 30 days on LinusTechTips.com

 

Link to comment
https://linustechtips.com/topic/763886-vs2013-bad-memory-allocation-error-c/
Share on other sites

Link to post
Share on other sites

What happens when i = 0 and you try to access 0 - 1? 

[Out-of-date] Want to learn how to make your own custom Windows 10 image?

 

Desktop: AMD R9 3900X | ASUS ROG Strix X570-F | Radeon RX 5700 XT | EVGA GTX 1080 SC | 32GB Trident Z Neo 3600MHz | 1TB 970 EVO | 256GB 840 EVO | 960GB Corsair Force LE | EVGA G2 850W | Phanteks P400S

Laptop: Intel M-5Y10c | Intel HD Graphics | 8GB RAM | 250GB Micron SSD | Asus UX305FA

Server 01: Intel Xeon D 1541 | ASRock Rack D1541D4I-2L2T | 32GB Hynix ECC DDR4 | 4x8TB Western Digital HDDs | 32TB Raw 16TB Usable

Server 02: Intel i7 7700K | Gigabye Z170N Gaming5 | 16GB Trident Z 3200MHz

Link to post
Share on other sites

7 minutes ago, DeadEyePsycho said:

What happens when i = 0 and you try to access 0 - 1? 

I still get the error

Spoiler

My system is the Dell Inspiron 15 5559 Microsoft Signature Edition

                         The Austrailian king of LTT said that I'm awesome and a funny guy. the greatest psu list known to man DDR3 ram guide

                                                                                                               i got 477 posts in my first 30 days on LinusTechTips.com

 

Link to post
Share on other sites

Just now, themaniac said:

I still get the error

You got to be more specific. I posted in that fashion to make you think about arrays in the sense that you don't want your condition to reach one end of the array and try to access a position that is offset by +1 or -1 beyond the array. It leads to a bad memory access.

 

If you really want your for loop to decrease, increase the size of the array by one and just don't use the 0th position. That's an easy fix.

 

The other option is to rethink your logic in the for loops and/or add some if statements to avoid that.

 

Either way the problem happens when i gets to zero.

[Out-of-date] Want to learn how to make your own custom Windows 10 image?

 

Desktop: AMD R9 3900X | ASUS ROG Strix X570-F | Radeon RX 5700 XT | EVGA GTX 1080 SC | 32GB Trident Z Neo 3600MHz | 1TB 970 EVO | 256GB 840 EVO | 960GB Corsair Force LE | EVGA G2 850W | Phanteks P400S

Laptop: Intel M-5Y10c | Intel HD Graphics | 8GB RAM | 250GB Micron SSD | Asus UX305FA

Server 01: Intel Xeon D 1541 | ASRock Rack D1541D4I-2L2T | 32GB Hynix ECC DDR4 | 4x8TB Western Digital HDDs | 32TB Raw 16TB Usable

Server 02: Intel i7 7700K | Gigabye Z170N Gaming5 | 16GB Trident Z 3200MHz

Link to post
Share on other sites

for (int i = 4; winnings >= highScore[i] && i >= 0; i++)
		{
			Rank = i;
		}

If I'm reading that correctly, this starts at 4 and keeps increasing which doesn't seem correct. For example, highscore[5] doesn't exist.

[Out-of-date] Want to learn how to make your own custom Windows 10 image?

 

Desktop: AMD R9 3900X | ASUS ROG Strix X570-F | Radeon RX 5700 XT | EVGA GTX 1080 SC | 32GB Trident Z Neo 3600MHz | 1TB 970 EVO | 256GB 840 EVO | 960GB Corsair Force LE | EVGA G2 850W | Phanteks P400S

Laptop: Intel M-5Y10c | Intel HD Graphics | 8GB RAM | 250GB Micron SSD | Asus UX305FA

Server 01: Intel Xeon D 1541 | ASRock Rack D1541D4I-2L2T | 32GB Hynix ECC DDR4 | 4x8TB Western Digital HDDs | 32TB Raw 16TB Usable

Server 02: Intel i7 7700K | Gigabye Z170N Gaming5 | 16GB Trident Z 3200MHz

Link to post
Share on other sites

change int I = 4 to I = 0 and that will solve your issue, I did not run into memory issues with your code tho.  

I'm Batman!

Steam: Rukiri89 | uPlay: Rukiri89 | Origin: XxRukiriXx | Xbox LIVE: XxRUKIRIxX89 | PSN: Ericks1989 | Nintendo Network ID: Rukiri

Project Xenos: Motherboard: MSI Z170a M9 ACK | CPU: i7 6700k | Ram: G.Skil TridentZ 16GB 3000mhz | PSU: EVGA SuperNova 850w G2 | Case: Caselabs SMA8 | Cooling: Custom Loop | Still in progress 

Link to post
Share on other sites

31 minutes ago, DeadEyePsycho said:

You got to be more specific. I posted in that fashion to make you think about arrays in the sense that you don't want your condition to reach one end of the array and try to access a position that is offset by +1 or -1 beyond the array. It leads to a bad memory access.

 

If you really want your for loop to decrease, increase the size of the array by one and just don't use the 0th position. That's an easy fix.

 

The other option is to rethink your logic in the for loops and/or add some if statements to avoid that.

 

Either way the problem happens when i gets to zero.

oh ok, also by more specific do you mean an image of the error or something?

25 minutes ago, DeadEyePsycho said:

for (int i = 4; winnings >= highScore[i] && i >= 0; i++)
		{
			Rank = i;
		}

If I'm reading that correctly, this starts at 4 and keeps increasing which doesn't seem correct. For example, highscore[5] doesn't exist.

oops that's supposed to decrease not increase.

 

After running it with it decreasing its working fine

Spoiler

My system is the Dell Inspiron 15 5559 Microsoft Signature Edition

                         The Austrailian king of LTT said that I'm awesome and a funny guy. the greatest psu list known to man DDR3 ram guide

                                                                                                               i got 477 posts in my first 30 days on LinusTechTips.com

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×