C\C++ debugging skills

From: http://www.open-open.com/lib/view/open1347778870974.html

When debugging code, sometimes there is no problem with the function that reports the error, but the parameter passed in by the function that calls it is abnormal, and it is impossible to accurately locate the specific calling position through the code, especially for For some basic functions, this often happens because there are too many places in the project to call them, and sometimes the return value detection is missed. Here is a quick way to find out who the caller is.

//filename: tt.h
#include <stdio.h>

int foo(int p);

#define foo(_a) \
    do { \
        printf("[%s:%d]call foo()\n", __FUNCTION__, __LINE__); \
        foo(_a); \
    } while (0)

//filename: tt.c
#include "tt.h"

#ifdef foo
#undef foo
#endif

int foo(int p)
{
    printf("input = %d\n", p);
    return p;
}

//filename: main.c
#include "tt.h"

int main()
{
    foo(1024);
    return 0;
}

When the #define foo(_a) macro is not defined , the execution result is as follows (comment out the code that defines foo in tt.h):

>> input = 1024
After defining the #define foo(_a) macro, the execution result is as follows:

>> [main:5]call foo()
>> input = 1024
By defining a macro that is consistent with the function, the modified code is executed when called, so that we can add some information we need to it to facilitate our debugging.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324361018&siteId=291194637