How to use union.

Union is a datatype in c/c++, but rarely used in practice, because I almost think the memory is enough now, don't need such trick. Recently I have read the source code of NFA , using union very beautiful.

 How to use union.
Wen construct a NFA , we have a pushdown stack , record the NFA fragment had been constructed.
How to use union.
When the construct is not completed, we have to store all the fragments, but how to store them? It is easy to see, we only need to store the start state and the out states of the fragment.
We may define the fragment as:
struct Frag
{
     state   *start;
     Ptrlist  *out;
};
 
It's easy to see , Ptrlist is a linklist of state.ok , we define Ptrlist as:
struct Ptrlist
{
     state *s;
     Ptrlist *next;
};
But to NFA , when the fragment not link to others , the out edge is not sure link to where,  and when linked to others we don't need this out state linklist.so we can use union.
union xy
{
     int a;
     int b;
     int c;
};
the both a,b,c shared the memory. We can define Ptrlist as
union Ptrlist
{
     state *s;
     Ptrlist *next;
};
At beginning using *next as a linklist , and then when link out to others using *s.

原文链接: https://www.cnblogs.com/x1957/archive/2013/01/18/2866008.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    How to use union.

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/76018

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 下午5:18
下一篇 2023年2月9日 下午5:18

相关推荐