NULL passed directly to a function expecting a const reference parameter (VC++ 4.2)

Dennis picture Dennis · Sep 7, 2011 · Viewed 9.6k times · Source

I am looking at something that I discovered in an old code base, and I am pretty confused.

Here is a function definition:

void vUpdateSequenceDetailsAndIncrement(
            const CallEvent& roCPEvent,
            const CallInfo& roCallInfo,
            BOOL bCreationEvent);

Here it is being called:

vUpdateSequenceDetailsAndIncrement(roCPEvent, NULL, FALSE);

Here NULL is being passed directly to the reference parameter roCallInfo. This function eventually calls:

vTimeChange(*pSeqDetails, roCPEvent, roCallInfo);

which is defined:

void vTimeChange(const SequenceDetails& roSequenceDetails,
        const CallEvent& roCPEvent,
        const CallInfo& roCallInfo)

Again passing the possibly NULL value to roCallInfo. I thought that NULL could not be passed as a reference? Does anyone know if VC++ 4.x had some kind of problem which made this kind of code okay? If NULL can be passed as a reference then what happens when in vTimeChange something like this happens:

roCallInfo.getCallStartTime(); 

Is that not a dereference of NULL in the same way as if I were to do

CallInfo * info = NULL;
info->getCallStartTime();

? I'll probably put a guard in there anyways and let the compiler remove it if unnecessary, but I'd love to get to the bottom of how this is happening!

Thanks.

Answer

c-smile picture c-smile · Sep 7, 2011

Depends on how NULL is defined in VC 4.2

If it is just

#define NULL 0 

then you are actually getting this under the hood:

vUpdateSequenceDetailsAndIncrement(roCPEvent, CallInfo(0), FALSE);

and reference of temp var of type CallInfo is passed to the function (if CallInfo has compatible ctor)