Unhandled Exception issues MS 2010

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;
}
}
Hey,

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.

Have fun!
Last edited on
I increased the int size to 127, but now I'm getting this:

Run-Time Check Failure #2 - Stack around the variable 'Numbuff' was corrupted.
Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>

const int 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;
}
Thank you guys for the help.

I tried the above code and I got the same problem, with the same exact error message...

I'm totally lost on what to do.
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
@booradley. Thanks for correcting it.

"For loop" range check should be "<" and not "<=" for the reason booradley mentioned above.

 
for (int j=0; j <= sizeof(Numbuff)/sizeof(Numbuff[0]); j++)

Should be
 
for (int j=0; j < sizeof(Numbuff)/sizeof(Numbuff[0]); j++)

Topic archived. No new replies allowed.