c ++严格别名问题---让我抓狂

时间:2011-09-06 16:56:58

标签: c++ unions strict-aliasing

好的......我有点绝望地试图让这段代码在打开严格别名(和-O3)的情况下工作。
我无法缩短代码(sry ......)所以它相当长~170行...

struct S
{
    enum
    {
        leaf,
        node
    } type; // the type of the structure. leaf means has a value. node has vector.

    union
    {
        int value;
        std::vector<struct S*> *v;
    } data; // the data. only one is active dependant on the type
};

//compares two structs
bool cmpStructs( const struct S *s1, const struct S *s2 );
//compares the 'top levels' i.e. type and if leaves then also the value
bool cmpStructs1( const struct S *s1, const struct S *s2 );

int main( void )
{
    // Create the structure: s1 = [1,2] s2 = [1,3]. Just some random stuff
    struct S *s1 = new struct S;
    struct S *s2 = new struct S;

    s1->type = s2->type = S::node;

    s1->data.v = new std::vector<struct S*>( 2U );
    s2->data.v = new std::vector<struct S*>( 2U );

    struct S *t = new struct S;
    t->type = S::leaf;
    t->data.value = 1;
    s1->data.v->front() = t;

    t = new struct S;
    t->type = S::leaf;
    t->data.value = 2;
    s1->data.v->back() = t;

    t = new struct S;
    t->type = S::leaf;
    t->data.value = 1;
    s2->data.v->front() = t;

    t = new struct S;
    t->type = S::leaf;
    t->data.value = 3;
    s2->data.v->back() = t;

    //compare s1 and s2. Note: the result is actually not important. the problem is the crash.
    if( cmpStructs( s1, s2 ) )
        std::cout << "equal" << std::endl;
    else
        std::cout << "not equal" << std::endl;

    return 0;
}

bool cmpStructs( const struct S *s1, const struct S *s2 )
{
    // compare 'top-level'
    if( cmpStructs1( s1, s2 ) == false )
        return false;
    // i.e. s1->type == s2->type and s1->value == s2->value
    if( s1->type != S::node )
        return true;
    // different vector sizes don't compare the same
    if( s1->data.v->size() != s2->data.v->size() )
        return false;
    // used to iterate over all elements in the tree structure of struct S
    struct const_iteratorList
    {
        std::vector<struct S*>::const_iterator it, end;
        struct const_iteratorList *next, *previous;
    } l1, l2, *c1, *c2;

    c1 = &l1;
    c2 = &l2;

    bool equal = true;

    c1->it = s1->data.v->begin();
    c1->end = s1->data.v->end();

    c2->it = s2->data.v->begin();
    c2->end = s2->data.v->end();

    c1->previous = c2->previous = c1->next = c2->next = NULL;

    do
    {
        while( c1->it != c1->end )
        {// This is where it crashes. Though basically the same stuff as above
            if( cmpStructs1( *(c1->it), *(c2->it) ) == false )
            {
                equal = false;
                break;
            }

            if( (*(c1->it))->type != S::node )
            {
                ++(c1->it);
                ++(c2->it);
                continue;
            }

            if( (*(c1->it))->data.v->size() != (*(c2->it))->data.v->size() )
            {
                equal = false;
                break;
            }
            // since *(c1->it) is not a leaf we need to look into its subnodes
            c1->next = new struct const_iteratorList;
            c2->next = new struct const_iteratorList;

            c1->next->it = (*(c1->it))->data.v->begin();
            c1->next->end = (*(c1->it))->data.v->end();

            c2->next->it = (*(c2->it))->data.v->begin();
            c2->next->end = (*(c2->it))->data.v->end();

            c1->next->previous = c1;
            c2->next->previous = c2;
            c1 = c1->next;
            c2 = c2->next;

            c1->next = c2->next = NULL;
        }

        if( c1->previous != NULL )
        {
            c1 = c1->previous;
            c2 = c2->previous;

            delete c1->next;
            delete c2->next;

            ++(c1->it);
            ++(c2->it);
        } else
            break;
    } while( equal == true );

    while( c1->previous != NULL )
    {
        c1 = c1->previous;
        c2 = c2->previous;

        delete c1->next;
        delete c2->next;
    }

    return equal;
}

bool cmpStructs1( const struct S *s1, const struct S *s2 )
{
    if( s1->type == S::node )
    {
        if( s2->type == S::node )
            return true;
    } else
    {
        if( s2->type == S::node )
            return false;

        if( s1->data.value == s2->data.value )
            return true;
    }

    return false;
}

这个问题很容易描述:它没有-fstrict-aliasing而且可以打破它 '休息'我的意思是'崩溃'。 请帮助我在两种情况下都能使用它:P
谢谢!!!提前(我已经尝试了几个小时......)

编辑:
它崩溃了 基本上我不知道什么是错的,所以我试图通过取走代码路径并重试来缩小范围......但它并没有把我带到任何地方。

编辑:添加了一些评论

2 个答案:

答案 0 :(得分:0)

以下是一个问题:为什么要在联盟中放置intvector<int>*?这使得联盟的两个成员共享相同的内存(即更新value覆盖v,反之亦然。)

在我看来,你宁愿使用结构:

struct
{
    int value;
    std::vector<struct S*> *v;
} data;

这会在内存中按顺序放置对象(即v现在 value之后,而不是“value之上”。

此外,在这种情况下,您可能只想使用法向量,因为您不再对联盟有限制:

struct
{
    int value;
    std::vector<struct S*> v;
}

但这取决于你。

修改

根据比利的评论,你也可以让事情变得更简单,并使结构变得美观和扁平:

struct S
{
    enum
    {
        leaf,
        node
    } type;

    int value;
    vector<S*> *v
};

答案 1 :(得分:0)

这似乎是一个错误。它适用于GCC 4.6.1。只是想关闭这个问题......