Recently I saw following piece code:
if ((rgb = (fp16 *)malloc(width*height*sizeof (*rgb)*3)) == NULL)
rgb is declared as a pointer of some class type.
In the above code, malloc() is taking arguments which is width*height*sizeof(*rgb)
So it is some kind of self referential initialization(If i may call it by giving this name!)
i.e. belore rgb pointer is allocated by malloc, it is dereferencing it in call to malloc.
In this particular code, i saw that the pointer rgb is not initialized to NULL or anything.
What would be the behaviour of such code.
-
Normal functioning or
-
Crash due to null pointer dereference, or a
-
Garage pointer dereference
thanks,
-AD.
sizeofdoes not evaluate its operand, so in this casesizeof(*rgb)will return the size ofrgb‘s type, which I assume isfp16 *.Completely valid C code.
If it did evaluate it and the pointer was just some declared pointer with no initialized value, then you would get undefined behavior.