开发者

Why escaping 'w' ('\w') character reverses memory representation of a int variable?

开发者 https://www.devze.com 2023-04-07 13:33 出处:网络
Consider the following code sample: int i1 = \'w\\\"\'; int i2 = \'\\w\\\"\'; int i3 = \'w\"\'; int i4 = \'w\\\"\';

Consider the following code sample:

int i1 = 'w\"';
int i2 = '\w\"';
int i3 = 'w"';
int i4 = 'w\"';

Note: MSVS SP1 2005 C++ compiler, just default debug compilation/linkage settings. x86 machine. The compiler outputs warning C4129: 'w' : unrecognized character escape sequence and everything else is just fine.

The raw memory representation of the given variables are as follows:

i1 -> 22 77 00 00
i2 -开发者_JAVA百科> 77 22 00 00
i3 -> 22 77 00 00
i4 -> 22 77 00 00

Why i2 has reverse order? Whats going on??


It's a bug in the compiler. I suggest you file a bug on Microsoft Connect (though I wouldn't bet on them fixing it any time soon). It also occurs with real escape sequences such as \n or \x6e, so it has nothing to do with the invalid escape sequence \w.

In VS 2008 and VS 2010, the output of this program:

#include <stdio.h>

int main(void)
{
    int x[] = {'abn"', 'abn\"', 'ab\x6e"', 'ab\x6e\"'};
    for (int i = 0; i < sizeof(x)/sizeof(x[0]); i++)
        printf("%08x\n", x[i]);

    return 0;
}

is this:

61626e22
61626e22
61626e22
2261626e

This shows that for some strange reason, the compiler moves the quotation mark (the 22) to the beginning of the multicharacter character constant, but only when it's escaped and when the constant has another escaped character in it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号