void * alligned_malloc(size_t bytes , uint16_t allign)
{
uint16_t offset = allign - 1 + sizeof(void*);
void* p1 = malloc(offset + bytes);
void**p2 = (void**)(((size_t)p1+offset) & ~(allign - 1));
p2[-1] = p1;
return p2; // Why is this correct ? should not the return be p2[0] as it is returning void**
}
Please help me understand this modified malloc how is void** equivalent to void * return in this code ?
A void function cannot return any values. But we can use the return statement. It indicates that the function is terminated. It increases the readability of code.
A void return type simply means nothing is returned. System. out. println does not return anything as it simply prints out the string passed to it as a parameter.
When used as a function return type, the void keyword specifies that the function doesn't return a value. When used for a function's parameter list, void specifies that the function takes no parameters. When used in the declaration of a pointer, void specifies that the pointer is "universal."
It points to some data location in the storage. This means that it points to the address of variables. It is also called the general purpose pointer. In C, malloc() and calloc() functions return void * or generic pointers.
Any kind of pointer can be implicitly converted to a void *
, including a pointer to a void *
, aka void **
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With