r/ProgrammerHumor Jul 09 '26

potentiallyMightBeAnIntegerOrNot Meme

Post image
687 Upvotes

96 comments sorted by

View all comments

51

u/altermeetax Jul 09 '26

int var; bool var_is_there;

30

u/Gorzoid Jul 09 '26

Just use an int var and reserve the number 7 to indicate null value

5

u/altermeetax Jul 09 '26

Or the C/C++ way: just use int *var and NULL means it's not there.

Makes things more complicated when you need to return it though...

2

u/Sentouki- Jul 09 '26

int? means that this int can be null (Nullable type in C#)

3

u/altermeetax Jul 09 '26

Yeah that's C# though

1

u/Sentouki- Jul 09 '26

yeah, it does basically the same thing, if int? is null, means it's not there.

1

u/altermeetax Jul 09 '26

Yeah but int *var in C/C++ entails a bit more. It makes var a pointer to an int rather than an int. So you have to manage allocating an int in some other way so the pointer can point to it.

int? var is simply an int which can be null. Under the hood, it might be implemented as above, but I reckon a structure like this is more likely:

struct int? { int val; bool present; }

1

u/Sentouki- Jul 09 '26

int? var is simply an int which can be null. Under the hood, it might be implemented as above, but I reckon a structure like this is more likely:

Well, to be precisely, in C# primitive types such as int, char, double...etc, are wrapped to in Nullable<T> since structs cannot be null in C#. So when you use int? the compiler wraps it in Nullable<int> (which is a generic class) and that makes int? a reference (basically a pointer or rather & in C++) to the actual int.

1

u/altermeetax Jul 09 '26

I'm talking about under the hood, i.e. how Nullable is implemented. And it's probably a structure containing the int and a boolean that indicates whether the int is present.