-
[c++] c++ cstype string아무거나적어~ 2023. 1. 10. 23:11
strcpy 는 destination배열의 크기를 고려하지 않으므로 허용되지 않은 메모리를 덮어쓸 수 있음. 쓰지 마셈
strncpy 가 그 대안인데 주의해서 사용할 것. 일어날 수 있는 문제점
- source 크기보다 작은 개수만큼 복사할 때 destination에 널문자 복사 안됨 -> 끝에 널문자 추가해주기
- destination의 크기가 복사하는 개수보다 작을 때(정확하게는 복사개수 이하일 때) -> 널문자 넣을 공간 없어서 제대로 작동 안함 + 허용되지 않은 메모리 덮어씌우게 됨
결론
destination 의 크기는 다음과 같이 잡는다
복사 문자의 개수 + 1≤ destination의 크기
(1은 널문자 넣기위한 공간)
널문자는 우리가 추가 해주자'아무거나적어~' 카테고리의 다른 글
[visual studio] setting (0) 2023.01.11 파이썬이 콘솔 default setting (0) 2023.01.11 [c++] The pointer that points to the whole array (0) 2023.01.10 next_permutation implementation (0) 2023.01.08 pycharm file size 뭐시기 (0) 2023.01.07