【发布时间】:2021-03-21 16:50:35
【问题描述】:
这是来自 facebook infer 的错误报告。
error: NULL_DEREFERENCE
pointer `stack` last assigned on line 24 could be null and is dereferenced at line 25, column 5.
22. struct string_stack* create_String_Stack(unsigned capacity)
23. {
24. struct char_stack* stack = calloc(1,sizeof(struct char_stack));
25. > stack-> capacity = capacity;
26. stack->top = -1;
27. stack->array = (char*)malloc(stack->capacity * sizeof(char));
struct char_stack
{
int top;
unsigned capacity;
char* array;
};
如何摆脱这个警告?
【问题讨论】:
-
查看calloc的结果?
if(stack == NULL) { /* handle error & terminate */ }. -
警告是 100% 正确的。
calloc可以返回NULL而你不检查这个。
标签: c pointers null dereference