Results 1 to 10 of 30

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    Quote Originally Posted by phibermon View Post
    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.
    The distinction is important when you do not explicitly use the Nil constant value. Assigned may have been originally intended for that, it became used generally for opaque pointer references, to maintain that opacity.

    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.
    Assigned() is a built-in function handled by the compiler. I don't think it is overloaded, as there are no definitions to overload. It only works on types which ultimate derive from, or are identical to, the Pointer type. It shouldn't be any slower than a comparison to Nil, since that is what it is replaced with at compile-time, at least on the Windows platform.

    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.

    I have no idea what you are referring to with respect to Assigned. It is simply a built-in function which the compiler hard-codes to a Nil check. If you're not explicitly using Nil as a data value, or are dealing with opaque (pointer) types, it is more "proper" to use Assigned.

  3. #3
    PGD Staff / News Reporter phibermon's Avatar
    Join Date
    Sep 2009
    Location
    England
    Posts
    524
    Oh fair enough I was going off some page I googled in terms of Assigned which showed three definitions, just checked in FPC and yes it's built in.

    http://stackoverflow.com/questions/4...ssigned-vs-nil

    May as well use assigned. doesn't make a blind bit of difference really. If you're dealing with method pointers to methods that return pointers/objects then using assigned will help you avoid making mistakes I suppose but if you were doing that and used pointers / nil comparisons a lot then you wouldn't make that mistake anyway. Well you'd hope
    When the moon hits your eye like a big pizza pie - that's an extinction level impact event.

  4. #4
    Well, here's the point. For some implementations, Assigned() may not be just a simple Nil check. For example, let's say that the Foo implementation of FPC does a Nil check AND a check for "0xbaadf00d" (uninitialized data value) to determine if a pointer is valid. If you port your code to Foo, which just checks Nil with a conditional, it may cause an unintended side effect.

    Practically, they are identical, but methodologically, they aren't. It goes back to the same reasons why it is bad to do pointer arithmetic, as in C-like languages... pointers need to be treated as opaque types, and depending on them being specific constant values is just generally a bad practice overall (and why Object Pascal is a much superior language than, say C++).

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
  •