# "Stack around the variable 'bar' was corrupted"?

Show 80 post(s) from this thread on one page
Page 2 of 2 First 12
• 09-29-2012
ryanmcclure4
In the function:

Code:

```string ZipCode::zipToBar(int zip) {                        //Converts zip to bar code     int zipArr[5];     string ZERO = "11000",             ONE = "00011",             TWO = "00101",             THREE = "00110",             FOUR = "01001",             FIVE = "01010",             SIX = "01100",             SEVEN = "10001",             EIGHT = "10010",             NINE = "10100";     for (int i = 4; i >= 0; i--) {                //Converts integer zip into and array zipArr         zipArr[i] = zip % 10;         zip /= 10;     };     for (int i = 0; i < 5; i++) {                //Concatenates strings onto barCode according to the current position in zipArr         if (zipArr[i] == 0)             barCode += ZERO;         else if (zipArr[i] == 1)             barCode += ONE;         else if (zipArr[i] == 2)             barCode += TWO;         else if (zipArr[i] == 3)             barCode += THREE;         else if (zipArr[i] == 4)             barCode += FOUR;         else if (zipArr[i] == 5)             barCode += FIVE;         else if (zipArr[i] == 6)             barCode += SIX;         else if (zipArr[i] == 7)             barCode += SEVEN;         else if (zipArr[i] == 8)             barCode += EIGHT;         else if (zipArr[i] == 9)             barCode += NINE;     };     barCode = "1" + barCode + "1";                //Adds a "1" to each end of barCode     return barCode; }```
I previously had "if (zipArr[i] = 0) ... ... etc" and I'm surprised it never gave me any errors. changing it to the code above fixed the problem.
• 09-29-2012
std10093
i thought you had in your code the vice versa.Of course he gives no warning or error :)

It is completely ok to write something like this.Well ok for the compiler,not you ;) So what you had there was a logical error and not a syntax error(way much easier to find).

What happens with if(zipArr[0]=0) is that it always assigns value zero to zipArr[0].That's why this was false.

Remember that one equal sign ,assigns something to something else.For comparison we always use two equal signs !
• 09-29-2012
whiteflags
Quote:

Originally Posted by ryanmcclure4
Ahh, that was it! Thanks a lot for explaining all that, I'll definitely use it in the future :D

Here's a better tip for your programming life. Next time you write a simple program, step through the lines with a debugger and watch the variables change. It will get you used to using a debugger. Then when you get a segmentation fault in the future, the debugger will stop the code on the line where the segfault was raised. So there really isn't any point to writing a bunch of printf() lines or whatever. If the segfault didn't happen on that line, it probably happened in a lower level function.

Segfaults do not have to be the hardest errors to fix. :)
Show 80 post(s) from this thread on one page
Page 2 of 2 First 12