Search code examples
c++pointersmemset

What is the result of assigning a ULONGLONG to a 16 byte array?


I'm a Java developer with very little knowledge of pointers. I have the following C++ code (that someone else wrote) that I'm trying to understand:

ULONGLONG passedValue;
BYTE myArr[16];
memset(myArr, 0x00, sizeof myArr);
*((ULONGLONG *) myArr) = passedValue;

While I understand that the memset method call fills up the myArr byte array (of size 16) with 0s, I'm not sure what the last line does.

According to MSDN, ULONGLONG is 8 bytes...whereas myArr is 16 bytes. What will be in myArr after the last statement is executed?


Solution

  • This term (ULONGLONG *) myArr will convert myArr to a pointer to ULONGLONG type.

    Then, *((ULONGLONG *) myArr) is the value of where the pointer is pointing to.

    And,

    *((ULONGLONG *) myArr) = passedValue;
    

    assigns passedValue to the first 8 byte of myArr BYTE array.

    passedValue 00 00 00 00 00 00 00 00
    ^^^^^^^^^^^
    8 bytes