在C语言编程移动从32位的结构为64位 [英] moving a structure from 32-bit to 64-bit in C programming language

查看:622
本文介绍了在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。因此为了解决这个probem我想声明在64位spcace我的结构(以便没有spcace紧缩)和对象的数量可以incrased切实无限

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;

因此​​,如果previously我的变量,如X> B或XA访问,我怎样才能确保相同的值传送至64位的结构,无需在整个code功能的变化。

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.

如果做不到这一点,你可以经常更改你的源$ C ​​$ C,使这些问题最小化,如:

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;

和与合适的定义编译:

gcc -DLONG_IS_64BITS myprog.c

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

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