“Nobody likes me. I’m despised everywhere I go. I am accussed of being the responsible of many problems and even calamities. Some say that I am accursed. Some even say that I am some kind of disease that needs to be cured. When people see me the first thing they think is “oh no, not again…”. Nobody has a kind word for me. People spends hours and hours finding ways to make sure that I am not welcomed. They spend incredibly amounts of time looking for ways to avoid me, to squash me and to make sure I cannot even get close. Even worst, some people has managed to make me invisible to their eyes, so even if I appear they do not see me. If I manage to get close and say “hi”, they do not say “hi” back to me. In fact, whenever I say something people look at me very weirdly. People has been told since the beginning that I am the worst thing that could ever happen to their software.
They do not understand me. And in fact I think nobody understands me. But that is ok, because no matter what I will still be around after your software is done. I will still be around when your software becomes outdated. In fact, you can count on me being around all the time. And yet I’m not haunting you. If you just stopped for a while and listened to my message, you will see that I can be your best friend. My mission is very simple: if I appear in your software it is not a sign that your software is badly designed, I’m just pointing out a case that was not considered before. If you could just understand that sometimes it is much better that I come and say “hi” than to close the door to me, you will realize that we can work together to make your software the best software ever. So please, stop cursing me and for once try to listen to my message.”
The text above was inspired by a conversation with some colleagues regarding when to detect a NULL pointer and when simply let the software crash.
No related posts.
39 comments
That is why I always initialize my pointers with random values
And by “random” I hope you mean 0xdeadbeef or 0xbaadf00d
I must say, that the NULL (or 0) pointer is actually something quite useful.
Worse is the invalid pointer, as a null pointer is perfectly okay, its a not-valid-pointer.
One if you’re done. But the invalid Pointer will slip through this, and, give you quite a job to find him, if the debugger doesn’t track him down.
Also, if useful, you always should think about using smartpointers instead.
Their less risky, still even they can fail.
This make me think of the first times I sweared at QObject after programming for Symbian where CBase took care of initialising to null every member of your class for you. That was one of the (very) few things I liked about Symbian C++ actually
Wow, poor null pointer, made me cry.
I like Python’s Null, which is ‘None’ object (singleton) of a ‘None’ type.
I wonder what a QObject-derirved QNone would be like…
It could also work as Javascript’s ‘null’
0 is a hack at best. It is clearly an integer (or long, or long long, or other, but I digress) NULL is at least a /pointer/ to something, which is not in existence. Which itself is at least pointer. So at a minimum you know that you’re not working with something, but rather a variable pointing to no instance.
“Now I am nothing” – Trent Reznor.
Not me. I love you NULL pointer. You keep me out of trouble by declaring what you really know. You don’t send me on my way with incorrect information with was correct when you recieved it, but because you haven’t kept up with current events, no longer holds true. Nor do you just declare whatever’s on your mind, because you never bothered to get informed in the first place …. unlike your evil brother; dangling pointer. Now he’s a deceptive trouble maker, and if I ever see him, I destroy him, and gladly invite you to replace him.
I think it’s much better to use assert to check for null pointer, than to let software crash with silent segfault
NULL is a macro which define cast of 0 to (void *).
).
* It’s C not C++.
* Pointer value of this cast is not necessarily 0 (but in the real world is always 0
* C++ compilator redefine NULL to 0
* C++0X add nullptr keywords
* Don’t use NULL macro in a beautiful C++ code
http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/nullptr
So, with old compilator replace NULL by 0 and with C++0x use nullptr. Very good crash and explicit error appear when a used pointer is not initialized
.
Null pointer is like your conscience..listen it!
@Konstantin Tokarev
assert is only of use during developement — during debugging — but is of no use for your end users unless you give them debug-versions of your program.
@JW: You are aware that the 0-pointer is not necessarily invalid, are you? 0 is a perfectly valid address, it is just a convention to have the kernel make sure that this page is never available (thus triggering a segfault when accessing anything in that page). There are ways to map data to this page though and some really interesting attacks on a linux system were possible due to this fact a while back.
Well, don’t worry, they made an exception for you.
Too bad Trolltech doesn’t it its own food (Qt doesn’t use smart pointers).
Too bad Trolltech doesn’t eat its own food (Qt doesn’t use smart pointers). (corrected grammar)
“Nobody likes me, everybody hates me, I’m going to let you make worms!”
(By the way, this is the third time I’ve tried to enter this. The first two CAPTCHAs were illegible, thanks to i/1 and O/0 ambiguity. There’s a perfectly good CAPTCHA system out there called reCAPTCHA – please consider using it.)
If you’re on a mac if you set the environment variables MallocScribble=1 and MallocPreScribble=1. This helps detect uninitialized pointers and free’d memory.
@KarlNapf :
[...]
6.3.2.3 Pointers
[...]
3 An integer constant expression with the value 0, or such an expression cast to type
void *, is called a null pointer constant.) If a null pointer constant is converted to a
pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal
to a pointer to any object or function.
[...]
6.5.3 Unary operators
4 The unary * operator denotes indirection. If the operand points to a function, the result is
a function designator; if it points to an object, the result is an lvalue designating the
object. If the operand has type “pointer to type”, the result has type “type”. If an
invalid value has been assigned to the pointer, the behavior of the unary * operator is
undefined.87)
[...]
NOTE 87: [...] Among the invalid values for dereferencing a pointer by the unary * operator are a null pointer
[...]
Funny note:
The standard does no specify that the null pointer (0) should actualy be equal to the have the value 0.
So int *ptr = 0; does not mean ‘ptr’ should have the address 0 once deferenced. (even if does on all the platforms I know)
For example, a null pointer to member will be -1 on most platforms
int Foo::*bar = 0;
std::cout (&bar);
will display -1
(because my code in my previous comment did not render well: http://pastebin.com/JPxpLeWq )
I consider the Qt’s decision to “not to use references, because it makes code less readable, because in case of methods returning stuff through out arguments you can’t see they really change stuff” (explanation from Api Design Principles wiki on gitorious: http://qt.gitorious.org/qt/pages/ApiDesignPrinciples#Pointers+vs.+References) one of the failures of Qt.
First of all, this explanation assumes that the arguments you pass exist on the stack, i.e. you keep them as objects or dereferenced pointers, thus you’ll have to use ampersand operator to send them to the method. If you keep some object through pointer (which is encouraged by Qt API), the code is equally “unreadable” as in case of using references as out arguments. I’d rather say that returning stuff through out arguments “void getStuff(Foo* outFoo, Bar* outBar)” is not very nice API and leave the whole pointer/reference argument out of this.
If you get some mandatory arguments as pointers, the best you can do is to perform some preconditions check that the pointer is not null and throw an exception (or *shudders* faile silently). You also have to rely on documentation to indicate that the argument is mandatory. In case of reference you don’t have any of those issues: there is no such thing as null reference.
Optional arguments are another issue – pointers can be used for that, but I find features like Haskell’s Maybe or boost::optional safer and more readable.
Actually Qt objects use a QScopedPointer for their private implementation (using the pImpl idiom) internally. I guess not using them _everywhere_ is a performance trade-off?
Performance trade-off is not the reason why we do not use more QSharedPointer inside Qt (as QSharedPointer does not cost much)
One reason is that this class is relatively new and most of Qt code is way older.
We also use the QObject hierarchy which is a different way of managing memory.
@chalup
All disadvantages of pointers in C++ can also be applied to references. They can dangle and as a consequence they can be NULL. Consider the following:
QString* string = NULL;
test((*string));
with test as
void test(QString& test)
{
qDebug()
@chalup
All disadvantages of pointers in C++ can also be applied to references. They can dangle and as a consequence they can be NULL. Consider the following:
QString* string = NULL;
test((*string));
with test as
void test(QString& test)
{
qDebug() test;
}
this will crash inside qDebug() .
And you say using arguments for out paramaters in not very nice. Well what else could Qt do if there is more then one argument if it does not want to become a template beast like boost *shudders*?
sry the system ate some of my post before
@chalup
All disadvantages of pointers in C++ can also be applied to references. They can dangle and as a consequence they can be NULL. Consider the following:
QString* string = NULL;
test((*string));
with test as
void test(QString& test)
{
qDebug() test;
}
this will crash inside qDebug() .
And you say using arguments for out paramaters in not very nice. Well what else could Qt do if there is more then one argument if it does not want to become a template beast like boost *shudders*?
@hunt0r
You’ll have “dangling” reference only if you store the passed value. But in that case you should rather use QSharedPointer (or another class providing such functionality), because you, well, *share* the passed object. If you don’t store the value, but use it only in scope of the called method, I see no downsides to using references.
If you want to return multiple values, return a struct – it’s more readable than out params. You don’t have to use boost::tuples (I guess that’s the “template beast” you mentioned).
Besides, wouldn’t your example crash on dereferencing NULL pointer?
“My mission is very simple: if I appear in your software it is not a sign that your software is badly designed, I’m just pointing out a case that was not considered before.”
I agree, the software is not badly designed – instead, it is the whole programming language in which the software is written. So, technically, you are right, the software is not to be blamed. We have been using badly designed programming language for sooo long that we simply got used to them, and forgot that type-safe high-performance languages in which the NULL pointer is *not* a nightmare are actually possible.
As a bonus, below is a C++ program which will compile just fine (by GCC), with a mere warning printed out. Who cares, just a warning. When one sees a compiler that is accepting obviously incorrect programs, the only conclusion that can be derived is that the designers of the language and of the compiler are TOTALLY INSANE people who have NO IDEA about programming language design.
int& fn() {
int i=10;
return i;
}
… the NULL pointer is not alone, in C/C++ it has many many friends.
… though I don’t actually expect the author of “Apology of the NULL pointer” to grasp this.
@chalup
no the examples will crash in qDebug(). Why shoud it crash before? The compiler wont insert a check for NULL on (*string). And you will get dangling references not only if you store the value. You will get dangling references in the same situations when you wou would get a dangling pointer. (That is if you want pass a bad pointer to the method). The downside of references is that you cant see that the values will be modified (unless you pass a pointer which you dont do in 99% of the cases).
And for returning structs: Woudnt the compiler generate some constructors for the structs? So if you declear a struct for every function that returns multiple arguments that would encrease code size significantly.
Wow, despite the fact that this is a controversial topic I didn’t expect this amount of replies. Specially not the ones attacking people because of what they say (and I don’t mean just the post by “*” which is a personal critic to my capabilities).
It is interesting to see that people are at least trying to have a discussion, and as the NULL pointer said, at least they are trying to answer to it. We have been told since the beginning of our programming days that NULL pointers are a bad idea. We have even shown examples of languages who manage to avoid the problem at all by controlling the memory for you and that is cool. However the point I was trying to make is simple, there are some situations where the only sensible thing to do is to let your application crash. Detecting a NULL pointer by introducing the classical:
{code}
if(!p) {
….. handle situation ….
}
{code}
might seem like a good idea, but there are situations where this will simply not work. Or to put it mildly, the consequences of stopping and handling the situation are worst than the consequences of just letting the app crash. For example, what can you do if you are deep inside a paint engine and you get a NULL pointer because you ran out of video memory? How are you going to continue on that situation? Just imagine the situation and you will realize that knowing that there is a NULL pointer gives you nothing because you still need to keep painting, and you will request more memory very shortly. Can you go back and tell whoever is requesting you to paint stuff to cool down and wait until the memory situation is solved? And by the way, who gives you any warranties that the situation is going to be fixed in the first place?
Now, please don’t come with the special cases where your application is handling a nuclear reactor and you cannot crash. That is a completely different story, and the way you will approach that will be very different. To begin with, you will avoid as much as possible the dependency on memory being available when you request it. Even more, you will have a redundant system to make sure that you can handle the situation if need arises. But in a normal application there is very little you can do, except crash or stop your application.
Finally, some clarifications. Memory it is a scarce resource and that is independent of the programming language you are using. There are very few programming languages that will offer any warranties on memory always being available when you request it. And that usually implies that you need to say at the beginning how much memory you will need, so the environment can reserve that memory for you and have it allocated and ready to be used. Even if the programming language does that, there is actually no warranties that the Operating System is going to provide that memory. In modern operating system it is a common practice to overcommit memory. I.e. accept as many memory requests as you can and only assign memory at the moment it is used. So even if the programming language runtime requested memory, it is still up to the operating system to be kind enough to actually have the memory ready for you. Even more, let’s assume that your operating system is kind enough to have memory enough for you to use, that still does not prevent from hardware failures. Back in 2005 I was working for a consultant firm in Belgium and my mission was to bring up a board delivered by a customer. The board was basically a PowerPC e series with some extra hardware implemented into an FPGA. As a conscious firmware engineer the first thing I did was to make sure the memory worked by writing and reading memory from it. Everything ok so far, there were no problems. So I just moved into porting redboot into the board and that is when the fun began. For those of you not very familiar with bringing up boards and bootloaders, usually the bootloader is stored into a secondary memory, usually flash or eeprom (or some other variety of ROM memory) and the processor is hooked to boot from that memory. The first thing you do in a bootloader is to initialize the mappings inside the processor and move the code you want to execute to RAM memory (not necessary but it improves speed considerably) and then you jump to that code and start doing your stuff (by the way, when doing that having a NULL pointer that equals to 0 is very useless since 0 it is a pretty perfectly valid address and many processors assume that when using linear memory you will start the memory from 0!). To my surprise the board didn’t want to boot and the processor always reported an exception. Note of the Editor: when booting it is usual to have all exception processing turned off until you have the environment up and running, since what you are doing is pretty simple this is a perfectly valid assumption. Many hours later, some low level hardware debuggers and some oscilloscope I realized that the problem was simple. The execution flag of the memory was permanently wired to 0, so there was no chance for me to use the memory for running code…. Hopefully the customer reacted quickly and we received a new FPGA model that had that fixed so we were able to continue.
With all that being said, just remember that “there are no silver bullets”. A NULL pointer is not something that can be handled only on the language side, nor even in the operating system side. So don’t trust that because you are using the language X with extensions Y and you are programming using paradigm Z you are free from being visited by the friendly NULL pointer.
cduclos, I think we have totally different viewpoints about what a statically typed programming language is for and what it can do or should be doing. From the entire message you just wrote, I think I only agree with the statements about overcommitment of memory by the OS.
You wrote: “A NULL pointer is not something that can be handled only on the language side”. What are you implying? That it is impossible to design a programming language that will prevent you from dereferencing a NULL pointer. In other words, that such a language, in which *all* accesses via a pointer would be bound *not* to crash the application, is impossible? If that’s really what you are thinking, then … oh my god …
You wrote: “We have been told since the beginning of our programming days that NULL pointers are a bad idea.” I think you are totally confused. The concept of a NULL pointer is *not* a bad idea. The bad stuff you are talking about comes *solely* from uncontrolled usage of NULL pointers.
You wrote: “We have even shown examples of languages who manage to avoid the problem at all by controlling the memory for you”. I hope you don’t mean Java, C#, Go or anything like that. Because all those languages actually *do* have NULL/nil pointers. Even Smalltalk has a nil object. Of course, that is not a bad thing by itself – the bad stuff comes from allowing totally uncontrolled placement and usage of NULL pointers.
You wrote: “… Can you go back and tell whoever is requesting you to paint stuff to cool down and wait until the memory situation is solved?” Yes, you can, or at least you could. It can be done even in C++ by utilizing exceptions. The alternative you are suggesting (to crash the whole application) is an extreme solution, the very last resort. As a counter-example to your “solution”, imagine you have an advanced text editor running and it is low on memory. It tries to allocate memory, but that fails. You are suggesting that the best course of action is for the whole editor to simply crash, instead of trying to remedy the situation by disabling some of its non-critical parts (disabling the spell checker, unloading cached fonts, compacting memory, and so on). It seems to me that if an application has a good architecture then automatically disabling non-critical parts in response to a memory allocation failure is actually pretty easy. Well, of course, most Linux applications are incapable of doing this – but what does that really mean: that it is a generally bad idea, or that the architecture of all those applications sucks? Personally, I think I would vote for the latter.
I don’t understand why you seem to be trying to reduce the whole NULL-pointer issue into a memory allocation issue.
Mr “*”: I think you are the only one in the whole thread that interpreted my message as “let the application crash all the time”. If that is the case, then let me assure you that my message is far from that. My message is simple, there are times where handling the a NULL pointer it is a good thing and situations where the handling will create very obscure bugs that will make debugging almost impossible. In those situations, letting the application crash is the only sensible thing to do. If you like to go the other way, i.e. handle the situation and find an alternative route then “vær så god og lykke til”.
Now regarding the abused concept of “NULL” pointer. Let’s examine the issue in detail, what is exactly a NULL pointer? it is a pointer that is pointing to a special section of memory that will trigger an exception when the memory management system tries to resolve it. It is important to notice that a NULL pointer is just a definition and it has no difference with any other pointer, except that the virtual memory address to which is pointed it is marked as a special one and it will trigger a exception when accessed. There is nothing more than that in the case of a NULL pointer.
Let’s talk about the other common problems encountered when dealing with pointers. The first one of course is that your pointer was pointing to a piece of memory that is not valid anymore. For instance you requested memory, and then released the memory. If you keep a pointer to that piece of memory, there is no warranty that what you will get after the releasing of the memory is going to be valid… there is not even a warranty that the memory is going to belong to your program at that point! Now, operating systems are quite smart beasts and for the most parts they don’t like revolving memory all the time, so you might get lucky and that page can still be a part of your memory space. Since memory is nothing but a collection of bytes (or bits if you prefer to talk in bits), whenever you reference that memory there is no way to distinguish valid data from garbage. As long as you can access the memory then the contents can be assumed to be valid. And that is the first cause of troubles, because data is going to be read and is going to be interpreted to match whatever data type you are trying to access. So, actually dereferencing a pointer and reading data from it, it is a big faith act. You trust the whole system that what you are reading is actually what you wanted to read! Situation gets a little bit better if you are doing something fancier, such as using function pointers and trying to read code from memory. Code gets interpreted by the processor and the chance that garbage is going to be valid code are minimal, however it can still happen. Although this is rare and most of the problems are caused by data, not by wrong code being executed.
What is the second problem? Well, you forgot to initialize a pointer and that pointer is pointing to the twilight zone. If for any reason that pointer points to memory inside your memory space, then you go back to case number 1. If not you simply get an invalid memory access and you crash.
But these two situations are not caused by NULL pointers. This are caused by dangling pointers, the evil brother (as somebody mentioned) of the NULL pointer.
What can one do against dangling pointers? Well…. very little, unless they point to memory that is outside your memory space, the process will continue to run and data is going to be interpreted. Some languages have tried to detect dangling pointers and some of them have been quite successful. I should say that to handle dangling pointers, just the pure semantics of a language are not enough. A whole runtime needs to be created to provide the infrastructure to isolate your program from the rest of the system. Since the memory is not controlled by you, you can count on it to be handled correctly, as long as you follow the semantic and ideas of the language you should be safe. Even languages with full memory management will require you to do things in a certain defined way in order for the runtime to know what can be freed, what can be kept and when to request more memory from the OS.
Take any language with full memory management and you will see that you are expected to do things in a very defined way. There are languages that try to mix both the benefits of memory management with the freedom of not having memory management. For instance Objective C but the mixture of both requires to be even more disciplined than when using only one scheme.
Now, let’s go back for a while to NULL pointers before getting to the final part of my argumentation. Given that a dangling pointer is something that is pointing to an area of memory that might be invalid, and that a NULL pointer is a definition, how do I can end up with a NULL pointer? Ending up with a dangling pointer can be easier than ending up with a NULL pointer, but that is language/runtime dependent. A NULL pointer is only gotten through variable assignment or via a failed memory assignment. The first case is easy to see, since all that is required is a simple:
int *p = NULL
and voila! p is a NULL pointer (adapt to the syntax of your preferred language and add salt and pepper to taste). What about the second case? Well, a well behaved memory “management” method will return NULL when it fails. Take for instance malloc(…). If the allocation fails it returns you a nicely dressed NULL pointer. It even sets the ERRNO environment variable for you so you can see what went wrong. There could be other obscures ways of getting a NULL pointer, however and since it is friday after 18:00 they escape to my imagination now.
What can you do with a NULL pointer? Well, as said, they are very easy to spot since their value is a constant and you just need to compare your pointer to that constant. The second part of the question is more difficult, since what to do when a NULL pointer is spotted is not easy to decide and I do not feel inclined to give general recommendations. However I can say that sometimes when you detect a NULL pointer the best thing to do is to take corrective actions and handle the situation, and some other situations where there is nothing you can do and the best course of action is let the app crash.
So, let’s go back to languages/runtimes and see what can be done to handle these situations. Let’s assume that we have the perfect language where whenever memory is requested, the runtime will do magic and will get you memory. Let’s assume that the concept of pointer in that language avoids the possibility of dereferencing and all you can do is to either set them to some value or read values from them. Such a thing seems quite reasonable, after all what we really want is just to store data and read data some time afterwards. Since we cannot get the “address” of a particular memory, i.e. we cannot dereference the pointer, then the chances that you are going to get a dangling pointer are slim, although it could happen. It is still to be decided how you will handle assignments between objects. Are you going to create a new object and copy the data from a to b, or are you going to try to optimize and only copy data if it is strictly required? You could just reference data from a in b and only change it in b when you actually set the value in b to a new value. I’m not going to argue about the goodness of both approaches, both are valid approaches and the decision must be taken depending on the situation at hand. If one could build such a language, then it seems all the problems with dangling pointers will be solved once and for all.
However there is still one thing that needs to be resolved, and that is the assumption of having a runtime that no matter what will give you memory when you request it. This is the equivalent of having an infinite pool of memory, which can be simulated. I used to work in projects where you could not use more than X memory, so by having nX with n>1 you were actually providing infinite memory. If that can be provided, I would definitively go that road. Unfortunately for most of us, those situations are very few and very far in between. The usual situation is the situation where you can only estimate how much memory you are going to use and how memory will be installed in the system. If at some point we request more memory than what the system can provide, voila! we will get a NULL pointer. Even if the language shields us from that, the runtime will have to provide a way for us to get the memory, so the runtime will have to come up with some memory for us to use. Any ideas?
Now, continue our assumption that the language does not let us dereference pointers, which seems to be the cause of a lot troubles when dealing with dangling pointers. So we don’t get access to the memory addresses, what could possibly go wrong? Unfortunately, the language can provide everything it can or it can be designed with all the security measures available (I can only imagine how much time a data read will take in such a system, but again if it is required and that is the only way I would use it), but the language and the runtime are still running in real hardware. And real hardware fails, and sometimes the failures are very hard to detect. For instance a bad wired connection in a board, or a memory bank that is not keeping the data for long enough. Damn, it can be even easier than that, it can be that the refresh periods for the memory were wrongly calculated so the memory is changing one or two bits randomly from time to time. It is just a matter of time until the perfectly designed language/runtime begins to fail in weird ways. So even in that particular situation you can still get a pointer that is pointing to the dark side of the moon, and with a language that has all the security measures you might even detect that, what can you do about it? Since the system cannot be trusted, the only sensible thing to do is to shut it down to avoid any major complications.
So, in summary: Even the most perfectly design system can have failures because reality is not perfect. In the real world, applications crash and sometimes that is the best we can do. Sometimes we can make sure they do not crash and we can handle the situation in a nice way, if that is possible I would strongly advice that route.
Sorry if the post was too long, but I started writing and I realized that there was no way to describe this in less words.
Well, that was long. But I still think you have no idea what you are talking about. It seems to me you are mixing together unrelated concepts or are writing false statements.
First, dangling pointers and NULL pointers are completely unrelated (orthogonal) concepts. This is best exemplified by the fact that if the language allows pointer arithmetic then a dangling pointer can be identical to NULL pointer. More precisely, in an unsafe language with pointer arithmetic (i.e: C++) the set “dangling pointers” contains the NULL pointer. What this means in practice is that when a program crashes, and in the debugger you see that the last thing it did was to access memory at address 0×123456, then it is impossible to tell (without having further information) whether the value 0×123456 is a dangling pointer or has its origin in a *computation* with a NULL pointer.
You wrote: “A NULL pointer is only gotten through variable assignment or via a failed memory assignment.” You forgot about pointer arithmetic (i.e: when the NULL pointer is the result of a computation).
You wrote: “What can one do against dangling pointers? Well…. very little”. Actually, crashes due to dangling pointers can be eliminated altogether by having a garbage collector and doing bound checking on arrays. Java for example did just that in 1995, and Lisp even before that in 1960s. Of course, that does not imply you don’t have to do any memory management anymore, it only means that the program will not crash because of accessing memory through a pointer to a no longer valid memory location.
You wrote: “Now, continue our assumption that the language does not let us dereference pointers”. What in the world are you talking about? I never mentioned nothing about forbidding to dereference pointers – what I wrote was that a good language should forbid the programmer to dereference the NULL pointer.
You wrote: “For instance a bad wired connection in a board…”. Oh my, oh my, here it comes. The “random bit flip” argument! All I have to say about this is the following: unless you can show me an actual computing machine that does *not* suffer from the “random bit flip” problem, I am going to treat this as a non-argument.
You wrote: “int *p = NULL; and voila! p is a NULL pointer”. I don’t understand what is so magical about it. I suppose you don’t understand what the type “int*” means in C++. If you think that the mathematical meaning of “int*” in C++ is “pointer to an int”, then you are totally mistaken. The precise mathematical description is a bit more complex. That wouldn’t be the major problem here – the major problem is that YOU subconsciously chose to ignore this.
Software engineers need to evolve, grow some legs and stand upright and walk into the brave new world of solving real problems.
Channel your obivous brain power into something worthwhile and leave NULL behind.
Being a **pragmatic** senior developer, I find this Apology of the NULL pointer excellent
I agree with ‘me’s post. Stop doing puzzles and solve worthwhile problems.
To Mr “x”:
A language doesn’t need to allow pointer manipulation to get dangling pointers. If you didn’t notice that was what cduclos tried to explain. “Safe” languages just hides most of this behind smoke an mirrors (and it seems some people get fooled).
You also wrote: “what I wrote was that a good language should forbid the programmer to dereference the NULL pointer”
And how do you suppose a “good language” knows whether a pointer is a NULL pointer or not?!
And the “int *p = NULL” saga: Being a nitpick is a very useless and unproductive talent.
Aha, no way that NULL pointer is not welcome !
Its a best thing that can happen while debugging a code. It is an indicator that code is not working properly and such indicators are always welcome
Comments on this entry are closed.