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){
exit(0);
}
return ch;
}
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
For c++ (what you report using in your question) the answer is
char *ch = new char[size];
followed by a delete[] ch
at some point.
You then don't need to check if the result is null as it will throw if it failed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.