Michael N. Moran - Re: Null pointer check elimination (original) (raw)

This is the mail archive of the gcc@gcc.gnu.orgmailing 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]

Joe Buck wrote:

On Sat, Nov 12, 2005 at 04:01:07PM -0500, Andrew Pinski wrote:

Was there an example of:int f(int &);

int g(void) { int *a = 0; return f(*a); }

Yes this would be undefined code but so what.

In a case like this, gcc could emit an error (since we can already detect that a reference is always initialized to a null pointer). If it did so, I can almost guarantee that some free software package will be flagged (I recall writing such code myself 10+ years ago, before I knew better), but it still might be a good thing.

Of course, it's not hard to hide the fact that a reference is null from the compiler, and the compiler might then do optimizations based on the assumption that the argument to f is a non-null reference. That would be valid according to the standard.

Excuse me. IANALL nor am I a compiler expert but ... what kind of optimization might be done with the information that a reference should never be null? Especially within the server code (the implementation of "int f(int& a)" in this case.)

Perhaps incorrectly, but I tend to use a (C++) reference when I require the caller/client to supply a non-null "pointer" to a single object (as opposed to an array of objects.) Client code is free to dereference a pointer to an object (perhaps allocated in the heap) and invoke the operation.

And what is the meaning of code that does this:

int foo(int& a) { int* b = &a;

if(b ==0)
{
    a();
}
else
{
    b();
}

}

Not that this example makes any sense, nor would I intentionally code this way ... but...

Is function a() invoked? Is function b() invoked?

Or (most likely) am I just one of those people that hasn't groked the subtleties of the C++ standard despite having used it for more years than I care to remember ;-)

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]