null-pointer

About the non-nullable types debate

I keep hearing people talk about how non-nullable reference types would solve so many bugs and make programming so much easier. Even the creator of null calls it his billion dollar mistake, and Spec# has introduced non-nullable types to combat this problem. EDIT: Ignore my comment about Spec#. I misunderstood how it works. EDIT 2: I m...

Accessing class members on a NULL pointer

I was experimenting with C++ and found the below code as very strange. class Foo{ public: virtual void say_virtual_hi(){ std::cout << "Virtual Hi"; } void say_hi() { std::cout << "Hi"; } }; int main(int argc, char** argv) { Foo* foo = 0; foo->say_hi(); // works well foo->say_virtual_hi(); // ...

Can using 0L to initialize a pointer in C++ cause problems?

In this question an initializer is used to set a pointer to null. Instead of using value of 0 value of 0L is used. I've read that one should use exactly 0 for null pointers because exact null pointer representation is implementation-specific. Can using 0L to set a pointer to null cause problems while porting? ...

C89: Access violation reading 0x00 (difficulty with malloc)

I am developing C89 on Visual Studio 2010 Ultimate Beta (Win 7). I don't think I'm using malloc() correctly. I am new to C, so please excuse the beginner question. The goal of my program is to count the occurrence of words in **argv using a tree. hist.c #include "tree.h" #include <stdlib.h> int main(int argc, char *argv[]) { unsi...

When does invoking a member function on a null instance result in undefined behavior?

This question arose in the comments of a now-deleted answer to this other question. Our question was asked in the comments by STingRaySC as: Where exactly do we invoke UB? Is it calling a member function through an invalid pointer? Or is it calling a member function that accesses member data through an invalid pointer? With the ans...

Which of these will create a null pointer?

The standard says that dereferencing the null pointer leads to undefined behaviour. But what is "the null pointer"? In the following code, what we call "the null pointer": struct X { static X* get() { return reinterpret_cast<X*>(1); } void f() { } }; int main() { X* x = 0; (*x).f(); // the null pointer? (1) x = X::get(); ...

Calling a method on an unitialized object (null pointer)

What is the normal behavior in Objective-C if you call a method on an object (pointer) that is nil (maybe because someone forgot to initialize it)? Shouldn't it generate some kind of an error (segmentation fault, null pointer exception...)? If this is normal behavior, is there a way of changing this behavior (by configuring the compiler...

Android - Dealing with a Dialog on Screen Orientation change

I am overriding the onCreateDialog and onPrepareDialog methods or the Dialog class. I have followed the example from Reto Meier's Professional Android Application Development book, Chapter 5 to pull some XML data and then use a dialog to display the info. I have basically followed it exactly but changed the variables to suit my own XML...

Null pattern with QObject

(C++/Qt) I have a smart pointer to a QObject. Let's say a QWeakPointer. For some external reason (something that might happen in another object or due to an event), it is possible that the pointed object gets destroyed. Since I have a smart pointer there will be no dangling reference, so there's no problem. But I always have to check if ...