Code/C, C++ 4

scanf 대신 fread 사용 시 참고

scanf는 null terminate를 해 주고 그만큼의 추가 공간이 필요 fread는 null terminate를 해 주지 않음 -> 배열이나 할당된 공간의 초기화 또는 직접 null terminate 필요 scanf("%[^\n]", str)으로 \n 문자를 입력받지 않을 수 있음 fread는 \n까지 읽어옴 // 100 글자 입력 char str[102] = {}; // null terminate를 위해 +1 int len = fread(str, sizeof(*str), sizeof(str) - 2, stdin); str[--len] = 0; //\n 무시 for (int i = 0; i < len; i++) ...

Code/C, C++ 2022.03.26

malloc() 결과값의 캐스팅

stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc Do I cast the result of malloc? In this question, someone suggested in a comment that I should not cast the result of malloc, i.e. int *sieve = malloc(sizeof(int) * length); rather than: int *sieve = (int *) malloc(sizeof(int) * stackoverflow.com 1. 캐스팅은 하지 않아야 한다 malloc() 함수를 사용하면서 실수로 헤더를 include 하지 않았을 때, malloc()의 결과값을 명시적으로 캐..

Code/C, C++ 2020.11.27