Home > front end >  "Exception thrown: write access violation. **_Left** was 0xCCCCCCCC." Why will my function
"Exception thrown: write access violation. **_Left** was 0xCCCCCCCC." Why will my function

Time:01-31

I'm trying to write a number conversion program that will take a decimal, hex, or binary number and convert it into a different type. This program will assign an enumerated value for the original number type and the type to covert to, and assign them to int variables. These variables are passed in a function call, along with the number to convert.

My code so far is as follows:


    #include<iostream>
    #include<string>
    
    using namespace std;
    void convert(int numType, int conversionType, string userNum);
    
    
    
    int main()
    {
        cout << "What type of number would you like to provide?\n";
        cout << "Decimal(1), Binary(2), Hexadecimal(3)\n";
        
        int numType;
        
        cin >> numType;
        
        if (numType < 1 || numType > 3)
        {
            cout << "Please enter an appropriate value.\n";
            cin >> numType;
        }
    
        string userNum;
        
        cout << "Please enter the number.\n";
    
        cin >> userNum;
    
        int conversionType;
        cout << "What type of number would you like to convert to?\n";
        if (numType == 1)
        {
            cout << "Binary(2) or Hexadecimal(3)\n";
            cin >> conversionType;
            if (conversionType < 2 || conversionType > 3)
            {
                cout << "Please enter an appropriate value.\n";
                cin >> conversionType;
            }
        }
        else if (numType == 2)
        {
            cout << "Decimal(1) or Hexadecimal(3)\n";
            cin >> conversionType;
            if (conversionType < 1 || conversionType == 2 || conversionType > 3)
            {
                cout << "Please enter an appropriate value.\n";
                cin >> conversionType;
            }
        }
        else if (numType == 3)
        {
            cout << "Decimal(1) or Binary(2)\n";
            cin >> conversionType;
            if (conversionType < 1 ||conversionType >2)
            {
                cout << "Please enter an appropriate value.\n";
                cin >> conversionType;
            }
        }
        cout << "The conversion yeilds: ";
        convert(numType, conversionType, userNum);
        
    return 0;
    }
    
    void convert(int numType, int conversionType, string userNum)
    {
        int num = stoi(userNum);
    
        if (numType == 1 && conversionType == 2)
        {
            int binNum[64];
            int i;
            for (i = 0; num > 0; i  )
            {
                binNum[i] = (num % 2);
                num /= 2;
            }
            
            for (i -= 1; i >= 0; i--)
            {
                cout << binNum[i];
            }
                cout << endl;
        }
    
        if (numType == 1 && conversionType == 3)
        {
            string hexNum[64];
            int i = 0;
    
            while (num != 0)
            {
                int tempVal;
    
                tempVal = (num % 16);
    
                if (tempVal < 10)
                {
                    num  = 48;
                }
                else
                {
                    num  = 55;
                }
    
                hexNum[i] = num;
    
                i  ;
                num /= 16;
            }
            for (i -= 1; i > 0; i--)
            {
                cout << hexNum[i];
            }
            cout << endl;
        }
    }

When I test this using numType = 1 and conversionType = 2 (decimal to binary) all works well. However, when I use numType = 1 and conversionType = 3 I get this error.

Unhandled exception thrown: write access violation.


    **_Left** was 0xCCCCCCCC. occurred

and in xstring file I see this:

     static _CONSTEXPR17 void assign(_Elem& _Left, const _Elem& _Right) noexcept {
            _Left = _Right;
        }

Could anyone help me understand why, when conversionType is 3 this error is thrown and not when it is 2? I have tested to make sure that conversionType actually stores the value before the function call.

EDIT: The advice to remove all irrelevant code was very useful as I see the error is not with the function call as I had assumed. It is in the conversion formula for hex. As Igor pointed out, I messed up the formula and when I put in a fix the exception is no longer thrown. I honestly cant recall a good reason as to why I thought the error was in the function call but I am at a novice level after all. Thank you all for taking the time to look at my question and to comment.

CodePudding user response:

EDIT: seems you figured it out while i was writing answer.

explantion in the bottom of answer. you should replace this:

if (numType == 1 && conversionType == 3)
{
    string hexNum[64];
    int i = 0;
    while (num != 0)
    {
        int tempVal;
        tempVal = (num % 16);
        if (tempVal < 10)
        {
            num  = 48;
        }
        else
        {
            num  = 55;
        }
        hexNum[i] = num;
        i  ;
        num /= 16;
    }
    for (i -= 1; i > 0; i--)
    {
        cout << hexNum[i];
    }
    cout << endl;
}

to this:

if (numType == 1 && conversionType == 3)
{
    string hexNum[64];
    int i = 0;
    while (num != 0)
    {
        int tempVal;
        tempVal = (num % 16);
        if (tempVal < 10)
        {
            tempVal  = '0'; // tempVal instead num and not use magic constant
        }
        else
        {
            tempVal  = 'A' - 10; // tempVal instead num and not use magic constant
        }
        hexNum[i] = tempVal; // tempVal instead num
        i  ;
        num /= 16;
    }
    for (i -= 1; i >= 0; i--) // >= instead > to not cut last hex digit
    {
        cout << hexNum[i];
    }
    cout << endl;
}

Explanation: you use num instead tempVal, so anyway num was increased by at least 16 (in one case 48 i other case 55), so when u divide num by 16 it never becomes zero, so i increases until haxNum[i] lies out of your stack and 0xCCCCCCCC was thrown.

Same explanation wrote @Igor Tandetnik in comments.

@Alan Birtles was right that it's stack acess issue

  •  Tags:  
  • Related