【问题标题】:Getting the value of a C 4-byte string as a uint将 C 4 字节字符串的值作为 uint 获取
【发布时间】:2017-01-06 16:09:58
【问题描述】:

简而言之,我的问题是:我正在构建一个动态内存管理器,其中包含各种不同类型的对象。我用标签标记每种不同类型的对象,并且为了使内存调试更容易,我希望这些标签在内存中显示为我可以读取的四字节字符串。但是,为了有效地打开这些值,我还想将它们视为无符号 32 位整数。

目前对象的定义如下:

/**
 * an object in cons space.
 */
struct cons_space_object {
  char tag[TAGLENGTH];         /* the tag (type) of this cell */
  uint32_t count;              /* the count of the number of references to this cell */
  struct cons_pointer access;  /* cons pointer to the access control list of this cell */
  union {
    /* if tag == CONSTAG */
    struct cons_payload cons;
    /* if tag == FREETAG */
    struct free_payload free;
    /* if tag == INTEGERTAG */
    struct integer_payload integer;
    /* if tag == NILTAG; we'll treat the special cell NIL as just a cons */
    struct cons_payload nil;
    /* if tag == REALTAG */
    struct real_payload real;
    /* if tag == STRINGTAG */
    struct string_payload string;
    /* if tag == TRUETAG; we'll treat the special cell T as just a cons */
    struct cons_payload t;
  } payload;
};

标签是四个字符串常量,例如:

#define CONSTAG  "CONS"

我希望能够做到这样的事情

switch ( cell.tag) {
  case CONSTAG : dosomethingwithacons( cell);
  break;

但是你当然不能打开一个字符串。但是,由于这些是四字节字符串,它们可以在内存中作为 32 位无符号整数读取。我想要的是一个宏,给定一个字符串作为参数,返回一个无符号整数。我试过了

/**
 * a macro to convert a tag into a number
 */
#define tag2uint(tag) ((uint32_t)*tag)

但实际上它所做的是将那个地址的第一个字符的 ASCII 值作为数字返回——也就是说,

tag2uint("FREE") => 70

这是'F'的ASCII码。

有人帮我解决这个问题吗?自从我用 C 写任何严肃的东西以来,已经有 20 年了。

【问题讨论】:

    标签: c string uint32 uint32-t


    【解决方案1】:
    #define tag2uint(tag) ((uint32_t)*tag)
    

    表示“取消引用tag(在您的示例中获取'F'),然后将其转换为uint32_t。”

    你想做的应该是

    #define tag2uint(tag) (*(uint32_t*)tag)
    

    这意味着“将tag 视为指向uint32_t 的指针,然后取消引用它。”

    【讨论】:

      猜你喜欢
      • 2015-04-24
      • 2011-05-11
      • 1970-01-01
      • 2022-10-16
      • 1970-01-01
      • 1970-01-01
      • 2014-02-02
      • 1970-01-01
      • 2013-04-02
      相关资源
      最近更新 更多