Ceesz Ceesz - 1 year ago 104
C++ Question

Derefering NULL pointer c++

I get the warning: Derefering NULL pointer 'ch' on lines 2, 4

I don't understand why. Can someone help me out?

char *my_alloc(size_t size) {
char *ch = (char *)malloc(size);
//FIXED: If malloc fails -> exit program
if(*ch == NULL){
return ch;

Answer Source

if(ch == NULL) is what you need

you dereference ch at the code *ch inside if

ch == NULL check whether ch is NULL

*ch == NULL check whether the item point by ch is NULL

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download