Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6753673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:11:26+00:00 2026-05-26T13:11:26+00:00

Okay… I’m getting kind of desperate trying to get this code to work with

  • 0

Okay… I’m getting kind of desperate trying to get this code to work with strict aliasing turned on (and -O3).
I was unable to shorten the code down (sry…) so it’s fairly long ~170 lines…

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;
}

The problem is easily described: It works without -fstrict-aliasing and breaks with it.
with ‘breaks’ I mean ‘crashes’.
Please help me get it to work in both cases 😛
Thanks!!! in advance (I’ve tried for some hours…)

EDIT:
It crashes.
Basically I have no idea of what could be wrong so I have tried to narrow things down by taking away code paths and retrying… But it didn’t take me anywhere.

EDIT: added some comments

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:11:26+00:00Added an answer on May 26, 2026 at 1:11 pm

    This seems to have been a bug. It works with GCC 4.6.1. Just want to close the question…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

okay, so I'm trying to get a directory of folders and sub folders, but
Okay, This one is pretty simmilar to my last one, but I don't get
Okay this is the css code I put on the master page so it
Okay, I'm trying to make a quick little class to work as a sort
Okay. I have this code on my site: <?php session_start(); include database.php; include bruger.php;
Okay, I'm thoroughly stumped on this one. I'm trying to build a menu of
Okay, I've looked all over the internet for a good solution to get PHP
Okay so im working on this php image upload system but for some reason
Okay, so this probably sounds terribly nefarious, but I need such capabilities for my
Okay. I know this looks like the typical Why didn't he just Google it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.