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

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    45

    "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

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Is the barcode string more than 25 characters long?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Take a look at this ref string::substr - C++ Reference
    substr takes as second parameter the length of the substring.In your code you use it as
    Code:
      
        barCode = barCode.substr(1,26);
    So you are trying to store a 26 characters string into the string barCode.So are you sure that barcode has enough space for it?Also i suppose you have store memory for it in first place..Well maybe this is not the problem,but i thought i should just point you out.Also remember that if you want to take the substring from the first character of the string you should call the function like this
    Code:
    mystring.substr(0,substringLength);
    EDIT->arrived second

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    45
    brewbuck - The barCode string is originally 27 characters long, and I wrote the code in line 2 in order to remove the first and last characters of the string. I wasn't sure if it would work to use the same name, although I tried it as follows:
    Code:
        string Code = "";
        Code = barCode.substr(1,26);
    I changed the rest of the code fittingly, and it still worked so I decided to leave it as it was...

    std10093 - I looked at the reference, and realized I was using the function incorrectly; I was under the impression you were supposed to input the range of characters you wanted to copy over. How should I have written it so that it will create a new string (25 characters long) that does not include the first and last entry of barCode(which is 27 characters long)? Should I use:
    Code:
    mystring.substr(0,substringLength);
    and then set the last character to null?

    Thanks

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    That error message means you blew past the end of the memory that was allocated to your stack variable. It most certainly means the program will cease to function correctly immediately or in the near future in release builds. If this happens to variables that are allocated on the heap then it results in heap corruption. Either way it means you overran an array somewhere.

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by ryanmcclure4 View Post
    std10093 - I looked at the reference, and realized I was using the function incorrectly; I was under the impression you were supposed to input the range of characters you wanted to copy over. How should I have written it so that it will create a new string (25 characters long) that does not include the first and last entry of barCode(which is 27 characters long)? Should I use:
    Code:
    mystring.substr(0,substringLength);
    and then set the last character to null?

    Thanks
    Since n is the length of substring as the ref says,you should set the second parameter to 25 and the first to 1 (because the first letter is in position zero and then you are going to take the 25 letters that follow)

    EDIT->here is an example
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main ()
    {
    	string s="012345678901234567890123456";
    	string s1=s.substr(1,25);
    	cout<<s1.length()<<" "<<s1<<endl;
    	return 0;
    }
    Output
    Code:
    25 1234567890123456789012345
    Last edited by std10093; 09-29-2012 at 06:03 AM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by std10093
    because the first letter is in position one and then you are going to take the 25 letters that follow
    The first character is in position 0, hence the idea is to start with the index of the second character, i.e., 1.

    By the way, this is exceedingly unnecessary, especially when these exist just for convenience:
    Code:
    memcpy (zipOne, bar, 5 * sizeof(int));
    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));
    You should have declared them as pointers instead, then you can just write:
    Code:
    zipOne = bar;
    zipTwo = bar + 5;
    zipThree = bar + 10;
    zipFour = bar + 15;
    zipFive = bar + 20;
    Or you could have an array of pointers then use a loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by laserlight View Post
    The first character is in position 0, hence the idea is to start with the index of the second character, i.e., 1.
    typo.Thanks Edited my post

  9. #9
    Registered User
    Join Date
    Mar 2012
    Posts
    45
    I changed the second parameter to 25 as std100093 said, and that seemed to fix the problem. Also, thanks for the tip laserlight I changed my code accordingly.

    Unfortunatley, now I've run into another problem...It's throwing this error "Unhandled exception at 0x778615de in Zip Code.exe: 0xC0000005: Access violation." It seems to be occurring at line 17 in main.

    This is the code in my main.cpp file:
    Code:
    #include <iostream>
    #include <iomanip>
    
    #include "ZipCode.h" 
    
    using namespace std;
    
    int main() {
        ZipCode zip1(99504); 
        ZipCode zip2(12345); 
        ZipCode zip3(67890);
        ZipCode zip4("100101010011100001100110001"); 
        ZipCode zip5("110100001011100001100010011"); 
        ZipCode zip6("100011000110101000011100101");
    
        cout << "Digits" << "       " << "Bar Code" << endl;
        cout << zip1.getZipCode() << setw(35) << zip1.getBarCode() << endl; 
        cout << zip2.getZipCode() << setw(35) << zip2.getBarCode() << endl; 
        cout << zip3.getZipCode() << setw(35) << zip3.getBarCode() << endl; 
        cout << endl;
        cout << zip4.getZipCode() << setw(35) << zip4.getBarCode() << endl;
        cout << zip5.getZipCode() << setw(35) << zip5.getBarCode() << endl; 
        cout << zip6.getZipCode() << setw(35) << zip6.getBarCode() << endl; 
        
        return 0;
    }
    My header file for the ZipCode class:

    Code:
    #ifndef _ZIP_
    #define _ZIP_
    
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class ZipCode {
    private:
        int zip;
        int bar[26];
        string barCode;
        void barToZip(string barCode);
        void zipToBar(int zip);
    public:
        ZipCode(int zip);                //Constructor to initialize zip code
        ZipCode(string barCode);        //Constructor to initialize bar code
        int getZipCode();                //Returns zip code
        string getBarCode();            //Returns bar code
    
    };
    
    #endif
    My ZipCode.cpp file:

    Code:
    #include <string>
    #include "ZipCode.h"
    
    ZipCode::ZipCode(int zip) : zip(zip) {
        cout << "zip code constructor" << endl;
    }
    
    ZipCode::ZipCode(string barCode) {
        cout << barCode << endl;
        barToZip(barCode);
        cout << "bar code Constructor" << endl;
    }
    
    int ZipCode::getZipCode() {
        if (zip == 0)
            barToZip(barCode);
        return zip;
    }
    
    string ZipCode::getBarCode() {
        if (barCode == "")
            zipToBar(zip);
        return barCode;
    }
    
    void ZipCode::barToZip(string barCode) {
        barCode = barCode.substr(1,25);
        int bar[25],
            zipArr[5] = {0},
            zip = 0,
            *zipOne, 
            *zipTwo, 
            *zipThree, 
            *zipFour, 
            *zipFive;
    
        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';
        };
    
        zipOne = bar;
        zipTwo = bar + 5;
        zipThree = bar + 10;
        zipFour = bar + 15;
        zipFive = bar + 20;
    
        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;
    }
    
    void ZipCode::zipToBar(int zip) {
        int zipArr[5];
    
        string ZERO = "11000";
        string ONE = "00011";
        string TWO = "00101";
        string THREE = "00110";
        string FOUR = "01001";
        string FIVE = "01010";
        string SIX = "01100";
        string SEVEN = "10001";
        string EIGHT = "10010";
        string NINE = "10100";
    
        for (int i = 4; i >= 0; i++) {
            zipArr[i] = zip % 10;
            zip /= 10;
        };
    
        for (int i = 0; i < 5; i++) {
            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;
        };
    }
    Not sure what this means...

    Just for the record I'm new to C++ and need to practice using pointers, algorithms, etc. better; I'm sure all this code could be very much condensed and neater.

    Wherever you see lines like
    Code:
    cout << "zip code constructor" << endl;
    I was just checking to see how far the program had gone until throwing the error.

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    The problem is here
    Code:
    zip1.getBarCode()
    So i follow this function ,which leads me to the zipToBar function where i saw this
    Code:
    for (int i = 4; i >= 0; i++) {
            zipArr[i] = zip % 10;
            zip /= 10;
        };
    the condition is i >=0 and i is set to 4 and been increased..this may be the bug

    Tip for your programming life.
    • You get a bus error or a segmentation fault.You are not the first nor the last who gets one,so stay cool
    • Go to your main and fill it with
      Code:
      cout<<"FLAG###1"<<endl;
      everywhere you fill that the error may occur.Remember to change the number in every single line you write this.
    • You will see what was the last cout that was executed,so you are now able to focus on a line
    • If the line has more than one function calls,break the line and put some extra cout too ,in order to isolate the exact function that produces the error to occur!
    • Once you isolate the function,go to her body and make the same (in a recursive way i would say :P )

    To much words i would say..Maybe i have to show you how i found yours
    your main
    Code:
    ..
        ZipCode zip6("100011000110101000011100101");
    
        cout << "Digits" << "       " << "Bar Code" << endl;
    cout<<"EDW`1"<<endl;
        cout << zip1.getZipCode() <<endl;
    cout<<"edwwww12"<<endl;
    cout<< setw(35);
    cout<<"Edwwww11"<<endl;
    cout << zip1.getBarCode() << endl;
    cout<<"EDW`2"<<endl;
        cout << zip2.getZipCode() << setw(35) << zip2.getBarCode() << endl;
    cout<<"EDW`3"<<endl;
        cout << zip3.getZipCode() << setw(35) << zip3.getBarCode() << endl;
        cout << endl;
    cout<<"EDW`4"<<endl;
        cout << zip4.getZipCode() << setw(35) << zip4.getBarCode() << endl;
    cout<<"EDW`5"<<endl;
        cout << zip5.getZipCode() << setw(35) << zip5.getBarCode() << endl;
    cout<<"EDW`6"<<endl;
        cout << zip6.getZipCode() << setw(35) << zip6.getBarCode() << endl;
         cout<<"EDW`7"<<endl;
        return 0;
    ..
    i compiled your code and the last thing i saw before the bus error was this
    Code:
    Edwwww11
    .
    As a result i knew that getBarCode() was the place i would found my answers

    So i went to ZipCode.cpp and so this
    Code:
    int ZipCode::getZipCode() {
        if (zip == 0)
            barToZip(barCode);
        return zip;
    }
    it is a small function ,so my mind focused in barToZip(barCode) in a picosecond

    So i searched for this function and i saw the for loop i stated before..

    It is not always like that though..If everything was -in my eyes of course- ok,i would fill the function with cout<<"inside-FLAG1"<<endl; and so on and do the same thing as i did in main in order to isolate the line that the error occured

  11. #11
    Registered User
    Join Date
    Mar 2012
    Posts
    45
    Ahh, that was it! Thanks a lot for explaining all that, I'll definitely use it in the future
    I hate to ask for more advice, but I keep running into more problems...in the following code I am testing to see whether or not the function barToZip is working correctly.
    Code:
    ZipCode::ZipCode(string barCode) {
        cout << barCode << endl;
        barToZip(barCode);
        cout << zip << endl;
        cout << "bar code Constructor" << endl;
    }
    At the end of the function barToZip I have cout << zip << endl; and it outputs the correct value. However, when I write cout << zip << endl; in the above segement (after the call of barToZip) it outputs a value of "-858993460" which I'm guessing is the location it is stored in memory? How come it isn't giving me the same value when I try outputting zip outside of the barToZip function?

    Thanks again

  12. #12
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by ryanmcclure4 View Post
    Thanks again
    I love that you ask

    Code:
        barToZip(barCode);
        cout << zip << endl;
    barToZip returns an int,which is the zip!!!But you did not assign the return value to the zip,so you loose the return value of the function..
    you have to write
    Code:
        zip = barToZip(barCode);
        cout << zip << endl;
    Also take notice that there is something that we call debugger.For now you can do the tactic with cout and if you are not ok with it,then you can search more about it on google

    Non-programming tip->If you see at the right bottom of every post there is a Like | Share section...You can use to let your friends know the tip about how to find the bus error

  13. #13
    Registered User
    Join Date
    Mar 2012
    Posts
    45
    Alright, I changed the barToZip funciton to return type int instead of void, changed the code as you said, and it worked! I keep forgetting about these small things and they always pop up when I do. I've used the debugger a few times but don't fully understand what everything means, I'll look into it further when I have time.
    One last thing - my zipToBar function is returning a bar code that looks like "0001100011000110001100011" which would be equivalent to the zip code 11111. The problem must be in the for loop that concatenates each string according to the current zip code digit onto the barCode string.

  14. #14
    Registered User
    Join Date
    Mar 2012
    Posts
    45
    Nevermind, I wan't thinking and used "=" in the if statement instead of "==". Thanks for the help though!

  15. #15
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by ryanmcclure4 View Post
    Nevermind, I wan't thinking and used "=" in the if statement instead of "==". Thanks for the help though!
    The = in the if statement.Explain to me exactly what you did( and show me the code).
    sorry for the time that made me to answer

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