【问题标题】:Packing a struct into a long in C在C中将结构打包成long
【发布时间】:2013-10-23 20:05:02
【问题描述】:

我有一个包含两个整数的struct

struct ref{
  int next;
  int marked;
}

我需要使用 CAS 原子地比较其中两个结构。

__sync_bool_compare_and_swap(&(ref1),old,new);

我如何转换这个struct 以便在c 中将其引用为long long

我试过了:

    __sync_bool_compare_and_swap(&((long long)(ref1)),(long long)(old),(long long)(new));

但它给了我这个错误:

aggregate value used where an integer was expected

【问题讨论】:

  • 类似于 * (( long long* ) &ref1) 的东西?
  • Casting struct into int 的可能重复项
  • 过去在 IBM 被称为“U 字”。
  • 你能说“联合”吗?当然可以。

标签: c casting struct


【解决方案1】:

假设ref1 是结构类型ref 的一个实例,并且oldnew 已经是long long 类型,您应该能够:

__sync_bool_compare_and_swap((long long *)&ref1, old, new);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-29
    • 1970-01-01
    • 2023-04-02
    • 2015-07-31
    相关资源
    最近更新 更多