Note: You will see this multiple times - type* ptr = malloc(sizeof ptr) and it's worked till now because the size of the address has always been greater than the size of an instance of that type. It should be corrected as so - type* ptr = malloc(sizeof *ptr)