This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Null pointer check elimination


Gabriel Dos Reis wrote:
"Michael N. Moran" <mike@mnmoran.org> writes:
| void bar(int& a);
| | void foo(int* a)
| {
| // dereference: conversion to reference
| // Since there is not necessarily any object access,
| // thus no assured SEGFAULT.
| bar(*a);


SEGFAULT is not a behaviour defined by the language.

Even *I* know that, in spite of my lack of legal expertise.


It is *just* one form of undefined behaviour.

Are you saying that this portion of the code *will* invoke undefined behavior even if the pointer "a" is valid?

Notice this example has nothing to do with null
pointers. It is a demonstration of the two kinds
of dereferences that I identified.

Perhaps the problem is that I am assuming the same
object code generated by this function is used for
all cases (e.g. null or valid values of "a",) and
that assumption falls apart in the presence of
optimizations such as inlining and inter procedural
optimizations.

If you execute that function, it might
reformat your harddrive and that woud be fine -- though I know of no
compiler that does that on purpose.  But, the point is that your
program in erring in the outer space.

|      // dereference: access to object
|      // If a is null, then SEGFAULT
|      *a	= 0;

Again, that may or may not happen.

I understand that specifically a SEGFAULT may not happen, but according to the excerpt from the GCC info page above,

"The compiler assumes that dereferencing a null pointer
would have halted the program."

In the first case, the result of the dereferencing *seems*
to be an address of the object in the form of a reference,
whereas the second case *seems* to clearly modify the object
(assuming the object is in memory ... whatever that means in
standardise).

<sigh>
FWIW, I appreciate your help, and realize that this is not
the forum for this kind of discussion. I apparently need to
get educated . Thank you for your patience.

--
Michael N. Moran           (h) 770 516 7918
5009 Old Field Ct.         (c) 678 521 5460
Kennesaw, GA, USA 30144    http://mnmoran.org

"So often times it happens, that we live our lives in chains
 and we never even know we have the key."
The Eagles, "Already Gone"

The Beatles were wrong: 1 & 1 & 1 is 1



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]