Hello folks! It has been a fair while since I came here, but alas I have some questions.
Alright, to start off, I have a structure with a constructor:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
typedef struct _Wl {
std::wstring category;
std::wstring name;
int rank;
inline _Wl(std::wstring, int);
} weaponList, WL;
inline _Wl::_Wl(std::wstring nameStr, int rankInt) : name(nameStr), rank(rankInt) {
for (int i = 0; i < 32; i++) { //determines category of weapon
if (name == categories::BattleRifles[i]) { //comparing strings
category = _T("Battle Rifles");
break;
}
//more cases...
}
}
|
Now, this is all fine and dandy (
100+ objects that have no problems with this struct).
However, something interesting on some certain objects occurs. For example take this innocent looking object:
static _Wl MJOLNIR(_T("Mjolnir"), NULL);
(Do note that the '_T("Mjolnir")' should be viewed as 'L"Mjolnir"'.)
Whenever I try to execute my program with this exact typing, I get an exception in the wide character library (wchar.h):
1 2 3 4 5 6 7 8 9 10 11 12
|
__inline int __CRTDECL wmemcmp(
_In_reads_(_N) wchar_t const* _S1,
_In_reads_(_N) wchar_t const* _S2,
_In_ size_t _N
)
{
for (; 0 < _N; ++_S1, ++_S2, --_N)
if (*_S1 != *_S2) //this line in particular
return *_S1 < *_S2 ? -1 : 1;
return 0;
}
|
Apparently, the function cannot read '_S2', but can read '_S1' ( L"Mjolnir"), _N (7), and *_S1 (77 ['M' in ASCII]) just fine. This is strange, so I thought it had to do with my code, but I searched my whole thing and couldn't find one single invocation of the wmemcmp() function.
So, I added
one space to the line in the string:
static _Wl MJOLNIR(_T("Mjolnir "), NULL);
And it suddenly threw no exceptions, leading me conclude that it could read the memory.
So that leads me to the question, why does a single space manage to differentiate an exception? Although I did not invocate the exact function, does my comparison operator invoke it indirectly? And if so, why does it throw that error?
Any help would be appreciated!