보통 디버깅은 gdb나 개발툴(IDE, SDT 등)에서 제공하는 디버거를 사용하게 된다. 하지만 때로는 단순히 출력문을 이용해서 디버깅을 진행하기도 한다. (간단한 확인 또는 디버깅 환경이 없을 경우)
단순히 printf("here!!\r\n"); 요렇게 쓰거나 아니면 printf("who are you -> %d\r\n", trouble_make); 요렇게 쓸 수도 있다.
하지만 약간 아쉽다. 명색이 디버깅 출력문인데, 너무 정보가 없는 것 같다.
좀 더 많은 정보를 포함하면서, 쓰기 쉬운 assert 함수를 만들어보자.
void _my_assert(bool condition, // 조건
char const* message, // 출력문
char const* func, // 함수명
char const* file, // 파일명
int line) // 라인
{
if (!condition)
{
fprintf(stderr, "Assert: %s, in %s (%s:%i)\r\n", message, func, file, line);
exit(0);
}
}
#define my_assert(condition, message) \
_my_assert(condition, message, __func__, __FILE__, __LINE__)
int main()
{
int *a = calloc(1, sizeof(int));
my_assert(a, "why u are NULL...");
return 0;
}
음.. 위에 코드가 좀 깨지는구나. 코드 에디트 창에서는 깔끔하게 썼는데 ㅋㅋ
이런 코드 잘 만들어서 활용하면 유용할 듯 하다.
'C' 카테고리의 다른 글
길이가 0인 배열 (0) | 2025.04.15 |
---|---|
Object file.. (0) | 2025.01.14 |
세그먼트 (0) | 2023.04.13 |
Why is the number of array elements smaller than minus value? (0) | 2023.04.06 |
array length as parameter (0) | 2022.11.09 |