LWN.net Logo

RE: nullable pointers

RE: nullable pointers

Posted Mar 31, 2011 16:00 UTC (Thu) by HelloWorld (guest, #56129)
In reply to: RE: nullable pointers by mister_m
Parent article: GCC 4.6.0 released

Of course, the compiler must ensure that a variable is never read before it is written. But this is fairly easy to do and does't require null.


(Log in to post comments)

RE: nullable pointers

Posted Mar 31, 2011 19:04 UTC (Thu) by mister_m (guest, #72633) [Link]

I am a bit of a novice. Would you mind explaining how I would enable the compiler to do that? I am missing some programming knowlege in this area I think = perhaps a suer defined type? In school, we always set unused pointers to null, and then check for null to see if we can dereference whatever is at the other end.

RE: nullable pointers

Posted Mar 31, 2011 20:36 UTC (Thu) by HelloWorld (guest, #56129) [Link]

> I am a bit of a novice. Would you mind explaining how I would enable the compiler to do that?
Unless you're the one writing the compiler, you can't. Of course, you could just use a language that does this by default, like Haskell or OCaml.

RE: nullable pointers

Posted Apr 12, 2011 16:04 UTC (Tue) by oelewapperke (guest, #74309) [Link]

The essence of the trick is that you simply make a type difference between "filled in" pointers and "null" pointers.

A tree would be defined like so :

abstract class TreeNode {}
abstract class EmptyTreeNode extends TreeNode {}
abstract class FilledTreeNode extends TreeNode {
TreeNode first;
TreeNode second;
}

If you combine this with runtime polymorphism you can do something like this (won't work in Java, but I'm using java code anyway)

void processTreeNode(EmptyTreeNode et) {}
void processTreeNode(FilledTreeNode ft) {
// you might want to do something useful here
processTreeNode(ft.first);
processTreeNode(ft.second);
// or here
}

then simply call processTreeNode(tree), and the compiler would figure out whether to call the first or the second function.

I would love for java to support this though.

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds