1

So I am trying to use the following code to add some memory to the heap without using malloc (size is a unsigned int parameter in the function, and is not a set number)

void * temp = sbrk(sizeof(void*)+sizeof(unsigned int)+size);

Now I want to set the value of the void * in temp to be NULL, however when I try to do

*(void *)temp = NULL;

my compiler tells me that I cannot dereference a void *. How do I solve this error?

mrswmmr
  • 2,081
  • 5
  • 21
  • 23

2 Answers2

2

You have declared temp as a void*, not a void**.

If it were declared as a void** then *temp = NULL would work.

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
2

If you want to change the value of temp, use temp=NULL.

If you want to put NULL in the address that temp points to, use *(void**)temp=NULL.

asaelr
  • 5,438
  • 1
  • 16
  • 22