Thread: "Stack around the variable 'bar' was corrupted"?

  1. #16
    Registered User
    Join Date
    Mar 2012
    Posts
    45
    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.

  2. #17
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    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 !

  3. #18
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by ryanmcclure4 View Post
    Ahh, that was it! Thanks a lot for explaining all that, I'll definitely use it in the future
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 07-05-2012, 08:32 AM
  2. Replies: 9
    Last Post: 09-14-2010, 07:16 AM
  3. stack around the variable corrupted
    By chintugavali in forum C++ Programming
    Replies: 2
    Last Post: 01-09-2008, 01:01 PM
  4. Stack around variable corrupted?
    By timmygee in forum C++ Programming
    Replies: 7
    Last Post: 12-10-2005, 08:19 AM
  5. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM