public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "scu319hy at msn dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/102022] New: incorrect code with -O2
Date: Mon, 23 Aug 2021 10:27:14 +0000	[thread overview]
Message-ID: <bug-102022-4@http.gcc.gnu.org/bugzilla/> (raw)

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

             reply	other threads:[~2021-08-23 10:27 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-23 10:27 scu319hy at msn dot com [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-102022-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).