Open andrewrk opened 5 years ago
Why even cast integer to c-pointer exists? If its used to create null pointers why not use null
?
null
would work once #1967 is implemented. Auto-translated C code will tests such as ptr == 0
which should continue to work. So comptime known 0 would still be an exception even if this proposal is accepted.
@andrewrk Is it possible to auto-translate C ptr != 0
ptr != NULL
ptr != (void*)0
as ptr != null
?
@Rocknest yes, and that change is now in master branch: https://github.com/ziglang/zig/commit/e3542196c0b0121fb52d9af599b549cf490e47a8
IRC user gamester pointed out this example:
I created an equivalent C code example:
This actually does compile in C (and has the same runtime behavior), but with clang it gives a warning:
Given that this warning is on by default, I think it would be OK to make this into a compile error. So - definitely an integer other than
usize
should have an error. Potentially even implicit cast to/fromusize
could be a problem if the address wasn't comptime known to be zero.