質問するログイン新規登録

回答編集履歴

3

update

2020/03/12 09:24

投稿

yohhoy
yohhoy

スコア6196

answer CHANGED
@@ -1,4 +1,4 @@
1
- GNU Cライブラリ(glibc)の [`atoi`関数実装](https://github.com/bminor/glibc/blob/master/stdlib/atoi.c) や [Newlibライブラリ](https://ja.wikipedia.org/wiki/Newlib) の [`atoi`関数実装](https://github.com/bminor/newlib/blob/master/newlib/libc/stdlib/atoi.c) では、単純に`strtol`関数に委譲しているだけですね。
1
+ GNU Cライブラリ(glibc)の [`atoi`関数実装](https://github.com/bminor/glibc/blob/master/stdlib/atoi.c) や [BSD libcライブラリ](https://ja.wikipedia.org/wiki/BSD_libc) の [`atoi`関数実装](https://github.com/freebsd/freebsd/blob/master/lib/libc/stdlib/atoi.c)、[Newlibライブラリ](https://ja.wikipedia.org/wiki/Newlib) の [`atoi`関数実装](https://github.com/bminor/newlib/blob/master/newlib/libc/stdlib/atoi.c) では、単純に`strtol`関数に委譲しているだけですね。
2
2
 
3
3
  [muslライブラリ](https://ja.wikipedia.org/wiki/Musl) の [`atoi`関数実装](https://github.com/bminor/musl/blob/master/src/stdlib/atoi.c) を引用します:
4
4
  ```c

2

refinement

2020/03/12 09:24

投稿

yohhoy
yohhoy

スコア6196

answer CHANGED
@@ -1,4 +1,4 @@
1
- GNU Cライブラリ(glibc)の [`atoi`関数実装](https://github.com/bminor/glibc/blob/master/stdlib/atoi.c) や [Newlib](https://ja.wikipedia.org/wiki/Newlib) の [`atoi`関数実装](https://github.com/bminor/newlib/blob/master/newlib/libc/stdlib/atoi.c) では、単純に`strtol`関数に委譲しているだけですね。
1
+ GNU Cライブラリ(glibc)の [`atoi`関数実装](https://github.com/bminor/glibc/blob/master/stdlib/atoi.c) や [Newlibライブラリ](https://ja.wikipedia.org/wiki/Newlib) の [`atoi`関数実装](https://github.com/bminor/newlib/blob/master/newlib/libc/stdlib/atoi.c) では、単純に`strtol`関数に委譲しているだけですね。
2
2
 
3
3
  [muslライブラリ](https://ja.wikipedia.org/wiki/Musl) の [`atoi`関数実装](https://github.com/bminor/musl/blob/master/src/stdlib/atoi.c) を引用します:
4
4
  ```c

1

update

2020/03/12 09:04

投稿

yohhoy
yohhoy

スコア6196

answer CHANGED
@@ -1,4 +1,5 @@
1
1
  GNU Cライブラリ(glibc)の [`atoi`関数実装](https://github.com/bminor/glibc/blob/master/stdlib/atoi.c) や [Newlib](https://ja.wikipedia.org/wiki/Newlib) の [`atoi`関数実装](https://github.com/bminor/newlib/blob/master/newlib/libc/stdlib/atoi.c) では、単純に`strtol`関数に委譲しているだけですね。
2
+
2
3
  [muslライブラリ](https://ja.wikipedia.org/wiki/Musl) の [`atoi`関数実装](https://github.com/bminor/musl/blob/master/src/stdlib/atoi.c) を引用します:
3
4
  ```c
4
5
  int atoi(const char *s)
@@ -14,4 +15,9 @@
14
15
  n = 10*n - (*s++ - '0');
15
16
  return neg ? n : -n;
16
17
  }
17
- ```
18
+ ```
19
+
20
+ > 下記のようなコードになったのですが、下記のコードには弱点があります。
21
+ > intのMINである-2147483648を変換するときに、一時的に変数resの中に、2147483648が代入されてしまいます。
22
+
23
+ muslライブラリでは、累積計算を負数で行うことで範囲`[0, -2147483648]`を安全に表現しておき、最後に符号反転するというトリッキーな実装になっているようです。