'carsh'에 해당되는 글 1건

  1. 2022.04.18 :: [C/C++] strtok, strtok_s crash
MINERVA/C_CPP 2022. 4. 18. 17:31
반응형

아주 오래전에 이와 유사한 이슈가 있었는데, 깜빡하고 똑같은 실수를 했음

개인적으로는 msdn에 해당 내용이 좀더 친절하게 되어있으면 좋겠다.

 

https://social.msdn.microsoft.com/Forums/vstudio/en-US/5da5dbcf-05e7-49a3-bf0d-d91dcb0f358a/strtok-dilemma-and-working-problem?forum=vcgeneral 

 

Strtok Dilemma and Working Problem

The difference is about whether the memory containing the string value is writable or read-only.  In the first case, an array of char is being initialized with the content of the string, and because the array is on the stack it is writeable. In the second

social.msdn.microsoft.com

https://stackoverflow.com/questions/40034176/strtok-s-crashes-program-when-string-is-of-char

 

strtok_s crashes program when string is of char *

I am trying to get a token from a string that is delimited by space(" "). But the following code crashes the application when the string is of char * type. #include <stdio.h> #include <st...

stackoverflow.com

해당 이유는 아래와 같이 정리가 되어있음.

" strtok() changes the memory pointed to by the argument str, so what it points at must be writeable - i.e. a char * and not a const char *.  Since in the second case this condition is not satisfied, when it attempts to write a NUL to the read-only memory it crashes."

 

간단하게 정리하면, strtok_s(strtok)함수의 첫번째 인자에 상수포인터(pointer to const)가 들어가면 않됨

즉, 포인터가 가르키는 address가 read-only이면 않됨

[참조]

https://choiwonwoo.tistory.com/entry/cc-const-%ED%82%A4%EC%9B%8C%EB%93%9C-%EC%9D%B4%ED%95%B4

 

[c/c++] const 키워드 이해

const 키워드는 개발중에는 자연스럽게(?)사용하다가, 질문을 받고 설명하려고 하면 은근히 까다롭게 느껴져정리를 하고자 합니다. 개발자마다 설명하는 방법이 여러가지 있을 수 있지만, 저의

choiwonwoo.tistory.com

 

 

 

반응형
posted by choiwonwoo
: