[英]Derefering NULL pointer c++
I get the warning: Derefering NULL pointer 'ch' on lines 2, 4我收到警告:在第 2、4 行取消引用 NULL 指针 'ch'
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 if(ch == NULL)
是你所需要的
you dereference ch
at the code *ch
inside if
你在代码
*ch
取消引用ch
if
ch == NULL
check whether ch
is NULL
ch == NULL
检查ch
是否为NULL
*ch == NULL
check whether the item point by ch
is NULL
*ch == NULL
检查ch
所指向的项是否为NULL
For c++ (what you report using in your question) the answer is对于 C++(您在问题中报告使用的内容),答案是
char *ch = new char[size];
followed by a delete[] ch
at some point.在某个时候跟一个
delete[] ch
。
You then don't need to check if the result is null as it will throw if it failed.然后您不需要检查结果是否为空,因为如果失败就会抛出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.