Download CV
← Back to All Blogs
Is NULL Nothing? 🤔
Is NULL Nothing? 🤔

Is NULL Nothing? 🤔

5/23/202676 Reads

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:

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

c
# define NULL ((void*)0)

in some cases

c
# 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

normally if we do

c
(int)4.5

this will convert 4.5 to 4 as int value

but when we try to convert to a pointer value

c
((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:

c
int main(int argc, char *argv[]) {  int *ptr = (void *)0;  printf("%d\n", *ptr);  return 0;}

in most OS, you will get an error:

bash
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

go
// 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

go
var p *int = nil

and try to access it

go
fmt.Println(*p)

you will get an err

bash
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

go
	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

go
	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

js
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

js
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 ->

So, null isn't an universal thing.. It depends on the language, runtime, OS, CPU architecture, and memory model.

Thanks for reading.. ❤️

Share this post