工会与空指针 [英] union versus void pointer

查看:120
本文介绍了工会与空指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是简单地用一个void *,而不是一个联盟之间的区别是什么?例如:

What would be the differences between using simply a void* as opposed to a union? Example:

struct my_struct {
    short datatype;
    void *data;
}

struct my_struct {
    short datatype;
    union {
        char* c;
        int* i;
        long* l;
    };
};

这些都可以用来实现完全一样的东西,是它更好地使用工会或void *的关系吗?

Both of those can be used to accomplish the exact same thing, is it better to use the union or the void* though?

推荐答案

我在我们的图书馆正是如此。我们有可能使用不同大小的索引,8,16或32位(历史原因)的通用串映射模块。因此,code满code这样的:

I had exactly the case in our library. We had a generic string mapping module that could use different sizes for the index, 8, 16 or 32 bit (for historic reasons). So the code was full of code like that:

if(map->idxSiz == 1) 
   return ((BYTE *)map->idx)[Pos] = ...whatever
else
   if(map->idxSiz == 2) 
     return ((WORD *)map->idx)[Pos] = ...whatever
   else
     return ((LONG *)map->idx)[Pos] = ...whatever

有这样的行100。在第一步中我改变了它的工会,我发现它是更具可读性。

There were 100 of lines like that. In a first step I changed it an union and I found it was more readable.

switch(map->idxSiz) {
  case 1: return map->idx.u8[Pos] = ...whatever
  case 2: return map->idx.u16[Pos] = ...whatever
  case 3: return map->idx.u32[Pos] = ...whatever
}

这alowed我看到更好发生了什么事情,然后我可以决定完全去除idxSiz变种只使用32位的索引。但这只是可能,一旦code得到了更多可读性。
PS:这只是我们的项目大约是几百几千由谁不存在了的人写code线的一小部分。所以在code中的变化是渐进的,以便不破坏的应用程序。

This alowed me to see better what was going on and I could then decide to remove completely the idxSiz variants using only 32 bit indexes. But this was only possible once the code got more readable. PS: That was only a minor part of our project which is about several 100 thousands of line of code written by people who do not exist anymore. So the changes in code are gradual so as not to break the applications.

结论:即使人到工会变异较少用到,我preFER它,因为它可以使code轻得多阅读。在大项目,这是非常重要的,使code更具可读性,哪怕是自己以后阅读。

Conclusion: Even if people are less used to the union variant, I prefer it because it can make the code much lighter to read. On big projects, it is extremely important to make the code more readable, even if it is yourself that will read it later.

修改:添加注释,为注释不格式code:

Edit: Added the comment, as comments do not format code:

切换变化到来之前(这是现在真正的code,因为它是)

The change to switch came before (this is now the real code as it was)

switch(this->IdxSiz) { 
  case 2: ((uint16_t*)this->iSort)[Pos-1] = (uint16_t)this->header.nUz; break; 
  case 4: ((uint32_t*)this->iSort)[Pos-1] = this->header.nUz; break; 
}

改成

switch(this->IdxSiz) { 
  case 2: this->iSort.u16[Pos-1] = this->header.nUz; break; 
  case 4: this->iSort.u32[Pos-1] = this->header.nUz; break; 
}

我不应该结合所有的美化我在code做,只表明一步。但是,我贴我的回答从家里,我不得不到code没有访问

I shouldn't have combined all the beautification I did in the code and only show that step. But I posted my answer from home where I had no access to the code

这篇关于工会与空指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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