Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

void ** equivalent to void * return?

Tags:

c++

c

    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 ?

like image 461
ashok v Avatar asked Jan 14 '14 07:01

ashok v


People also ask

What does void * function return?

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.

What is a void * return type?

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.

What is void * return type in C++?

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."

What does a void pointer return?

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.


1 Answers

Any kind of pointer can be implicitly converted to a void *, including a pointer to a void *, aka void **.

like image 196
Colin D Bennett Avatar answered Oct 12 '22 02:10

Colin D Bennett