1

我在 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);

}
4

2 回答 2

1

建议您使用您编写的函数从磁盘读取文件read_uint32_whatever_endian(FILE*),然后将它们存储在新的 64 位内存中struct

这将您的新代码与编译器对结构的内存布局所做的选择隔离开来。

在现代机器上,这种解析的性能成本非常低,我相信你几乎无法衡量它。

虽然存在一个轻微的极端情况,nmap即存储与编译器的内存表示相同的二进制结构的大型数据库文件是一个优点,但这种情况在实践中并不值得。

从磁盘到内存的不同序列化的好处提供了许多实用的优点:

  • 它是可移植的 - 您可以在具有不同字长和不同字节序的不同处理器上运行代码而不会出现问题
  • 您可以随时扩展结构 - 您可以使用方法等,甚至虚拟 C++ 方法,以及面向对象设计的其他好处,将内存中的结构变成对象;您还可以添加未序列化的成员,例如指针和其他字段,并且您可以轻松支持新的数据库文件版本
于 2010-02-12T06:48:36.603 回答
1

为了使指针前进其本机大小以外的值,您必须强制转换为 char *。

要使用 64 位处理器从使用 32 位值作为“指针”的文件中读取,您必须重新定义您的结构,以便曾经是指针的字段仍然是 32 位大小。

typedef int Off32; // our "pointers" need to be 32 bit ints rather than pointers.

struct CamNodeTag { 
   Off32  nextCam; // next cam
   SInt32 numMake; 
   Off32  menuMake; 
}; 

char * pBaseAddr; // this is char * so we an offset bytes from it.

// set this to point to the first node.
CamNodeTag * pNode = (CamNodeTag *)(pBaseAddr + first_node_offset);

// to get the next node, we add the base addr to the offset
// in the structure.

pNode = (CamNodeTag *)(pBaseAddr + pNode->nextCam);

// assuming that the menuMake pointer is also an offset from the base
// use this code to get the actual pointer.
//
char * pMenu = (pBaseAddr + pNode->menuMake);
于 2010-02-12T06:58:15.083 回答