[英]Solving C warning - dereferencing a null pointer
這是來自 facebook 推斷的錯誤報告。
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;
};
如何擺脫這個警告?
我相信問題出在struct char_stack* stack = calloc(1,sizeof(struct char_stack));
. 如果你只是簡單地說struct char_stack* stack= malloc(sizeof(struct char_stack);
因為你只想要 1 個項目來節省空間,那么我相信它可能會解決它。如果沒有,那么我建議檢查你是否正確發音sizeof(struct char_stack)。最后,你總是必須檢查if (stack==NULL)
因為程序可能找不到空間來為指針分配空間。另外,我建議使用typedef struct char_stack Char_stack;
所以你不要不需要一直寫struct char_stack
而只Char_stack
。我希望這能幫助你找到問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.