64位系统如何添加n个字节

发布于 2024-08-21 00:12:16 字数 949 浏览 2 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

用心笑 2024-08-28 00:12:16

建议您使用自己编写的函数(例如read_uint32_whatever_endian(FILE*) 等)从磁盘读取文件,并将它们存储在新的 64 位内存中<代码>结构。

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

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

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

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

  • 它是可移植的 - 您可以在具有不同字大小和不同字节序的不同处理器上运行代码,没有问题
  • 您可以随时扩展结构 - 您可以将内存中的结构转换为具有方法等的对象,甚至虚拟 C++ 方法,以及面向对象设计的其他好处;您还可以添加不序列化的成员,例如指针和其他字段,并且可以轻松支持新的数据库文件版本

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-memory structs.

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 nmaped 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:

  • it's portable - you can run your code on different processors with different word-sizes and different endians without issues
  • you can extend the structures at any time - you could make the in-memory structures into objects with methods and such, even virtual C++ methods, and other benefits of object oriented design; you can also add members that don't get serialised, such as pointers and other fields, and you can support new database file versions easily
我乃一代侩神 2024-08-28 00:12:16

为了将指针前进除其本机大小之外的其他值,您必须强制转换为 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);

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.

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文