CHAPTER 23. POINTERS TO FUNCTIONS CHAPTER 23. POINTERS TO FUNCTIONS
Listing 23.2: MSVCR80.DLL
.text:7816CBF0 ; void cdecl qsort(void *, unsigned int, unsigned int, int (cdecl )(const ⤦
Çvoid , const void *))
.text:7816CBF0 public _qsort
.text:7816CBF0 _qsort proc near
.text:7816CBF0
.text:7816CBF0 lo = dword ptr -104h
.text:7816CBF0 hi = dword ptr -100h
.text:7816CBF0 var_FC = dword ptr -0FCh
.text:7816CBF0 stkptr = dword ptr -0F8h
.text:7816CBF0 lostk = dword ptr -0F4h
.text:7816CBF0 histk = dword ptr -7Ch
.text:7816CBF0 base = dword ptr 4
.text:7816CBF0 num = dword ptr 8
.text:7816CBF0 width = dword ptr 0Ch
.text:7816CBF0 comp = dword ptr 10h
.text:7816CBF0
.text:7816CBF0 sub esp, 100h
....
.text:7816CCE0 loc_7816CCE0: ; CODE XREF: _qsort+B1
.text:7816CCE0 shr eax, 1
.text:7816CCE2 imul eax, ebp
.text:7816CCE5 add eax, ebx
.text:7816CCE7 mov edi, eax
.text:7816CCE9 push edi
.text:7816CCEA push ebx
.text:7816CCEB call [esp+118h+comp]
.text:7816CCF2 add esp, 8
.text:7816CCF5 test eax, eax
.text:7816CCF7 jle short loc_7816CD04
comp— is the fourth function argument. Here the control gets passed to the address in thecompargument. Before it, two
arguments are prepared forcomp(). Its result is checked after its execution.
That’s why it is dangerous to use pointers to functions. First of all, if you callqsort()with an incorrect function pointer,
qsort()may pass control flow to an incorrect point, the process may crash and this bug will be hard to find.
The second reason is that the callback function types must comply strictly, calling the wrong function with wrong arguments
of wrong types may lead to serious problems, however, the crashing of the process is not a problem here —the problem is how
to determine the reason for the crash —because the compiler may be silent about the potential problems while compiling.