64位 内存对齐问题
MSDN中的一段话:
On 64-bit Windows, if a data structure is misaligned, routines that manipulate the structure, such as RtlCopyMemory and memcpy, will not fault. Instead, they will raise an exception. For example:
Copy
#pragma pack (1) /* also set by /Zp switch */
struct Buffer {
ULONG size;
void *ptr;
};
void SetPointer(void *p) {
struct Buffer s;
s.ptr = p; /* will cause alignment fault */ //为什么这里有问题?
...
}
You could use the UNALIGNED macro to fix this:
Copy
void SetPointer(void *p) {
struct Buffer s;
*(UNALIGNED void *)&s.ptr = p;
}
Unfortunately, using the UNALIGNED macro is very expensive on Itanium-based processors. A better solution is to put 64-bit values and pointers at the beginning of the structure.
问题:
s.ptr = p;有什么问题?
我的理解是:原来64位是8字节对齐,现在#pragma pack (1)后就不会在size后填充4个字节,s.ptr = p;中“.”操作仍然会从s地址的第8个字节开始读,造成错误。不知道MSDN是不是这个意思?求教~
On 64-bit Windows, if a data structure is misaligned, routines that manipulate the structure, such as RtlCopyMemory and memcpy, will not fault. Instead, they will raise an exception. For example:
Copy
#pragma pack (1) /* also set by /Zp switch */
struct Buffer {
ULONG size;
void *ptr;
};
void SetPointer(void *p) {
struct Buffer s;
s.ptr = p; /* will cause alignment fault */ //为什么这里有问题?
...
}
You could use the UNALIGNED macro to fix this:
Copy
void SetPointer(void *p) {
struct Buffer s;
*(UNALIGNED void *)&s.ptr = p;
}
Unfortunately, using the UNALIGNED macro is very expensive on Itanium-based processors. A better solution is to put 64-bit values and pointers at the beginning of the structure.
问题:
s.ptr = p;有什么问题?
我的理解是:原来64位是8字节对齐,现在#pragma pack (1)后就不会在size后填充4个字节,s.ptr = p;中“.”操作仍然会从s地址的第8个字节开始读,造成错误。不知道MSDN是不是这个意思?求教~
...全文
相关推荐
pe.rar_64 PE_The Work
the fact that pe32 isn t padded where pe32+ is 64-bit means union won t work right.
完美的内存中加载DLL支持32位和64位DLL
一位高手写的内存中加载DLL的源代码,支持32位和64位的DLL,支持XE,完美的处理32/64位Windows动态库的加载模,网上很少能下载到了,放到这里备份一下
易语言对64位程序内存读写操作纯源码-易语言
易语言对64位程序内存读写操作纯源码
对64位程序进行内存读写操作模块源码-易语言
对64位程序进行内存读写操作模块源码
内存对齐 详细介绍内存对齐的原理
关于内存对齐的很详细的解析.内容有什么是内存对齐,为什么需要内存对齐,编译器对内存对齐的处理,栈内存对齐等,并附有验证代码.值得一看.


