What is the meaning of `static char THIS_FILE [] = __FILE __;`?

What is the point of static char THIS_FILE[] = __FILE__; ?

Introduction: what is he doing? Where is he from?

MFC, Microsoft's native Windows class library, has the DEBUG_NEW macro, which tracks memory allocations and where they were (in user code).

For this to work, the VS wizard puts the following code in each cpp file: (not in the header files)

 #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif 

and the new debug macro is defined as (in afx.h ):

 #define DEBUG_NEW new(THIS_FILE, __LINE__) 

All equipment will lead to a meaningful conclusion of the leak, for example:

 Detected memory leaks! Dumping objects -> f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(141) : {615} normal block at 0x04081CE0, 56 bytes long. Data: <¬9Í] > AC 39 CD 5D 13 00 00 00 13 00 00 00 01 00 00 00 c:\my\dev\path\myfile.cpp(237) : {614} normal block at 0x04087FC0, 4 bytes long. Data: <ð > F0 1C 08 04 Object dump complete. 

So what's the question again?

What puzzles me is the THIS_FILE char array. Technique does not make sense. If they defined DEBUG_NEW in the same way as:

 #define DEBUG_NEW new(__FILE__, __LINE__) 

They could just put it in the header and do with it instead of having this ifdef block in every file.

So what is the point of THIS_FILE ?

(By the way, this is exactly what MS CRT does with malloc and _malloc_dbg , where the debug macro is defined in the crtdbg.h header as follows:

 #define malloc(s) _malloc_dbg(s, _NORMAL_BLOCK, __FILE__, __LINE__) 

)

So why is it a tricky way in the MFC DEBUG_NEW macro when the easy way will work (better) ???


Update: Ha! I recently noticed that the VS2005 wizard does not put the THIS_FILE definition in the generated cpp file.

Examining this, it seems that MS decided some time ago that it was not necessary anymore, since afxtempl.h defines the following:

 #undef THIS_FILE #define THIS_FILE __FILE__ 

However, I think the question remains the same, why it was always necessary. (And I guess the answer with a memory request back then is pretty right.)


+8
memory-leaks visual-c ++ mfc visual-studio-debugging
source share
3 answers

The debug dispenser stores a pointer to the file name in the heap block. Only 4 bytes instead of each allocated block also allocating space for the file name.

Note that this can lead to loss of debugging information when a block leak was allocated by the DLL and that the DLL was unloaded by the time the leak report was generated.

Only the THIS_FILE character THIS_FILE guaranteed to be always the same in the translation block. Not __FILE__ , this is literal. And two literals are not guaranteed to have the same address, even if they have the same value, which means that the compiler can create a separate literal - and use memory - for each use of __FILE__ .

+8
source share

I believe, and I could be mistaken, that this delimits memory blocks inside a compiled exe. This would help if you got an exception, you could follow the memory error chain to find the violation code, given which address was loaded in the dll / exe.

0
source share

__FILE__ same in the whole file, and __LINE__ in every line. Therefore, if you have a lot of code for debugging in a file, old compilers that did not merge constants would have many unnecessary copies of the string "filename.ext". You can check the difference with something like -fno-merge-constants (sorry, this is for gcc CFLAG for it, not for VS). Most compilers now combine default constants, making this definition unnecessary.

0
source share

All Articles