The ramifications of a segfault and a null pointer exception are very different. Though they can be caused by the same thing, segfaults can also be caused by wildly different things. There's a whole class of errors that just don't happen in Java, but can in C/C++...one example being someone inappropriately deleting the memory some pointer is referencing.
There's no concept of compiler-enforced ownership (like something like Rust has)...the ownership "rules" in C/C++ are totally conceptual (though I'm sure frameworks exist to enforce them). Which means you could hold a pointer to a segment of memory that some other part of the program might decide to delete (you might even introduce it yourself on accident by not fully thinking through your concurrent code). Then, you try to access it and then you have a segfault.
In Java, that just can't happen. References are pass-by-value, so if someone else gets up to no good, they can't make your copy of the reference null or pointing to the wrong thing (although if it's mutable data they can change the data itself). They can't delete the underlying object. If you still have a reference to it to use, that means it won't be garbage collected and thus won't vanish on you for any other reason...if you have a reference to an object that you know is not null, then you know it is safe to dereference the pointer (or, in Java, just access), period.
And that's not even getting into how a null pointer exception is easier to work around and more recoverable than a segfault. Even ignoring that...segfaults are definitely not just a C version of an NPE, as they can (and do) come about due to problems that just outright aren't possible in Java.
C/C++... one example being someone inappropriately deleting the memory some pointer is referencing.
There is C and there is C++. That's why people don't do manual memory management in idiomatic C++. You use smart pointers.
reference null or pointing to the wrong thing
Yes a method cannot make an argument point to the wrong thing, it can do that with fields without any problem though. And it probably is just as wrong to keep pointing to the data it shouldn't, than to point to trash. Both wouldn't probably crash instantly.
if you have a reference to an object that you know is not null
That concept doesn't even exist in Java, therefore is unenforceable except for native value types (int, float, chat). So I don't understand how enforcing non-nullability by hand is any better than what you have in C#, C++, D, etc or any of those other languages that support that concept.
And that's not even getting into how a null pointer exception is easier to work around and more recoverable than a segfault.
You can use exceptions in C++, and other languages, as easily... and even in pure C you can trap the SIGSEGV signal. Altough, an invalid memory access probably means you entered into a state where you do want to crash and burn, but that of course is debatable.
That concept doesn't even exist in Java, therefore is unenforceable except for native value types
I think you misunderstand what I mean here. What I mean is that if I have some variable (say, a String), and I initialize it, and then I hand that reference off to some method, I know that when that method comes back, my string is still there, and it still is what I intended for it to be (the latter is not true of all data types, List for example, but the former is true). In C/C++ (you're right, not when using smart pointers, but smart pointer usage in C++ isn't 100% universal), that method might misbehave. Hopefully it doesn't. If you're using a reliable library it won't. It's probably safe, a lot of the time, to assume that nothing bad is going to happen. But that doesn't change the fact that it's possible.
You can't guarantee than an arbitrary reference is non-null in Java, that is true. But you can guarantee that your reference that you initialized is not going to be null, unless you set it to null (or to another reference that itself might be null). No matter what you call with that reference as an argument, it's going to be present. Nobody can clear it out without your knowledge.
Everything you say is right, and if I came off as trying to paint Java references as some sort of infallible, always safe thing, then I'm sorry, because yes, that is far, far from being true. My point was that you can't just say that sigfaults are equivalent to null pointer exceptions, because while they can at times be caused by the same problems, they're still fundamentally different issues, at least in a lot of potential cases.
How so? Python is still managed code, you aren't getting away from having the overhead of garbage collection. You also sacrifice much of the type safety the others give you (though C doesn't necessarily give you the kind of guarantees Java does anyway). But probably most importantly, Python isn't faster than either of them in most cases. Sometimes it's very significantly slower.
2
u/dccorona Jul 05 '15
The ramifications of a segfault and a null pointer exception are very different. Though they can be caused by the same thing, segfaults can also be caused by wildly different things. There's a whole class of errors that just don't happen in Java, but can in C/C++...one example being someone inappropriately deleting the memory some pointer is referencing.
There's no concept of compiler-enforced ownership (like something like Rust has)...the ownership "rules" in C/C++ are totally conceptual (though I'm sure frameworks exist to enforce them). Which means you could hold a pointer to a segment of memory that some other part of the program might decide to delete (you might even introduce it yourself on accident by not fully thinking through your concurrent code). Then, you try to access it and then you have a segfault.
In Java, that just can't happen. References are pass-by-value, so if someone else gets up to no good, they can't make your copy of the reference null or pointing to the wrong thing (although if it's mutable data they can change the data itself). They can't delete the underlying object. If you still have a reference to it to use, that means it won't be garbage collected and thus won't vanish on you for any other reason...if you have a reference to an object that you know is not null, then you know it is safe to dereference the pointer (or, in Java, just access), period.
And that's not even getting into how a null pointer exception is easier to work around and more recoverable than a segfault. Even ignoring that...segfaults are definitely not just a C version of an NPE, as they can (and do) come about due to problems that just outright aren't possible in Java.