"Stack around the variable 'bar' was corrupted"?
Hi everyone, I'm writing a part of a program that is going to convert a bar code into a zip code using the Postnet format. It works when I compile it up until the end of this function:
Code:
void ZipCode::barToZip(string barCode) {
barCode = barCode.substr(1,26);
int bar[25],
zipOne[5], //Each zip array will contain 5 integers from the bar code
zipTwo[5],
zipThree[5],
zipFour[5],
zipFive[5],
zipArr[5] = {0},
zip = 0;
char currentChar;
for(int i = 0; i < barCode.length(); i++) { //Converts string barCode to int array bar[]
currentChar = barCode.at(i); //Sets currentChar to the character in barCode at index i
bar[i] = (int)currentChar-'0';
};
memcpy (zipOne, bar, 5 * sizeof(int)); //copies memory of the first 5 integers of bar to zipOne
memcpy (zipTwo, &bar[5], 5 * sizeof(int));
memcpy (zipThree, &bar[10], 5 * sizeof(int));
memcpy (zipFour, &bar[15], 5 * sizeof(int));
memcpy (zipFive, &bar[20], 5 * sizeof(int));
zipArr[0] = (7 * zipOne[0]) + (4 * zipOne[1]) + (2 * zipOne[2]) + (1 * zipOne[3]); //Converts bar code numbers into zip code digit
zipArr[1] = (7 * zipTwo[0]) + (4 * zipTwo[1]) + (2 * zipTwo[2]) + (1 * zipTwo[3]);
zipArr[2] = (7 * zipThree[0]) + (4 * zipThree[1]) + (2 * zipThree[2]) + (1 * zipThree[3]);
zipArr[3] = (7 * zipFour[0]) + (4 * zipFour[1]) + (2 * zipFour[2]) + (1 * zipFour[3]);
zipArr[4] = (7 * zipFive[0]) + (4 * zipFive[1]) + (2 * zipFive[2]) + (1 * zipFive[3]);
for (int i = 0; i < 5; i++) { //Changes any zip digits that are 11 to 0
if (zipArr[i] == 11)
zipArr[i] = 0;
};
zip = 10000 * zipArr[0]; //Converts array into a single zip code integer
zip += 1000 * zipArr[1];
zip += 100 * zipArr[2];
zip += 10 * zipArr[3];
zip += 1 * zipArr[4];
cout << zip << endl;
}
I know the function is working because it outputs the correct zip code at the end. However, each time I compile it, it stops at the end of this function and says "Run-Time Check Failure #2 - Stack around the variable 'bar' was corrupted."
When I look at the Autos window (I'm using Visual C++ 2010) the 'this' pointer has a very long value and it is red. I've seen similar problems when I searched for this but couldn't figure out a solution...
Let me know if you need to see the code for the rest of the program.
Thanks for any help!
-Ryan