Why does memset take an int instead of a char?

后端 未结 3 1985
梦毁少年i
梦毁少年i 2020-11-28 09:21

Why does memset take an int as the second argument instead of a char, whereas wmemset takes a wchar_t instea

3条回答
  •  佛祖请我去吃肉
    2020-11-28 09:32

    See fred's answer, it's for performance reasons.

    On my side, I tried this code:

    #include 
    #include 
    
    int main (int argc, const char * argv[])
    {
        char c = 0x00;
    
        printf("Before: c = 0x%02x\n", c);
        memset( &c, 0xABCDEF54, 1);
        printf("After:  c = 0x%02x\n", c);
    
        return 0;
    }
    

    And it gives me this on a 64bits Mac:

    Before: c = 0x00
    After:  c = 0x54
    

    So as you see, only the last byte gets written. I guess this is dependent on the architecture (endianness).

提交回复
热议问题