Hey guys, so I'm having an issues with buffer overflow and I'm not quite sure what to do. I've stepped through it a few times and since I'm pretty new at programming I'm having a difficult time finding the issue. It'll run once and then tell me, upon exiting the program, that there is a buffer overflow at "Numbuff".
Here's the error:
Unhandled exception at 0x76f215ee in Homework__3.exe: 0xC0000005: Access violation.
Here's the code:
#include <iostream>
using namespace std;
const int SIZE=132;
void CharCount(char CharInput[], int Numbuff[]);
void CountOut (int Numbuff[]);
int main()
{
int Numbuff[122];
char CharInput[SIZE];
int count = 0;
for (int j=0; j <= sizeof(Numbuff); j++)
{
Numbuff[j]=0;
}
cout<< "Enter a line of text less than 132 characters: \n";
cin.getline(CharInput, sizeof(CharInput));
count = cin.gcount();
if (count > SIZE) //FIX LATER
cout << "You've entered more than 132 characters.";
CharCount(CharInput, Numbuff);
CountOut(Numbuff);
system("pause");
return 0;
}
void CharCount(char CharInput[], int Numbuff[])
{
for (int i=0; i<=122; i++)
{
char x = CharInput[i];
if (x >= 97 && x <= 122) //a to z
{
Numbuff[x]++;
}
if (x >= 65 && x <= 90) //A to Z
{
Numbuff[x]++;
}
if (x >= 48 && x <= 57) //0 to 9
{
Numbuff[x]++;
}
}
}
void CountOut(int Numbuff[])
{
for (int x = 48; x <= 57; x++)
{
cout << char(x) << " - " << Numbuff[x] << endl;
}
for (int x = 65; x <= 90; x++)
{
cout << char(x) << " - " << Numbuff[x] << endl;
}
for (int x = 97; x <=122; x++)
{
cout << char(x) << " - "<< Numbuff[x] << endl;
}
}
Have you checked what sizeof(Numbuff) actually returns?
=> It's the actual size in bytes of your array
=> an "int" has (considering we have a 32bit compiler) the size of 4.
=> sizeof(numbuff) = 122*4
This means your loop changes stuff in memory outside of the array
Besides that your array has the size of 122 which means you have the array-positions 0-121
but some of your loops later go from "0 to 122" (the 122th position is not in the array anymore. 121 is the last.)
I guess that should provide you with enough to figure everything out now.
#include <iostream>
constint SIZE=132;
void CharCount(char CharInput[], int Numbuff[], int count);
void CountOut (int Numbuff[]);
int main()
{
int Numbuff[SIZE];
char CharInput[SIZE];
int count = 0;
for (int j=0; j <= sizeof(Numbuff)/sizeof(Numbuff[0]); j++)
Numbuff[j]=0;
std::cout<< "Enter a line of text less than 132 characters: \n";
std::cin.getline(CharInput, sizeof(CharInput));
count = std::cin.gcount();
if (count > SIZE) //FIX LATER
std::cout << "You've entered more than 132 characters.";
CharCount(CharInput, Numbuff, count);
CountOut(Numbuff);
return 0;
}
void CharCount(char CharInput[], int Numbuff[], int count)
{
for (int i=0; i<count; i++)
{
char x = CharInput[i];
if (x >= 97 && x <= 122) //a to z
Numbuff[x]++;
if (x >= 65 && x <= 90) //A to Z
Numbuff[x]++;
if (x >= 48 && x <= 57) //0 to 9
Numbuff[x]++;
}
}
void CountOut(int Numbuff[])
{
for (int x = 48; x <= 57; x++)
std::cout << char(x) << " - " << Numbuff[x] << std::endl;
for (int x = 65; x <= 90; x++)
std::cout << char(x) << " - " << Numbuff[x] << std::endl;
for (int x = 97; x <=122; x++)
std::cout << char(x) << " - "<< Numbuff[x] << std::endl;
}
for (int j=0; j <= sizeof(Numbuff)/sizeof(Numbuff[0]); j++)
Shouldn't the <= be just < instead? Recall that arrays are accessed starting at zero, so valid indicies for an array of size n are 0 through n-1