64位系统如何添加n个字节
我正在 64 位 x_86 64 位 OSX 系统中工作。
我正在阅读旧数据库的文件。 它被加载到内存块并使用读取到结构的偏移量。 它是以32位模式编写的。
所以为了在64位模式下正确读取, 我想将 n 个字节添加到结构的基地址。
由于指针增量
增量不能帮助我做到这一点,因为它处于 64 位模式,所以每个指针都是 b 字节长。
问候, 达那。
我在这里发布了一些代码。我觉得是对的..
struct CamNodeTag {
CamNodeTag *nextCam; // next cam
SInt32 numMake;
char *menuMake;
};
long pBaseAddr; // this is long so we an offset bytes from it.
//Get the size of of the structure in 32 bit environment.//in 64 bit envi it is 20 bytes.
int CamNodeTagSizeIn32BitMode = 12;
//Do it in a while loop..
int i=0;
CamNodeTag * pNode = (CamNodeTag *)(pBaseAddr + i*CamNodeTagSizeIn32BitMode);
while(pNode !=NULL)
{
//Do the cam stuff here..
// to get the next node, we add the base addr to the offset
//This will give next cam
i++;
pNode = (CamNodeTag *)(pBaseAddr + i*CamNodeTagSizeIn32BitMode);
}
I am working in 64 bit x_86 64 bit OSX system.
I am reading the file of legacy database.
It is loaded to a memory block and using offsets it read to the structures.
It is written in 32 bit mode.
So in order to read properly in 64 bit mode,
I want to add n bytes to the base address of a structure.
Since pointer incement
increment does not help me to do it As it is in 64 bit mode every pointer is b byte long.
Regards,
Dhana.
I have posted some code here. I guess it is right..
struct CamNodeTag {
CamNodeTag *nextCam; // next cam
SInt32 numMake;
char *menuMake;
};
long pBaseAddr; // this is long so we an offset bytes from it.
//Get the size of of the structure in 32 bit environment.//in 64 bit envi it is 20 bytes.
int CamNodeTagSizeIn32BitMode = 12;
//Do it in a while loop..
int i=0;
CamNodeTag * pNode = (CamNodeTag *)(pBaseAddr + i*CamNodeTagSizeIn32BitMode);
while(pNode !=NULL)
{
//Do the cam stuff here..
// to get the next node, we add the base addr to the offset
//This will give next cam
i++;
pNode = (CamNodeTag *)(pBaseAddr + i*CamNodeTagSizeIn32BitMode);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议您使用自己编写的函数(例如
read_uint32_whatever_endian(FILE*)
等)从磁盘读取文件,并将它们存储在新的 64 位内存中<代码>结构。这可以将您的新代码与编译器对结构的内存布局所做的选择隔离开来。
在现代机器上,这种解析的性能成本非常小,我相信您几乎无法衡量它。
虽然有一个轻微的极端情况,其中
nmap
ed大型数据库文件存储与编译器内存中表示相同的二进制结构是一个优点,但这种情况不值得很多在实践中。磁盘上到内存中的不同序列化的好处提供了很多实用的优点:
I recommend instead that you read the file from disk using functions that you write such as
read_uint32_whatever_endian(FILE*)
and such, and store these in your new 64bit in-memorystruct
s.This insulates your new code from the choices the compiler makes about the memory layout of your structures.
On a modern machine, the performance cost of such parsing is so minimal that I am sure you can hardly measure it.
Whilst there is a slight corner-case where
nmap
ed large database files that store the same binary structure as the compiler's in-memory representation is a plus, this case is not worth much in practice.The benefits of a different serialisation on disk to in memory provide plenty of practical pluses:
为了将指针前进除其本机大小之外的其他值,您必须强制转换为 char *。
要使用 64 位处理器读取使用 32 位值作为“指针”的文件,您必须重新定义结构,以便以前作为指针的字段的大小仍为 32 位。
In order to advance a pointer by something other than it's native size, you have to cast to char *.
To read from a file that uses 32 bit values as "pointers" using a 64 bit processor, you have to redefine your structures so that fields that used to be pointers are still 32 bits in size.