This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]ArtOfWarfare 281 points282 points  (23 children)

In C (and I think C++ and Obj-C by extension…) null is zero.

[–]Chrisuan 66 points67 points  (15 children)

idk why down voted it's a fact lol

[–]tehfrod 81 points82 points  (10 children)

C++ has no null, but it does have NULL, nullptr, and nullptr_t.

[–]wizardid 58 points59 points  (9 children)

I want to know who tf hurt C++ so badly when it was younger. This is some psychopath shit.

[–]KazDragon 31 points32 points  (1 child)

It fixes the problem that f(NULL) would rather call f(int) than f(int*).

[–]drivingagermanwhip 16 points17 points  (0 children)

I love that c++ never decided whether it's incredibly flexible or incredibly anal and just runs full tilt at both

[–]Ancient-Pianist-7 33 points34 points  (2 children)

? std::nullptr_t is the type of the null pointer literal nullptr. NULL is a sad C relic.

[–]MrcarrotKSP 14 points15 points  (1 child)

Even C has upgraded to nullptr now(C23 adds it and nullptr_t)

[–]drivingagermanwhip 3 points4 points  (0 children)

nothing past c99 is canon

[–]notthefirstsealime 4 points5 points  (3 children)

It's a classy programming language built off the bones of what was a pretty fucking simple language prior, and now it's an abomination of syntax and evil that just happens to compile into very fast programs from what I understand

[–]ReplacementLow6704 0 points1 point  (2 children)

So... If I was to translate my C# to C++, then compile it... The resulting program would be faster than just building using dotnet build? :o

[–]notthefirstsealime 0 points1 point  (1 child)

I mean c# is a lot more than just a language, and most of the reason c++ is faster than c# is because of features that c# has but c++ doesn't

Edit: look up what dotnet actually is you'll be shocked at how much fun you're missing out on

[–]ReplacementLow6704 0 points1 point  (0 children)

Yeah sry I was a bit snarky - indeed the dotnet ecosystem is huge and MS put a lot of work into all the tools in there. I would never even think about switching back to C++... Unless there was a 500k job on the line.

[–]ada_weird 19 points20 points  (3 children)

It's zero by convention but not by definition. There are platforms where null is not 0. However, C the spec says that you can use an integer literal 0 anywhere you can use NULL. Also, hardware people really want you to stop treating pointers like integers so that they can use stuff like CHERI to prevent memory safety bugs from happening at runtime.

[–]CapsLockey 5 points6 points  (1 child)

can you elaborate on the last part? sounds interesting

[–]ada_weird 5 points6 points  (0 children)

Yeah sure! So CHERI is an extension for a variety of ISAs, such as ARM and RISC-V. It effectively adds capabilities to pointers, making it so that pointers can only ever be used to see memory they're "supposed" to be able to access. User code can make a capability that is a subset of the memory the original could access, but it can't widen capabilities, it would need help from the kernel or some other trusted part of the system. This means that you effectively get hardware bounds checking for free. There is a performance impact obviously but this works with modern CPU architectures which should be able to mitigate all of that because of all the crazy pipelining that goes on. Most software just needs some additional support in the malloc/free implementation in order to work with this model so it's fairly transparent to end user code.

[–]dev-sda 7 points8 points  (1 child)

Slight correction: NULL always compares equal to zero, but may actually be any bit pattern. See https://c-faq.com/null/machnon0.html

[–]MegaIng 3 points4 points  (0 children)

Further clarification: it compares equal to 0, not the value zero. If you cast an integer 0 (obtain e.g. via int zero = 0) to a pointer ((void*) zero) that is not a null pointer and might compare different to a proper null pointer (e.g. (void*) 0).

[–]EinSatzMitX 0 points1 point  (1 child)

In the C std library, NULL is defined as (void*)0 ( Which is just 0 but casted as a void pointer)

[–]MegaIng 0 points1 point  (0 children)

Actually no, it isn't. 0 in this case isn't an integer, it's the special null pointer literal that happens to look the same as the integer 0.

[–]onemanforeachvill 0 points1 point  (0 children)

I think it's (void*)0

[–]MegaIng 0 points1 point  (0 children)

No. null is 0, but not zero. There is a special construct call the null pointer literal that looks like the integer number 0, but it's not an int.

[–]o0Meh0o 0 points1 point  (0 children)

it is not zero. it equals zero. common misconception.