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