I was using CRTDBG library to identify memory leaks in visual studio 2019. I need to define "#define _CRTDBG_MAP_ALLOC" macro for getting the line number of the leak occurs. I am unclear that how the macro gets replaced when there is no value to be replaced ??
#define _CRTDBG_MAP_ALLOC
#include <iostream>
#include <crtdbg.h>
using namespace std;
int main()
{
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
int *ptr = (int*)malloc(sizeof(int));
*ptr = 10;
//free(ptr);
return 0;
}
I am a new to c/c , Kindly help me on this.
CodePudding user response:
#define _CRTDBG_MAP_ALLOC
just tells the preprocessor that _CRTDBG_MAP_ALLOC
is defined.
Code can later check if its defined with a #if defined(_CRTDBG_MAP_ALLOC)
.
Note nowhere do we set or care or check what value its defined to. So doing something like if (_CRTDBG_MAP_ALLOC == 2)
doesn't make sense.
But we dont need it to. It just has to exist.
CodePudding user response:
It's simply replaced by the empty string. E.g.
#define FOO 1
std::cout << FOO 1; // expands to 1 1, outputs 2
#undef FOO
#define FOO
std::cout << FOO 1; // expands to 1, outputs 1
But typically these things are checked with #ifdef
.