关于memcpy

cc4Java 2016-08-05 09:38:40
int nread = 0;
data 是一个unsignd char数组
rlen = 4;
memcpy(&nread, data + rlen, sizeof(uint32_t));

为什么在mac 32位机上nread在内存拷贝后是一个正常的int值,而在64位上是一个很大的int值?
有什么办法能让他在32和64上返回一样的值吗?
...全文
217 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
cc4Java 2016-08-06
  • 打赏
  • 举报
回复
能帮我大致解释下吗 这段话
lm_whales 2016-08-06
  • 打赏
  • 举报
回复
这涉及到,int的长度和字节序 uint32_t 这是固定字节类型,应该是32Bits的 但是int类型,和平台相关 如果int 是32Bits,那么二者的值是一致的 如果,int 长度是64Bits 并且 字节序是,并且低字节在高地址 那么这么memcpy 的是在高4个字节上 这个数据,必然很大 另外,char数组,总是从低字节到高字节存放的 所以,即便是int 长度是32Bits 用char数组表示的数,和int 类型的关系,也跟字节序有关,而不是顺序对应的
xiaohuh421 2016-08-05
  • 打赏
  • 举报
回复
知道具体位数, 就按位数拷贝, 把每一位放到对应的字节上去.
sdghchj 2016-08-05
  • 打赏
  • 举报
回复
自己研究一下字节序吧
paschen 版主 2016-08-05
  • 打赏
  • 举报
回复
uint32_t 在不同平台下大小是不同的,改成sizeof(nread)
cc4Java 2016-08-05
  • 打赏
  • 举报
回复
sizeof(int)也不行,而且如果这样写的话, 拷贝的数就对不上了吧
sevancheng 2016-08-05
  • 打赏
  • 举报
回复
sizeof(int) 试试
赵4老师 2016-08-05
  • 打赏
  • 举报
回复
ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.chs/winprog64/winprog64/programming_guide_for_64-bit_windows.htm Programming Guide for 64-bit Windows Microsoft has released 64-bit versions of the Windows operating system, such as 64-bit Windows Vista, Windows XP Professional x64 Edition, and Windows Server 2003 R2 x64 Enterprise Edition. 64-bit Windows was designed with compatibility in mind. Developers can ensure that their existing 32-bit applications run well under 64-bit Windows or take advantage of the benefits of 64-bit Windows by migrating their applications. Benefits of 64-bit Windows A 64-bit operating system supports far more physical memory than a 32-bit operating system. For example, most 32-bit Windows systems support a maximum of 4 gigabytes of physical memory, with up to 3 gigabytes of address space for each process, while 64-bit Windows supports up to 2 terabytes of physical memory with 8 terabytes of address space for each process. The increased physical memory includes the following benefits for applications: Each application can support more users. All or part of each application must be replicated for each user, which requires additional memory. Each application has better performance. Increased physical memory allows more applications to run simultaneously and remain completely resident in the system's main memory. This reduces or eliminates the performance penalty of swapping pages to and from disk. Each application has more memory for data storage and manipulation. Databases can store more of their data in the physical memory of the system. Data access is faster because disk reads are not necessary. Applications can manipulate large amounts of data easily and more reliably. Video composition for motion picture work requires 64-bit Windows for this reason. Modeling for scientific and financial applications benefits greatly from memory-resident data structures that are not possible on 32-bit Windows. There are also important benefits for businesses: Increased productivity. Knowledge workers can spend their time thinking and producing, rather than waiting for the software to finish its tasks. Lower cost of ownership. Each server can support larger numbers of users and applications, so your business will require fewer servers. This translates directly into less management overhead—one of the highest costs in any computing environment. New application opportunities. New applications can be designed without the barriers imposed by 32-bit Windows. New graphics applications will make work easier and more enjoyable. Data-intensive tasks that are impossible today can be done with 64-bit Windows. In this Section Getting Ready for 64-bit Windows Designing 64-bit Compatible Interfaces Running 32-bit Applications Migration Tips Send comments about this topic to Microsoft Build date: 8/10/2007

64,663

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧