问题
My problem is, I want to move a structure form 32-bits space to 64-bits space.
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 ?
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
suppose we have a structure in 32 bit space
typedef struct x{
int a ;
int *b ;
} x_t;
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;
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.
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 ??
回答1:
You can copy data just fine between 32-bit and 64-bit, provided you use compatible data types.
For example, the structure:
struct xyzzy {
int a;
int b;
}
won't be compatible if your int
types are of different widths in the 32-bit and 64-bit world.
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
来源:https://stackoverflow.com/questions/17965142/moving-a-structure-from-32-bit-to-64-bit-in-c-programming-language