locked
correct Pointer Truncation and Corruption issues when porting a 32-bit application to 64-bit RRS feed

  • Question

  • This is the original C++ code:
    void *AddToBuf(void *buf, UInt32 v) {
        MemCopy(buf, &v, sizeof(v));
        return (void *)((unsigned long)buf + sizeof(v));
    }

    //...
        fprintf(stderr, ": 0x%x.\n", (int) retCode);

    To port from 32-bit to 64-bit on windows server 2008 R2, is this code good?
    #ifdef OS_WIN64
    void *AddToBuf(void *buf, UInt32 v) {
        MemCopy(buf, &v, sizeof(v));
        return (void *)((size_t)buf + sizeof(v));
    }

    //...
        fprintf(stderr, ": 0x%I64x.\n", retCode);
    #else
    void *AddToBuf(void *buf, UInt32 v) {
        MemCopy(buf, &v, sizeof(v));
        return (void *)((unsigned long)buf + sizeof(v));
    }

    //...
        fprintf(stderr, ": 0x%x.\n", (int) retCode);

    #endif

    • Edited by PowahW Tuesday, August 11, 2009 10:16 PM
    Tuesday, August 11, 2009 9:47 PM

Answers

  • You do not need to put size_t inside a #ifdef.
    size_t is automatically 32 bit in win32 and 64 bit in x64.

    «_Superman_»
    • Marked as answer by Wesley Yao Tuesday, August 18, 2009 7:41 AM
    Wednesday, August 12, 2009 2:32 AM