public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/102022] New: incorrect code with -O2
@ 2021-08-23 10:27 scu319hy at msn dot com
  2021-08-23 10:30 ` [Bug c++/102022] " marxin at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: scu319hy at msn dot com @ 2021-08-23 10:27 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102022

            Bug ID: 102022
           Summary: incorrect code with -O2
           Product: gcc
           Version: 10.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: scu319hy at msn dot com
  Target Milestone: ---

following code doesn't works well with -O2 flag.

#include <stdio.h>
#include <memory.h>
#include <stdlib.h>

struct _node_t {
        _node_t * _next;
};
static void _rebase_ptr(char *&ptr, void *old_base, void *new_base) {
        if ( ptr != 0 ) {
                ptr = (char *)((char *)(ptr) - (char *)(old_base) + (char
*)(new_base));
        }
}

static void _rebase_ptr( _node_t *&head, void *old_base, void *new_base ) {
        _rebase_ptr(*(char**)&head, old_base, new_base);
        // error!! value of "head" not changed

        _node_t * node = head;
        while( node != 0 ) {
                _rebase_ptr( *(char**)&(node->_next), old_base, new_base );
                node = node->_next;
        }
}

int main() {
        _node_t *tmp = (_node_t *)calloc(1, sizeof(_node_t));
        _node_t *tmp2 = (_node_t *)calloc(1, sizeof(_node_t));
        memcpy(tmp2, tmp, sizeof(_node_t));
        free(tmp);

        _rebase_ptr(tmp, tmp, tmp2);
        printf("%llx == %llx\n", (long long)tmp, (long long)tmp2);
        free(tmp);
        return 0;
}

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-08-23 13:24 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-23 10:27 [Bug c++/102022] New: incorrect code with -O2 scu319hy at msn dot com
2021-08-23 10:30 ` [Bug c++/102022] " marxin at gcc dot gnu.org
2021-08-23 10:33 ` pinskia at gcc dot gnu.org
2021-08-23 10:36 ` pinskia at gcc dot gnu.org
2021-08-23 13:22 ` rguenth at gcc dot gnu.org
2021-08-23 13:24 ` jakub at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).