你好?我在研究双链接列表
无论如何,当我过去在节点上放置数据时,出现了C6011错误:删除空指针'NewNode‘
所以我引用了https://learn.microsoft.com/ko-kr/visualstudio/code-quality/c6011?view=vs-2019和
尝试检查空值。但没成功..。
我检查了错误窗口的一些细节,比如我添加的图片。
他们说'NewNode将为空‘,'NewNode将仍然为空,即使它被取消引用’。

代码如下所示
1.头文件(DoubleLinkedList.h)
#pragma once
#ifndef DOUBLY_LINKEDLIST_H
#define DOUBLY_LINKEDLIST_H
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef int ElementType;
typedef struct MyNode
{
ElementType Data;
struct MyNode* Prev;
struct MyNode* Next;
}Node;
Node* DLL_CreateNode(ElementType NewData); //1.create node
void DLL_DestroyNode(Node* Node);//2.destroy node
void DLL_AppendNode(Node** Head, Node* NewNode);//3.append node
/*
4.insert node
connect
prev-insert-next
*/
void DLL_InsertAfter(Node* Current, Node* NewNode);
void DLL_RemoveNode(Node** Head, Node* Remove);//5.remove node
Node* DLL_GetNodeAt(Node* Head, int location);//6.search node
int DLL_GetNodeCount(Node* Head);//7.count the number of nodes
#endif2.DLL.cpp(用于头文件)
#include "DoubleLinkedList.h"
Node* DLL_CreateNode(ElementType NewData)
{
//create node with memory allocation
Node* NewNode = (Node*)malloc(sizeof(Node));
//nullptr check for c6011(https://learn.microsoft.com/ko-kr/visualstudio/code-quality/c6011?)
if (NewNode)
{
NewNode->Data = NewData;
NewNode->Prev = NULL;
NewNode->Next = NULL;
}
return NewNode;
}
void DLL_DestroyNode(Node* Node)
{
free(Node);
}
void DLL_AppendNode(Node** Head, Node* NewNode)
{
if ((*Head) == NULL)
{
*Head = NewNode;
}
else
{
Node* Tail = (*Head);
while (Tail->Next != NULL)
{
Tail = Tail->Next;
}
Tail->Next = NewNode;
NewNode->Prev = Tail;
}
}
void DLL_InsertAfter(Node* Current, Node* NewNode)
{
NewNode->Prev = Current->Next;
NewNode->Next = Current;
if (Current->Next != NULL)
{
Current->Next->Prev = NewNode;
Current->Next = NewNode;
}
}
void DLL_RemoveNode(Node** Head, Node* Remove)
{
if (*Head == Remove)
{
*Head = Remove->Next;
if (*Head != NULL)
{
(*Head)->Prev = NULL;
}
Remove->Next = NULL;
Remove->Prev = NULL;
}
else
{
Node* Temp = Remove;
if (Remove->Prev != NULL)
{
Remove->Prev->Next = Temp->Next;
}
if (Remove->Next != NULL)
{
Remove->Next->Prev = Temp->Prev;
}
Remove->Prev = NULL;
Remove->Next = NULL;
}
}
Node* DLL_GetNodeAt(Node* Head, int location)
{
Node* Current = Head;
while (Current != NULL && (--location) >= 0)
{
//--location >=0 is for check index
Current = Current->Next;
}
return Current;
}
int DLL_GetNodeCount(Node* Head)
{
int count = 0;
Node* Current = Head;
while (Current != NULL)
{
Current = Current->Next;
count++;
}
return count;
}3.实习档案(.c)
#include "DoubleLinkedList.h"
int main()
{
int i = 0;
int Count = 0;
Node* List = NULL;
Node* Current = NULL;
Node* NewNode = NULL;
//create&append nodes
for (i = 0; i < 5; i++) {
NewNode = DLL_CreateNode(i);
DLL_AppendNode(&List, NewNode);
}
Count = DLL_GetNodeCount(List);
for (i = 0; i < Count; i++) {
Current = DLL_GetNodeAt(List, i);
printf("List[%d] : %d\n", i, Current->Data);
}
Current = DLL_GetNodeAt(List, 2);
NewNode = DLL_CreateNode(3000);
DLL_InsertAfter(Current, NewNode);
Count = DLL_GetNodeCount(List);
for (i = 0; i < Count; i++) {
Current = DLL_GetNodeAt(List, i);
printf("List[%d] : %d\n", i, Current->Data);
}
for (i = 0; i < Count; i++) {
Current = DLL_GetNodeAt(List, 0);
if (Current != NULL) {
DLL_RemoveNode(&List, Current);
DLL_DestroyNode(Current);
}
}
return 0;
}谢谢!
发布于 2019-09-29 05:57:05
这是静态分析器发出的警告。编译器说可能有一些潜在的错误。
实际上,在DLL_CreateNode中有一个对malloc的调用,它可能会失败并返回NULL。在函数中检查它,这很好。
但是,如果malloc失败,您仍然将向main.c中的调用方返回一个NULL。
NewNode = DLL_CreateNode(i); //<-- this could return NULL
DLL_AppendNode(&List, NewNode);在这种情况下,这个NULL将作为第二个参数提供给DLL_AppendNode。
如果此时存在一个现有的head节点,那么在DLL_AppendNode中这一行:
NewNode->Prev = Tail;它将提供一个EXC_BAD_ACCESS异常。
因此,静态分析器正确地发现了一个潜在的问题。
有几种方法可以解决这个问题。一种解决方案是检查NULL,在stderr上打印错误消息,并使用错误代码退出程序。
https://stackoverflow.com/questions/58152437
复制相似问题