1

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

4

1 回答 1

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

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

你想做的应该是

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

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

于 2017-01-06T16:14:35.507 回答