c语言用void修饰一个地址,C语言 使用uint64_t作为内存的通用地址而不是void *的利弊是什么?...

我是C编程的新手.我发现uint64_t在libibverbs中用作缓冲区的地址而不是void *.

struct ibv_sge{

uint64_t addr;

uint32_t length;

uint32_t lkey;

}

我能想到的一个缺点是它的便携性.在32位系统上,它将导致编译错误或至少浪费内存.那为什么呢?有什么好处吗?

我还注意到还有其他替代方法,例如uintptr_t.描述通用内存地址的最佳方法是什么?

pros and cons of using uint64_t as an universal address of memory

缺点:

>当通用地址较窄时,可能会有不必要的宽度.

>当需要更广泛的通用地址时,可能会不必要地缩小范围.

C没有通用地址.最接近的是void *,但只指定对象指针就足够了. void *可能不足以用于函数指针.任何函数指针类型与另一个函数指针类型的大小相同.

A pointer to void may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer. C11dr §6.3.2.3 1

A pointer to a function of one type may be converted to a pointer to a function of another type and back again; the result shall compare equal to the original pointer. §6.3.2.3 8

What’s the best approach to describe an universal address of memory?

用C代替使用union.

union u_pointer {

void *obj;

int (*fun)();

};

读取和写入这些成员的访问权限需要小心处理,就像任何通用指针一样.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值