intのビット幅 検索
> the minimum size for … int is 16 bits,
> The type int should be the integer type that the target processor is most efficiently working with.
フムフム
C data types
https://en.wikipedia.org/wiki/C_data_types
> The int, uint, and uintptr types are usually 32 bits wide on 32-bit systems and 64 bits wide on 64-bit systems.
fmfm
A Tour of Go
https://go.dev/tour/basics/11
待って。isizeとusizeだ。
https://doc.rust-lang.org/book/ch03-02-data-types.html
$ cat src/main.rs
fn main() {
let mut x: usize = 0;
x -= 1;
println!("x is now {}", x);
}
$ cargo run --release
Compiling integer v0.1.0 (/home/zunda/c/src/local/rust_projects/integer)
Finished release [optimized] target(s) in 0.31s
Running `target/release/integer`
x is now 18446744073709551615
$ ruby -e 'puts Math.log(18446744073709551615.0)/Math.log(2)'
64.0
$ uname -srm
Linux 5.15.0-102-generic x86_64
Rustさんは開発版だと境界チェックしてくれるよね
$ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.04s
Running `target/debug/integer`
thread 'main' panicked at src/main.rs:3:5:
attempt to subtract with overflow
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Goさんは境界チェックなし
$ cat main.go
package main
import "fmt"
func main() {
var x uint = 0
x -= 1
fmt.Printf("x is %v\n", x)
}
$ go run main.go
x is 18446744073709551615
Cは
$ cat main.c
#include <stdio.h>
void main(void) {
uint x = 0;
x--;
printf("x is now %d\n", x);
}
$ gcc main.c && ./a.out
main.c: In function ‘main’:
main.c:4:3: error: unknown type name ‘uint’; did you mean ‘int’?
4 | uint x = 0;
| ^~~~
| int
まちがいたw
こうね(書式指定もまちがいてた)
$ cat main.c
#include <stdio.h>
void main(void) {
unsigned int x = 0;
x--;
printf("x is now %ud\n", x);
}
$ gcc main.c && ./a.out
x is now 4294967295d
その d はw
そういえばgccさんはオプション付けてコンパイル時の境界チェックしてくれた気のする