0

In a C++ pgm, I have code that looks like

int expectedNewValue = pCellScanData->WLToggle(iWL);

where pCellScanData is a class and WLToggle is a member function of that class. Due to a programming error, I invoked this code with pCellScanData 0 (i e, nullptr), and it did not give a runtime error in a debug (unoptimized) build. The function WLToggle did not attempt to access any data members of the class. It is not a static member.

Is this behavior expected, or allowed by the C++ standard? Even if it is, I would expect the debug build to at least warn about this suspicious condition.

Using Visual Studio 2015 update 3 under 64-bit Windows 7.

UPDATE: I am asking two questions: (1) What does the current C++ standard say? This is addressed in the referred "duplicate" question, but using the standard in force at the time (6 years ago). (2) Why does Visual Studio debug runtime not give a warning or error for this undefined behavior?

Woody20
  • 791
  • 11
  • 30
  • You have undefined behavior. – πάντα ῥεῖ Dec 07 '16 at 19:44
  • Null pointer points to no object. Expression that evaluates to no object got undefined behavior. Undefined behavior in general case. In particular, it might be that method DOES NOT access `this`(which is null in this case, you can check that by debugger) implicitly or explicitly and thus doesn't raise platform's analog of segfault. Maybe it just accesses static members\fields or is a NOP? – Swift - Friday Pie Dec 07 '16 at 19:55
  • "`pCellScanData` is a class" -- no, it's not. From the way it's used it appears to be a pointer. – Pete Becker Dec 07 '16 at 20:37
  • 1
    Visual C++ needs to support calling member functions with null `this` pointers in order to support Microsoft's own APIs (or at least MFC). See `CWnd::GetSafeHwnd` https://msdn.microsoft.com/en-us/library/d64ehwhz(v=vs.120).aspx – Christopher Oicles Dec 08 '16 at 01:28
  • @ChristopherOicles: sounds plausible, but there should be some warning at runtime (only applicable to user-written code). – Woody20 Dec 08 '16 at 21:21
  • Still need to know what the current C++ standards say about this. – Woody20 Dec 08 '16 at 21:22

0 Answers0