用 C 语言将结构从 32 位移动到 64 位 [英] moving a structure from 32-bit to 64-bit in C programming language

查看:22
本文介绍了用 C 语言将结构从 32 位移动到 64 位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,我想将一个结构从 32 位空间移动到 64 位空间.

My problem is, I want to move a structure form 32-bits space to 64-bits space.

假设我在具有相同字段的 64 位空间中声明了相同的结构,有没有办法将相应的字段从 32 位结构复制到 64 位结构?

Suppose I declare same structure in 64-bits space with same fields, is there a way to copy the corresponding fields from 32-bit to 64-bit structure ?

让我介绍一下实际问题,这与缓存有关,我们在 ram 上的大小有限,即 32 位空间,因此如果我们在 ram 上有 1G 空间并且我的单个缓存对象的结构大小为 1M那么我可以存储的对象数量限制为 1G/1M.因此为了解决这个问题,我想在 64 位空间中声明我的结构(以便没有空间紧缩)并且对象的数量实际上可以增加到无限

let me introduce to the actual problem, this is related to caching stuff, we have limited size on ram that is a 32 bit space, so if we have 1G space on ram and my structure size for a single cached object is 1M then the number of objects i can store is limited to 1G/1M. therefore to solve this probem i want to declare my structure in 64 bits spcace (so that there is no spcace crunch ) and number of objects can be incrased to infinite practically

假设我们有一个 32 位空间的结构

suppose we have a structure in 32 bit space

typedef struct x{
int a ;
int *b ;
} x_t;

现在我想在 64 位空间中移动这个结构,删除 32 位空间结构.

now i want to move this structure in 64 bit space, remove the 32 bit space structure.

typedef struct x_64{
    int a ;
    int *b ;
    } x_64_t;

所以,如果以前我的变量像 x->b 或 x.a 一样被访问,我如何确保将相同的值传输到 64 位结构,而不改变整个代码的功能.

so, if previously my variables were accessed like x->b or x.a, how can i make sure the same values are transmitted to 64 bit structure, without the change in functionality of entire code.

一种方法可以是在 64 位空间中有一个缓冲区,对于 32 位空间中的变量的每次访问,都会对缓冲区进行 64 次写入/读取.但这是一个乏味的过程,有没有其他选择??

One method can be to have a buffer in 64bit space, for every access of variable in 32bit space go to 64 write/read into the buffer. but that is a tedious process, can there be some alternative ??

推荐答案

您可以在 32 位和 64 位之间很好地复制数据,前提是您使用兼容的数据类型.

You can copy data just fine between 32-bit and 64-bit, provided you use compatible data types.

例如结构:

struct xyzzy {
    int a;
    int b;
}

如果您的 int 类型在 32 位和 64 位世界中具有不同的宽度,则将不兼容.

won't be compatible if your int types are of different widths in the 32-bit and 64-bit world.

C99 提供了固定宽度类型,例如 int32_t,无论平台如何,它们的大小都相同.如果您的编译器兼容,则需要提供这些类型,前提是它具有该大小的实际类型.

C99 provided fixed width types such as int32_t which are meant to be the same size regardless of platform. If your compiler is compliant, it is required to provide those types if it has an actual type of that size.

如果做不到这一点,您可以经常更改源代码以将这些问题最小化,例如:

Failing that, you can often change your source code so that these problems are minimised, such as with:

#ifdef INT_IS_64BITS
    typedef int my_64bit_type;
#endif

#ifdef LONG_IS_64BITS
    typedef long my_64bit_type;
#endif

my_64bit_type xyzzy;

并使用合适的定义进行编译:

and compile with a suitable definition:

gcc -DLONG_IS_64BITS myprog.c

这篇关于用 C 语言将结构从 32 位移动到 64 位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆