1

わかりました...厳密なエイリアシングをオン(および-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
ありがとう!!! 事前に(私は数時間試しました...)

編集:
それはクラッシュします。
基本的に何が悪いのかわからないので、コードパスを取り除いて再試行することで物事を絞り込もうとしました...しかし、それは私をどこにも連れて行きませんでした。

編集:いくつかのコメントを追加しました

4

2 に答える 2

0

これはバグのようです。GCC4.6.1で動作します。質問を閉じたいだけです...

于 2011-11-01T10:34:07.383 に答える
0

ここで質問があります。なぜ anintと avector<int>*を和集合に入れるのですか? これにより、共用体の両方のメンバーが同じメモリを共有します (つまり、value上書きの更新vとその逆)。

むしろ構造体を使用したいようです:

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

これにより、オブジェクトがメモリ内に順番に配置されます (つまりv、「上」ではなく、「 」になります)。valuevalue

また、この場合、ユニオンに対する制限がなくなったため、法線ベクトルを使用したい場合があります。

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

しかし、それはあなた次第です。

編集

ビリーのコメントによると、物事をよりシンプルに保ち、構造体を素晴らしくフラットにすることもできます。

struct S
{
    enum
    {
        leaf,
        node
    } type;

    int value;
    vector<S*> *v
};
于 2011-09-06T17:22:31.153 に答える