C++ Part4

Why do C++ compilers need name mangling?

Name mangling is the rule according to which C++ changes function’s name into function signature before passing that function to a linker. This is how the linker differentiates between different functions with the same name.

Is there anything you can do in C++ that you cannot do in C?

No. There is nothing you can do in C++ that you cannot do in C. After all you can write a C++ compiler in C. 

 

 


This is my little collection of C++ Tipps/Tricks/little known facts.
Every time i discover something worth adding, i will put it here.

1. Discovering Memory Leaks
2. CPUID/RDTSC
3. Exact Time Measuring
4. Functionpointer to class memberfunctions
5. Setting Fullscreen mode under windows
6. Know thy stringstreams
7. std::sort with non-static compare-method in a class

1. Discovering Memory Leaks
Of course if you are working on a serious project you will use a sophisticated memory manager that will also discover memory leaks (along with their exact position in the code), but if you just want to do a quick test there is an easy way to let the debugger check for memory leaks:
– Include crtdbg.h in your project.
– At the beginning of you main() / WinMain () put this code:

int flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
flag |= _CRTDBG_LEAK_CHECK_DF;
_CrtSetDbgFlag(flag);

This works in VC++ ( i don’t know about other compilers tho ) and, if you run the programm in debug mode (Hit F5), it will report memory leaks in the debug output window.

2. CPUID/RDTSC
You have propably heard about the assembler instruction CPUID and RDTSC, which are supported by VC++ 6 and higher, but if you have an older compiler you can make use of the __emit function. This is a pseudo-asm instruction that lets you insert bytes directly into the outputted .exe. So, with a compiler that doesn’t know the cpuid/rdtsc instuctions we can just use a little macro to put their respective opcodes directly as binary values into our program:

#define rdtsc __asm __emit 0fh __asm __emit 031h
#define cpuid __asm __emit 0fh __asm __emit 0a2h

Advertisements