I am coding for re-implementing malloc
function. I saw a man's example code , which has some strange code like this
struct s_block{
size_t size; // size per block
int free; // whether free flag exist
t_block pre;
t_block next;
void *magic_ptr;
int padding; // bytes for padding
char data[1]; // first byte of data, i.e. the address returned from malloc
};
typedef struct s_block *t_block;
t_block get_block(void *p) {
char *tmp;
tmp = p;
...
}
but I use gcc or g++ to compile this code, the error is "can't use void* pointer to char* pointer". I want to know where the question arise? gcc or g++ ? code is error?
You have to use explicit cast to cast
void*
to any other pointer whereas other way is implicit.One thing to note here is your initial pointer ( which currently is represented by void* ) should be of type char* to avoid any issues.