Results 1 to 10 of 30

Thread: Yet another segfault (a.k.a. Darkhog is a total noob)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #21
    PGD Staff / News Reporter phibermon's Avatar
    Join Date
    Sep 2009
    Location
    England
    Posts
    524
    Quote Originally Posted by Murmandamus View Post
    Just to echo something that was said earlier...

    Whenever you use the condition:

    If SomePointerReference <> Nil Then

    it is much better (more portable, readable, etc) if you use this instead:

    If Assigned(SomePointerReference) Then

    It is an inline function/macro, so it shouldn't be less efficient.
    Assigned() was created to allow coders to make clear distinctions between checking to see if a func pointer is nil and checking if the result of an assigned function is nil. so without Assigned : MyFunc=nil and MyFunc()=Nil and with assigned : Assigned(MyFunc) and MyFunc()=nil.

    Probably best to use Assigned for readability, It's most likely an inline function however It's also an overloaded function so I believe there's run-time type checking going on, don't really know but probably is. If so it'd be technically faster to stick with nil comparisons. Maybe, I'm only assuming that type checking for overloaded functions is run-time for all types, I think it is for class types. Or maybe not, class instance reference being a pointer, maybe you can't have overloaded functions for different class types with the same calling conventions, meaning that overloading might be a compile time thing. Perhaps the compiler will optimize when the program only ever uses one version of the overloaded function. Maybe.

    I'm probably giving this too much thought. I'm a bit bored today. In fact it'd probably just be quicker to fire up Lazarus and try overloading a function with different class types.

    EDIT : yes you can overload with identical calling convention for different class types so it's Run Time type checking and nil comparisions are faster (At least in ObjFPC mode with FPC) for class references and pointers. Assuming of course that a nil comparison isn't also subject to run-time checks, which it probably isn't as it wouldn't get past the compile time checks.

    I should imagine overloaded functions with different conventions are sorted out at compile time though.
    Last edited by phibermon; 28-07-2013 at 05:22 PM.
    When the moon hits your eye like a big pizza pie - that's an extinction level impact event.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •