'The callstack always shows _errno after strtol in WPA

I'm study WPA. I wrote a simple program and used WPR to collect the etl. My program is very simple, just trying to covert string to int in a loop.

#include <string>

using namespace std;
 
int main()
{

    string d = "99999";

    for(int i = 0 ; i < 9999999 ; i++)
        int index = stoi(d);
    
} 

However, from the callstack in WPA, there is always _errno after strtol.

Callstack in WPA

Check the source code of stoi in string. I only can see the call of strtol, not sure which line will cause _errno.

inline int stoi(const string& _Str, size_t* _Idx = nullptr, int _Base = 10) {
    // convert string to int
    int& _Errno_ref  = errno; // Nonzero cost, pay it once
    const char* _Ptr = _Str.c_str();
    char* _Eptr;
    _Errno_ref      = 0;
    const long _Ans = _CSTD strtol(_Ptr, &_Eptr, _Base);

    if (_Ptr == _Eptr) {
        _Xinvalid_argument("invalid stoi argument");
    }

    if (_Errno_ref == ERANGE || _Ans < INT_MIN || INT_MAX < _Ans) {
        _Xout_of_range("stoi argument out of range");
    }

    if (_Idx) {
        *_Idx = static_cast<size_t>(_Eptr - _Ptr);
    }

    return static_cast<int>(_Ans);
}

Any assistance you can provide would be greatly appreciated. Thank you.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source