strtol()
과 strtod()
는 효과적으로 문자열에서 constness를 버리는 것을 허용하고 강제하는 것으로 보입니다 :strtol, strtod가 안전하지 않습니까?
#include <stdlib.h>
#include <stdio.h>
int main() {
const char *foo = "Hello, world!";
char *bar;
strtol(foo, &bar, 10); // or strtod(foo, &bar);
printf("%d\n", foo == bar); // prints "1"! they're equal
*bar = 'X'; // segmentation fault
return 0;
}
위와 같이, 나는 어떤 캐스트도 직접 수행하지 않았습니다. 그러나 strtol()
은 기본적으로 내 const char *
을 char *
로 캐스팅합니다. 실제로 bar
을 const char *
로 입력 할 수 없기 때문에 안전하지 않은 유형의 변경이 필요합니다. 그렇게 위험하지 않습니까?
하지만 C++은 함수 오버로딩을 막지 않습니다. long int strtol (char * nptr, char ** endptr, int base);'* and *'long int strtol (const char * nptr, const char ** endptr, int base);': 이것은 컴파일 에러를 수정합니다. 사실, 표준은'strchr'과'strstr' 같은 다른 함수들에 대해서 이것을 수행합니다. – Thanatos
C FAQ 웹 사이트 ['const char * p'와'char const * p'와 'char * const p'?] (http://c-faq.com/ansi/constptrconst.html), 특히''char ** '를''char **'를 기대하는 함수에 전달할 수없는 이유는 무엇입니까? const char **'?] (http://c-faq.com/ansi/constmismatch.html) 대신에 C++ FAQ를 사용하여 설명을 이해하기 쉽다고 전적으로 확신하지는 않습니다. –