[geos-devel] Why empty LineString should return a null pointer from getCoordiante()

Stephen Woodbridge woodbri at swoodbridge.com
Thu Mar 30 22:21:01 EST 2006


Mateusz,

I think this thread is great and I am learning a lot about GEOS 
internals and about C++ in general. I can not imagine anyone taking 
exception to your trying to learn more about GEOS or trying to explain 
your reason behind you suggestions. GEOS is an important tool for us in 
the C/C++ world so many thanks to you for trying to make it better, for 
Sandro for all his efforts in porting JTS and Martin for his efforts in 
creating, and all the others that I have missed calling out.

Personally, I appreciate all the effort.
Thank you all,
   -Steve

Mateusz Łoskot wrote:
> Hi,
> 
> Please, forgive me that I'm annoying with all those recent questions but
> I really would like to understand GEOS internals.
> 
> 
> Here is getCoordinate:
> 
> const Coordinate* LineString::getCoordinate() const
> {
>    // should use auto_ptr here or return NULL or throw an exception !
>    // 	--strk;
>    if (isEmpty())
>       return(new Coordinate());
> 
>    return &(points->getAt(0));
> }
> 
> as you see, there is Sandro's comment about what could be best.
> I think it's time to answer this question :-)
> 
> For comparison and reference, here is JTS' version of this function:
> 
> public Coordinate getCoordinate()
> {
>    if (isEmpty()) return null;
>    return points.getCoordinate(0);
> }
> 
> Here are my comments to current solution:
> 1. Returning newly allocated Coordinate is confusing and will cause
> problems that user won't free the memory
> 
> 2. This brokes the logic because how empty linestring can return any
> Coordinate? This does not make any sense.
> 
> 
> Here are my comments to possible solution:
> 
> 1. auto_ptr will not solve the problem because it won't fix broken logic
> will
> 
> 2. My suggestion is to return *null pointer*
> 
> As you can see above, JTS version returns null reference.
> 
> What is Java null reference (a pointer to nothing)?
> It is a reference that does not refer to any instance of any type.
> 
> What we can do with Java null reference?
> null reference can be safely compared with other references (null or not
> null), null reference can be safely passed to the function, returned
> from function, null reference can be safely assigned to the existing
> reference etc.
> What we can not do with null reference is dereferencing, e.g.:
> 
> Type x = null;
> x.Fire(); // BUM!
> 
> Now, how to transit this Javism to C++. It's very simple - just use null
> pointer.
> 
> What is null pointer in C++?
> Null pointer is a *valid* pointer value that doesn’t point to anything.
> Just as null reference in Java.
> 
> Similarly, with null pointers in C++ we can do (almost) the same as what
> we can do with Java null reference.
> 
> The only thin that can not be done with C++ null pointer is
> dereferencing. Dereferencing null pointer causes undefined behavior
> This rule applies to every pointer that does not point to point to anything.
> 
> What I'd like to repeat is that "null pointer is a *valid* pointer",
> but not invalid. Invalid pointer is another category and *any* use of
> invalid pointer causes Undefined Behaviour, even comparing, etc.
> 
> So, I vote for returning null pointer.
> 
> 3. Throwing an exception is a bad idea
> 
> Exceptions should be used for unexpected situations but when user is
> asking object for coordinate he is aware that the object may or may not
> include any coordinates. So, there is no unexpected behaviour here.
> 
> 
> BTW, I'm really sorry if my explanation seems to be like in books of
> seriece "XXX for dummies". This is completely not my intetion!
> Simply, I usualy try to explain my point as clear as possible.
> 
> 
> Cheers




More information about the geos-devel mailing list