NULL is passed directly to a function that expects a const reference parameter (VC ++ 4.2)

I look at what I found in the old code base, and I'm pretty confused.

Here is the function definition:

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

Here it is called:

 vUpdateSequenceDetailsAndIncrement(roCPEvent, NULL, FALSE); 

Here, NULL is currently passed directly to the roCallInfo reference parameter. This function ultimately calls:

 vTimeChange(*pSeqDetails, roCPEvent, roCallInfo); 

which is defined:

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

roCallInfo possibly NULL roCallInfo to roCallInfo . I thought NULL could not be passed as a reference? Does anyone know if VC ++ 4.x has any problem that made such code ok? If NULL can be passed as a reference, then what happens when something like this happens in vTimeChange:

 roCallInfo.getCallStartTime(); 

Isn't it dereferencing NULL just as if I were doing

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

? I will probably guard there, and let the compiler delete it if it is not necessary, but I would like to understand how this happens!

Thanks.

+4
source share
3 answers

Depends on how NULL is defined in VC 4.2

If it's just

 #define NULL 0 

then you really get this under the hood:

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

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

+8
source

I thought NULL could not be passed as a reference?

A valid reference cannot be null, but null can be invalid.
Here you have an invalid link .

The fact that a link cannot be null does not mean that links are somehow safer than pointers, as you see here in this case. There can be many ways a program can lead to invalid links, your code is one such example.

Wikipedia Link:
There are also de facto ways in which reference can begin with invalid . Since the link is usually implemented as a base pointer, initializing the link to the pointer-dereference expression, it will usually be implemented by the compiler as a simple assignment from a pointer to the main link pointer. Thus , if you have a NULL pointer or a pointer pointing to an invalid location in memory, you de facto have a link pointing to NULL or an invalid location . C ++ purists would argue that technically, dereferencing NULL or an invalid pointer leads to undefined behavior in any case, so this does not violate the above statements that the link cannot be null or point to arbitrary places in memory , however, this ignores the fact that in this case the basic implementation simply performs the โ€œassignmentโ€ and there is no access to the memory location, therefore this initialization of the link usually does not cause problems, and programmers should be aware of the possibility of a de facto โ€œinvalidโ€ link in a real program.

Using Invalid roSequenceDetails reference will ultimately result in Undefined Behavior.

+4
source

The expression *pSeqDetails results in undefined behavior if pSeqDetails is null. Everything can happen. Most implementations do not do any special checks; your code seems to work until you do what the object actually requires, then it will not work. But implementation can lead to an immediate failure.

0
source

All Articles