64位字节顺序问题 [英] 64 bit Endianness issue

查看:113
本文介绍了64位字节顺序问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码只有64位大端机才有问题。

我的结构中有一个成员size_t val1。 size_t的大小是32位机器中的
32(unsigned int)和64位机器中的64(无符号长)

机器。


我将此变量的地址传递给另一个函数(其中
只接受指向unsigned int的指针)unsigned int *。基本上传递

无符号长指针而不是unsigned int指针。我认为

值在解除引用int *时会被截断。在这种情况下,如何使我的代码可移植?
像htonl / ntohl这样的函数会有帮助吗?

或者演员可以提供任何帮助吗?


谢谢,

Suresh

I have problem with the code only with 64 bit big endian machine.
i have a member size_t val1 in my structure. The size of size_t is
32(unsigned int) in 32 bit machines and 64(unsigned long) in 64 bit
machines.

I am passing address of this variable to a different function (where it
accepts only pointer to unsigned int) unsigned int *. Basically passing
a unsigned long pointer instead of unsigned int pointer. I think the
value is getting truncated while dereferencing int *. In this case how
do I make my code portable? Can functions like htonl/ntohl will help?
or casting could be of any help?

Thanks,
Suresh

推荐答案

在文章< 11 ********************** @ q12g2000cwa.googlegroups .com> ;,

< su *********** @ gmail.com>写道:
In article <11**********************@q12g2000cwa.googlegroups .com>,
<su***********@gmail.com> wrote:
我的代码只有64位大端机才有问题。
我的结构中有一个成员size_t val1。 size_t的大小在32位机器中为32(unsigned int),在64位机器中为64(unsigned long)。
我将此变量的地址传递给另一个函数(其中它只接受指向unsigned int的指针)unsigned int *。基本上传递
一个无符号长指针而不是unsigned int指针。我认为
值在解除引用int *时被截断。在这种情况下,如何使我的代码可移植?
I have problem with the code only with 64 bit big endian machine.
i have a member size_t val1 in my structure. The size of size_t is
32(unsigned int) in 32 bit machines and 64(unsigned long) in 64 bit
machines. I am passing address of this variable to a different function (where it
accepts only pointer to unsigned int) unsigned int *. Basically passing
a unsigned long pointer instead of unsigned int pointer. I think the
value is getting truncated while dereferencing int *. In this case how
do I make my code portable?




在64位机器上,可以将值存储在结构中

成员合法超过32位?如果他们可以,那么机会

是你唯一的选择是重写其他功能。

你无能为力远程迫使另一个例程运行比它编码的更多比特。

-

一切都是虚荣。 - 传道书



On the 64 bit machines, can the values to be stored in the structure
member legitimately exceed 32 bits? If they can, then chances
are your only choice is to rewrite the other function. There is
nothing you can do to "remotely" force another routine to operate on
more bits than it was coded for.
--
All is vanity. -- Ecclesiastes


su ****** *****@gmail.com 写道:
我的代码只有64位大端机才有问题。
我的结构中有一个成员size_t val1 。 size_t的大小在32位机器中为32(unsigned int),在64位
机器中为64(unsigned long)。

我将此变量的地址传递给a不同的函数(它只接受指向unsigned int的指针)unsigned int *。基本上传递
一个无符号长指针而不是unsigned int指针。我认为
值在解除引用int *时被截断。在这种情况下,我如何使我的代码可移植?像htonl / ntohl这样的功能可以提供帮助吗?
或者施法可以提供任何帮助吗?

谢谢,
Suresh
I have problem with the code only with 64 bit big endian machine.
i have a member size_t val1 in my structure. The size of size_t is
32(unsigned int) in 32 bit machines and 64(unsigned long) in 64 bit
machines.

I am passing address of this variable to a different function (where it
accepts only pointer to unsigned int) unsigned int *. Basically passing
a unsigned long pointer instead of unsigned int pointer. I think the
value is getting truncated while dereferencing int *. In this case how
do I make my code portable? Can functions like htonl/ntohl will help?
or casting could be of any help?

Thanks,
Suresh




这种情况​​是否允许以下​​情况?


struct {

size_t val1;

} my_struct;

int tmp = 0;

some_function_that_takes_a_pointer_to_an_int(& tmp);

if(tmp< 0)/ *做某事* /;

my_struct.val1 = tmp;


我假设some_function_that_takes_a_pointer_to_an_int()将取消引用稍后传递给它的指针。
在这种情况下,你仍然有一些选项,但这取决于你将如何使用

结构。


查理



Do the circumstances allow the following?

struct {
size_t val1;
} my_struct;
int tmp = 0;
some_function_that_takes_a_pointer_to_an_int(&tmp) ;
if (tmp < 0) /* do something */ ;
my_struct.val1 = tmp;

I assume some_function_that_takes_a_pointer_to_an_int() will be
dereferencing the pointer passed to it later. In this case you still
have some options, but it depends on how you are going to be using the
structure.

Charlie


就像..


struct {

size_t val1; //可以64位无符号长

} my_struct;

struct my_struct val;


func(& val。 val1);


void func(unsigned int *)

{

// val1可以参考

}


64位是否有解决方法?

It''s like..

struct {
size_t val1; // can be unsigned long in 64 bit
} my_struct;
struct my_struct val;

func(&val.val1);

void func(unsigned int *)
{
// val1 can be deferenced
}

Is there workaround in case of 64bit?


这篇关于64位字节顺序问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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