digitalmars.D - Weird codegen bug
- RazvanN (18/18) Jun 13 2019 import std.stdio : writeln;
- Daniel Kozak (6/24) Jun 13 2019 ldc will print
- Dennis (8/10) Jun 13 2019 What happens is that b lives in a register.
- Radu (3/21) Jun 13 2019 Check out gotbolt output:
- Patrick Schluter (3/21) Jun 13 2019 It's definitely not a codegen bug. It's a programmers assumption
- John Colvin (3/21) Jun 13 2019 Why would you think this should work? Locals are not the same as
- RazvanN (3/31) Jun 13 2019 I think it's odd that b changes its value for no apparent reason.
- John Colvin (2/34) Jun 13 2019 Undefined behaviour is often surprising.
- Walter Bright (5/31) Jun 14 2019 It's undefined behavior to access memory beyond the boundaries of the me...
import std.stdio : writeln; void main() { int a = 7; int b = 9; /* some code later */ *(&a + 1) = 2; writeln(b); writeln(b); } This code prints: 9 2 with latest version of dmd. Is this a bug or am I missing something? For the record, gdc prints (as expected): 2 2
Jun 13 2019
ldc will print 9 9 and sigsegv :D On Thu, Jun 13, 2019 at 1:30 PM RazvanN via Digitalmars-d < digitalmars-d puremagic.com> wrote:import std.stdio : writeln; void main() { int a = 7; int b = 9; /* some code later */ *(&a + 1) = 2; writeln(b); writeln(b); } This code prints: 9 2 with latest version of dmd. Is this a bug or am I missing something? For the record, gdc prints (as expected): 2 2
Jun 13 2019
On Thursday, 13 June 2019 at 11:28:25 UTC, RazvanN wrote:Is this a bug or am I missing something? For the record, gdc prints (as expected):What happens is that b lives in a register. After the first call, the register is invalidated and it has to be reloaded with the value on the stack (modified by your pointer). I'm pretty sure *(&a + 1) = 2; is undefined behavior, so both dmd and gdc are correct. (Assembly can be seen here: https://run.dlang.io/is/bjxVXg)
Jun 13 2019
On Thursday, 13 June 2019 at 11:28:25 UTC, RazvanN wrote:import std.stdio : writeln; void main() { int a = 7; int b = 9; /* some code later */ *(&a + 1) = 2; writeln(b); writeln(b); } This code prints: 9 2 with latest version of dmd. Is this a bug or am I missing something? For the record, gdc prints (as expected): 2 2Check out gotbolt output: https://godbolt.org/z/sxo_8l
Jun 13 2019
On Thursday, 13 June 2019 at 11:28:25 UTC, RazvanN wrote:import std.stdio : writeln; void main() { int a = 7; int b = 9; /* some code later */ *(&a + 1) = 2; writeln(b); writeln(b); } This code prints: 9 2 with latest version of dmd. Is this a bug or am I missing something? For the record, gdc prints (as expected): 2 2It's definitely not a codegen bug. It's a programmers assumption bug :-)
Jun 13 2019
On Thursday, 13 June 2019 at 11:28:25 UTC, RazvanN wrote:import std.stdio : writeln; void main() { int a = 7; int b = 9; /* some code later */ *(&a + 1) = 2; writeln(b); writeln(b); } This code prints: 9 2 with latest version of dmd. Is this a bug or am I missing something? For the record, gdc prints (as expected): 2 2Why would you think this should work? Locals are not the same as struct members, you can't just walk a pointer around.
Jun 13 2019
On Thursday, 13 June 2019 at 13:45:04 UTC, John Colvin wrote:On Thursday, 13 June 2019 at 11:28:25 UTC, RazvanN wrote:I think it's odd that b changes its value for no apparent reason. I understand why it happens, still it is surprising.import std.stdio : writeln; void main() { int a = 7; int b = 9; /* some code later */ *(&a + 1) = 2; writeln(b); writeln(b); } This code prints: 9 2 with latest version of dmd. Is this a bug or am I missing something? For the record, gdc prints (as expected): 2 2Why would you think this should work? Locals are not the same as struct members, you can't just walk a pointer around.
Jun 13 2019
On Thursday, 13 June 2019 at 15:42:38 UTC, RazvanN wrote:On Thursday, 13 June 2019 at 13:45:04 UTC, John Colvin wrote:Undefined behaviour is often surprising.On Thursday, 13 June 2019 at 11:28:25 UTC, RazvanN wrote:I think it's odd that b changes its value for no apparent reason. I understand why it happens, still it is surprising.import std.stdio : writeln; void main() { int a = 7; int b = 9; /* some code later */ *(&a + 1) = 2; writeln(b); writeln(b); } This code prints: 9 2 with latest version of dmd. Is this a bug or am I missing something? For the record, gdc prints (as expected): 2 2Why would you think this should work? Locals are not the same as struct members, you can't just walk a pointer around.
Jun 13 2019
On 6/13/2019 4:28 AM, RazvanN wrote:import std.stdio : writeln; void main() { int a = 7; int b = 9; /* some code later */ *(&a + 1) = 2; writeln(b); writeln(b); } This code prints: 9 2 with latest version of dmd. Is this a bug or am I missing something? For the record, gdc prints (as expected): 2 2It's undefined behavior to access memory beyond the boundaries of the memory object pointed to: https://github.com/dlang/dlang.org/pull/2649/files#diff-2bcc1b9af1e2da4f63bfda4b26f114a2R90 in an update to the spec that nobody sees fit to pull :-(
Jun 14 2019