Tricky C problem
Julie Stamp (8365) 474 posts |
This is infuriating. When I run the below (compiled with Norcroft RISC OS ARM C vsn 5.83 [01 Jun 2020]) I get
where I expect the two addresses to be different. When I look at the disassembly, it seems to be calculating the address of What’s going on here?? Edit: If I compile with
|
David Pitt (3386) 1248 posts |
I tried our luck with DDE30 but … *cc -v Norcroft RISC OS ARM C vsn 5.85 [22 Oct 2020] cc -c -depend !Depend -throwback -ff -fah -o o.Julie c.Julie Norcroft RISC OS ARM C vsn 5.85 [22 Oct 2020] "c.Julie", line 25: Warning: extern 'f' not declared in header "c.Julie", line 32: Warning: extern 'g' not declared in header "c.Julie", line 38: Warning: extern 'done' not declared in header "c.Julie", line 47: Warning: actual type <pointer> mismatches format '%x' "c.Julie", line 53: Warning: actual type <pointer> mismatches format '%x' "c.Julie", line 57: Warning: variable 'argv' declared but not used "c.Julie", line 57: Warning: variable 'argc' declared but not used c.Julie: 7 warnings, 0 errors, 0 serious errors link -o Julie o.Julie CLIB:o.stubs squeeze Julie Julie: application built *Julie 8d20 7 8d20 14 … and then with gcc which looks more hopeful … *gcc julie.c *a/out 8a7c 7 8a98 14 * |
Jean-Michel BRUCK (3009) 359 posts |
This is a problem in C, your call to the function f (s) does not modify s.
and call f2 (& s); instead of s = f (s); in the while loop. I get with DDE 30: 8d78 7 8d94 14 Note : in the struct s_t, it’s better to write void * a; instead of char * a; => in function g() s_t s = {(char*)-1,&t1}; becomes: s_t s = {NULL,&t1};more readable Sorry for my English |
Stuart Swales (1481) 351 posts |
No, it is a compiler bug. All members of s must be treated as having been modified by function f as that structure is its return value. Jean-Michel: NULL != -1 |
Paolo Fabio Zaino (28) 1882 posts |
I agree with Jean-Michel, the function f should be defined as: void f(s_t *s) { s->a = NULL; s->t = &t2; } and in main obviously replace s = f(s) with: f(&s); This is how you would do what you want in C. |
Jean-Michel BRUCK (3009) 359 posts |
Thanks. NULL != -1 |
Paolo Fabio Zaino (28) 1882 posts |
just a note, GCC would compile Julie’s code and it would work as she expects, but I am not 100% sure if this is actually a correct behaviour or not, hence I do not want to state there is a bug in DDE !CC… The correct C syntax for what she wants to do is what Jean-Michael described. The NULL vs Bool is also correct, Bool should be either -1 or 0. If you want to be pedantic and stick to C99 standard then you should use stdbool.h provided values, which are 0 for false and 1 for true. |
Julie Stamp (8365) 474 posts |
Thanks guys, I’ve filed a ticket. It seems to come down to returning a struct from a function, and not having the pointer members of the structs at the start of the structs. |