Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pointer arithmetic doesn't work #51

Closed
ghost opened this issue Apr 27, 2017 · 6 comments
Closed

Pointer arithmetic doesn't work #51

ghost opened this issue Apr 27, 2017 · 6 comments

Comments

@ghost
Copy link

ghost commented Apr 27, 2017

Repro code:

#import "fmt.odin";

main :: proc() {
  buffer := make([]u32, 1);
  buffer[0] = 23;
  fmt.printf("initial buffer: %v\n", buffer);

  p: rawptr = ^buffer[0];

  p_u32 := cast(^u32)p;
  p_u32^ = 42;

  fmt.printf("changed buffer: %v\n", buffer);

  fmt.printf("p_u32 (should be 42): %d\n", p_u32^);
  p_u32 += 1;
  p_u32 -= 1;
  fmt.printf("p_u32 (should be 42): %d\n", p_u32^);
}

Prints:

initial buffer: [23]
changed buffer: [42]
p_u32 (should be 42): 42
p_u32 (should be 42): 0
@ghost
Copy link
Author

ghost commented Apr 28, 2017

Minus operator increments the address

@ghost
Copy link
Author

ghost commented Apr 28, 2017

I guess because Token_Add and Token_Sub share the same code: f320958#diff-5f7101e83ab40f9f58bba057da941225R1488

@ghost
Copy link
Author

ghost commented Apr 28, 2017

https://github.com/gingerBill/Odin/blob/b78e970/src/ssa.c#L1551

GB_PANIC("TODO(bill): Ptr arith");

Not sure if related!

@ghost
Copy link
Author

ghost commented Apr 28, 2017

Manual pointer arithmetic workaround:

  p_u32 += 1;
  p_u32 = cast(^u32)cast(rawptr)(cast(uint)cast(rawptr)p_u32 - 1 * size_of(u32));

Prints:

initial buffer: [23]
changed buffer: [42]
p_u32 (should be 42): 42
p_u32 (should be 42): 42

gingerBill added a commit that referenced this issue Apr 28, 2017
@gingerBill
Copy link
Member

Fixed!

@ghost
Copy link
Author

ghost commented Apr 28, 2017

@gingerBill Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant