Ticket #496 (Fixed)Tue Nov 03 10:22:28 UTC 2020
cc 5.83: Assignment of struct return value incorrectly optimised away
Reported by: | Julie Stamp (8365) | Severity: | Normal |
Part: | RISC OS: C/C++ toolchain | Release: | |
Milestone: | Status | Fixed |
Details by Julie Stamp (8365):
If I run the below code I get
*cc c.test
Norcroft RISC OS ARM C vsn 5.83 [01 Jun 2020]
*test
00008ce0 7
00008ce0 14
whereas I would expect to get different addresses, which I do with debug:
*cc c.test -g
Norcroft RISC OS ARM C vsn 5.83 [01 Jun 2020]
*test
00008d00 7
00008d0c 14
#include <stdio.h> #include <stdbool.h>
typedef struct { int x; /* If you remove this, the problem goes away */ char y[1]; int z; /* Just to show it's not y being at the end causing it */ } t_t;
typedef struct { int a; /* This must come first */ t_t *t; } s_t;
static t_t t1 = {0,{7},0}; static t_t t2 = {0,{14},0};
s_t f(void) { s_t s = { 0, &t2 }; return s; }
int main(int argc, char **argv) { s_t s = { 0, &t1 }; printf("%p %d\n", s.t->y, s.t->y[0]); s = f(); printf("%p %d\n", s.t->y, s.t->y[0]); return 0; }
Changelog:
Modified by Julie Stamp (8365) Thu, December 03 2020 - 15:55:37 GMT
- Summary changed from Misoptimisation to cc 5.83: Assignment of struct return value incorrectly optimised away
Modified by Julie Stamp (8365) Thu, December 03 2020 - 16:16:48 GMT
- Attachment added: example
Attached is another example of this sort of thing, this time using div_t.
Modified by Julie Stamp (8365) Thu, December 03 2020 - 16:18:31 GMT
For output I get
a = 43795, b = 1
rather than the expected
a = 2, b = 1
Modified by Sprow (202) Sat, February 27 2021 - 08:13:14 GMT
- Status changed from Open to Fixed
From the cc 5.86 change log:
- An assignment to a structure from a structure returning function could
ignore the subsequent result if there was an earlier reference to the same
structure. This problem has been fixed & the result is re-read after the
function call.
And the example above prints 2 & 1.