[질문] atoi 내부

segenny의 이미지

리눅스 플랫폼에서
/usr/include/stdlib.h 안에 보면

extern __inline int
__NTH (atoi (__const char *__nptr))
{
  return (int) strtol (__nptr, (char **) NULL, 10);
}

atoi를 호출 했을때 내부적으로 strtol 함수를 호출하는 것이 맞는건가요?

전웅의 이미지

> 리눅스 플랫폼에서
> /usr/include/stdlib.h 안에 보면
>
> extern __inline int
> __NTH (atoi (__const char *__nptr))
> {
> return (int) strtol (__nptr, (char **) NULL, 10);
> }
>
> atoi를 호출 했을때 내부적으로 strtol 함수를 호출하는 것이 맞는건가요?
>

넵, 설사 inline 이 되지 않을 때에도 strtol() 로 구현됐던 것으로
기억하고 있습니다.

스팩 상 strtol() 이 atoi() 의 superset 이기에 atoi() 를 구현하는 가장
손쉬운 방법입니다.

--
Jun, Woong (woong at icu.ac.kr)
Web: http://www.woong.org (서버 공사중)

--
Jun, Woong (woong at gmail.com)
http://www.woong.org

전웅의 이미지

제 답변이 좀 성의가 없었나요?

좀 더 친절히 설명 드리면...

- 원칙적으로 atoi 의 행동이 strtol 을 사용해 정의되어 있긴 하지만,
이것이 atoi 구현시 반드시 strtol 을 사용해야 함을 의미하는 것은
아닙니다. 속도 등의 이유로 atoi 를 strtol 과 별도로 구현하는 경우도
적지 않습니다.

하지만, 기능상 strtol 은 atoi 의 superset 입니다. 따라서 아래와 같은
이유로 atoi 가 strtol 을 직접 이용하거나 혹은 strtol 을 위한 내부 함수
를 공유하는 경우가 적지 않습니다.

- atoi 와 strtol 의 큰 차이점 중 하나는 반환값의 type 입니다. overflow
가 발생하는 경우, strtol 의 경우에는 표준이 명시된 행동을 보여야
하지만, atoi 는 임의로 동작할 수 있기 때문에 (undefined behavior)
주어진 인자에 대해 strtol 을 수행한 후 그 결과를 단순히 int 로
변환해 반환해 줄 수 있습니다.

- C99 에서는 새로 추가된 정수형에 대해 strto* 가 추가되었습니다.
덕분에 중복 구현을 막기 위해 가장 큰 유부호/무부호 정수형에 대한
내부 함수 (예를 들면, _Strto*) 를 도입한 후 atoi 와 strto* 에서
필요에 맞춰 호출해 사용하는 방식이 유용합니다.

제가 표준 라이브러리를 구현했을 때에도 strtol 을 위한 내부 함수인
_Strtol 을 구현한 후 strtol 과 atoi 에서 호출해 사용하도록 만든 바
있습니다. 당시 라이브러리 구조의 적지 않은 부분을 glibc 에서 참고했기
때문에 (glibc 보다 성능은 떨어지지만 가독성과 이식성은 높습니다 ^^),
glibc 역시 atoi 구현시 strtol 을 사용했던 것으로 기억하고 있습니다.

결론적으로 기억에 따라 atoi 의 호출 형태에 따라 정리하면,

- atoi 를 위한 masking macro 제공시 strtol 혹은 strtol 을 위한 내부
함수 호출로 확장
- inlining 이 수행되는 경우 strtol 혹은 strtol 을 위한 내부 함수 호출
- atoi 의 실제 함수 호출시 내부에서 strtol 혹은 strtol 을 위한 내부
함수 호출

의 방식으로 구현됩니다.

atoi 와 strtol 의 경우 다행히도 동일한 표준 헤더에서 선언되기 때문에
atoi 가 맘놓고 strtol 을 사용할 수 있는 경우에 해당됩니다.

--
Jun, Woong (woong at icu.ac.kr)
Web: http://www.woong.org (서버 공사중)

--
Jun, Woong (woong at gmail.com)
http://www.woong.org

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.