Is NULL Nothing? 🤔

while talking about NULL there is nothing better than C to understand it
in C null is just a macro nothing else
what is macro?
macro is a preprocessor text substitution
and preprocessor is a program that runs before our program passed to the compiler
example macro in C:
# define PI 3.1428
in the statement PI is called macro template and 3.1428 is the macro expansion
in C null is typically defined as
# define NULL ((void*)0)
in some cases
# define NULL 0
NULL expands to 0 (or equivalent),
which the compiler converts into a null pointer value.
when we assign NULL to something C’s preprocessor converts that NULL to 0 before compilation
for compiler null is just 0 c-faq
normally if we do
(int)4.5
this will convert 4.5 to 4 as int value
but when we try to convert to a pointer value
((void*)0)
(void*) tells the compiler convert/interpret 0 as a pointer type
converting 0 as pointer type produces a special pointer value
if you do:
int main(int argc, char *argv[]) {
int *ptr = (void *)0;
printf("%d\n", *ptr);
return 0;
}
in most OS, you will get an error:
segmentation fault
A segmentation fault occurs at runtime when a program tries to access or modify memory that it is not allowed to use.
NIL in Golang
in Golang we get nil
nil isn’t literally 0 in golang
nil is predeclared identifier in go
that means we can use it anywhere in our code without declaring it
nil represent zero values of many type
if we look at the source code we will see
// nil is a predeclared identifier representing the zero value for a
// pointer, channel, func, interface, map, or slice type.
var nil Type // Type must be a pointer, channel, func, interface, map, or slice type
Type must be one of pointer, channel, func, interface, map, or slice type
you can think of it conceptually:
- no pointer
- no channel
- no function
- etc…
if you do
var p *int = nil
and try to access it
fmt.Println(*p)
you will get an err
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x1005f949c]
you can see the address is 0x0 similar to C
but as C you can’t access it from your program
but if you do
var a []int = nil
fmt.Println(a)
it will print an empty slice.. here is nothing related 0 address
also you can append, remove value from the slice
a = append(a, 1)
fmt.Println(a)
so nil in Go behaves differently depending on the type using it
Let’s see how javascript sees NULL 👀
Let’s start with a javascript bug 😂
if you execute
typeof null
you will get “object” as result
early javascript engines stored values in memory using small type tag system
The type tag was 0 of object and null was represented as NULL pointer (0x00 in most cases) so null got 0 type tag, hence the typeof null returns “object” as result
unlike Go’s nil null isn’t an identifier, null in javascript a syntax keyword and primitive value
if we do
let user = null;
that means the user exists, but currently no object value is assigned to it.
look at here for more details about null and undefined -> mdn-null
So, null isn’t an universal thing.. It depends on the language, runtime, OS, CPU architecture, and memory model.
Thanks for reading.. ❤️