简体   繁体   English

Linux 和 Windows 上的输出差异

[英]Output difference on Linux and Windows

unsigned long id = 12;
unsigned long age = 14;
unsigned char* pData = new unsigned char[8];
memcpy(pData,&id,4);/* using memcpy to copy */
pData = pData + 4;
memcpy(pData,&age,4);/* using memcpy to copy */
std::cout<<*reinterpret_cast<unsigned long*>(pData)<<std::endl;
pData = pData - 4;
std::cout<<*reinterpret_cast<unsigned long*>(pData)<<std::endl;

Output on linux linux上的输出

14 60129542156 14 60129542156

on windows (vc++)在 Windows 上 (vc++)

14 12 14 12

You are making the assumption that sizeof(unsigned long) is always 4. This is not true.您假设sizeof(unsigned long)始终为 4。这是不正确的。

Try this:尝试这个:

const size_t NBYTES = sizeof(unsigned long);
unsigned long id = 12;
unsigned long age = 14;
unsigned char* pData = new unsigned char[2 * NBYTES];
memcpy(pData,&id, NBYTES);/* using memcpy to copy */
pData = pData + NBYTES;
memcpy(pData,&age, NBYTES);/* using memcpy to copy */
std::cout<<*reinterpret_cast<unsigned long*>(pData)<<std::endl;
pData = pData - NBYTES;
std::cout<<*reinterpret_cast<unsigned long*>(pData)<<std::endl;

If you want to use explicit data sizes, then use the types defined in <cstdint> .如果要使用显式数据大小,请使用<cstdint>中定义的类型。 Documentation can be found here .文档可以在这里找到。

memcpy(pData,&id,4);/* using memcpy to copy */
//               ^

You are assuming the sizeof (unsigned long) is 4 which may or may not be true on a system as it is implmentation dependent.您假设sizeof (unsigned long)4 ,这在系统上可能是也可能不是,因为它依赖于实现。

Use the following line for the consistent behaviour.使用以下行以获得一致的行为。

memcpy(pData,&id,sizeof id);/* using memcpy to copy */

Moreover the following line may cause problem if some day the size of your variables are more than 8. Change these also to sizeof此外,如果有一天变量的大小超过 8,以下行可能会导致问题。也将这些更改为sizeof

unsigned char* pData = new unsigned char[2 * sizeof (unsigned long)];
...
pData = pData + sizeof id;  // etc

On Windows, mostly for backward compatibility reasons, sizeof(long) is 4 even on 64 bit.在 Windows 上,主要是出于向后兼容性的原因,即使在 64 位上, sizeof(long)也是 4。 On linux sizeof(long) in x86-64 (the platform you're probably using) is instead 8 bytes.在 x86-64(您可能正在使用的平台)中的 linux sizeof(long)上改为 8 个字节。

在你的程序中使用 sizeof() 而不是 4。

unsigned long is different size if you are using different operating system.如果您使用不同的操作系统, unsigned long的大小会有所不同。

On 32 bit machines, usually unsigned long is 32 bit (4 bytes).在 32 位机器上,通常 unsigned long 是 32 位(4 字节)。
On 64 bit, usually unsigned long is 64 bit (8 bytes).在 64 位上,通常 unsigned long 是 64 位(8 字节)。

On top of that, it depends what is the C/C++ compiler.最重要的是,这取决于什么是 C/C++ 编译器。 Eg What is true for some compiler, might be not true for another.例如,对于某些编译器来说是正确的,对于另一个编译器可能不是正确的。

Last I do not see where you release the memory you allocated with new .最后,我看不到您在哪里释放使用new分配的内存。

Here is your code using sizeof():这是您使用 sizeof() 的代码:

#include <iostream>

#include <string.h>

int main(){
    unsigned long id = 12;
    unsigned long age = 14;
    size_t size = sizeof(unsigned long);
    unsigned char* pData = new unsigned char[2*size];

    memcpy(pData, &id, size);/* using memcpy to copy */

    pData = pData + size;

    memcpy(pData, &age, size);/* using memcpy to copy */

    std::cout<<*reinterpret_cast<unsigned long*>(pData)<<std::endl;

    pData = pData - size;

    std::cout<<*reinterpret_cast<unsigned long*>(pData)<<std::endl;

    // do you need to release the dynamic memory?
    delete pData;

    return 0;
}

Hope this helps.希望这可以帮助。 Comment if I miss something.如果我错过了什么,请发表评论。

UPDATE:更新:

I seems to missread the code.我似乎看错了代码。 I am updated the code.我更新了代码。

I will give you updated version where pointer arithmetic is used:我会给你使用指针算法的更新版本:

#include <iostream>

#include <string.h>

int main(){
    unsigned long id = 12;
    unsigned long age = 14;

    unsigned long* pData = new unsigned long[2];

    memcpy(pData, &id, sizeof(unsigned long));/* using memcpy to copy */

    pData++;

    memcpy(pData, &age, sizeof(unsigned long));/* using memcpy to copy */

    std::cout<<*reinterpret_cast<unsigned long*>(pData)<<std::endl;

    pData--;

    std::cout<<*reinterpret_cast<unsigned long*>(pData)<<std::endl;

    // do you need to release the dynamic memory?
    delete pData;

    return 0;
}

If I was you, I would do it like this - eg no sizeof, no & (address of).如果我是你,我会这样做 - 例如没有 sizeof,没有 &(地址)。

#include <iostream>

#include <string.h>

int main(){
    unsigned long id = 12;
    unsigned long age = 14;

    unsigned long *pData = new unsigned long[2];

    pData[0] = id;
    pData[1] = age;

    std::cout << pData[1] << std::endl;

    std::cout << pData[0] << std::endl;

    delete pData;

    return 0;
}

Hope this helps.希望这可以帮助。 Comment if I miss something.如果我错过了什么,请发表评论。

I saw the comment for Windows / Microsoft C++ and size of unsigned long.我看到了 Windows / Microsoft C++ 的评论和 unsigned long 的大小。 This might be true, but I still think sizeof() is the way to do it.这可能是真的,但我仍然认为 sizeof() 是这样做的方法。 With sizeof() code is more portable and might compile to different platform, even on platforms you have no knowledge about.使用 sizeof() 代码更具可移植性,并且可以编译到不同的平台,即使在您不了解的平台上也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM