3 ms·
in C NULL is just 0. a nullptr in c++ is just a pointer which points to 0. so it's not an undefined value... it's set to 0 on purpose so you can check it. cons
by vectorEQ 8y ago
in C NULL is just 0. a nullptr in c++ is just a pointer which points to 0. so it's not an undefined value... it's set to 0 on purpose so you can check it.
consider this:
char ptr;
ptr = (char )0xb8000;
before assigning ptr, ptr can be ANY value from 'random memory'. (compiler trickery aside.. because it might initialise it to 0 anyway...)
so you want to have:
char ptr = NULL;
ptr = (char )0xb8000;
So you can then do IF(ptr != NULL) { do_stuff(); }
you could not check for validity of the ptr value or it being present otherwise. an if(ptr) or if(!ptr) would only work if it's initialised and reset to NULL each time before assignment, so you can validate the assignment.
This is not mistake but a tool.
for a hardcoded offset like this it might be fair to say you could do if(ptr == 0xb8000) {do_stuff()} but what if it was a ptr returned by a new allocation or so? Or by taking the address of another variable or object? In that case setting things to null and checking them is absolutely essential to assuring your code works like you intended it to.
this whole article seems just a bunch of nonsense. for some languages it might hold true, but i can't beleive it would do for any. perhaps this original algol null... who knows.
- pjc50 8y agoProper typesafe systems wouldn't let you use C-style reinterpretation casts either. It's quite instructive to see how the low-level Rust people handle this.
- AnimalMuppet 8y agoSo if you have memory-mapped IO, how would you write to a specific address? In C/C++, a reinterpret cast is exactly what you need there. What would you use in Rust?
- pjc50 8y agoThere are various alternatives, but generally the approach is to know what address you need in advance and create an object for it at compile time that can be accessed type-safely. e.g. https://zinc.rs/apidocs/ioreg/index.html https://zinc.rs/apidocs/ioreg/index.html or the longer but more detailed http://blog.japaric.io/brave-new-io/ http://blog.japaric.io/brave-new-io/ , which covers various approaches. It even points out that you can use the type system to enforce that peripherals are only accessed from multiple threads or parts of the program in a safe manner, which you can't do if you can just reinterpret-cast into anything.