gpt4 book ai didi

c - VS2019 C6011 错误取消引用空指针 'NewNode'

转载 作者:行者123 更新时间:2023-12-04 01:39:18 26 4
gpt4 key购买 nike

你好?我正在学习双向链表

反正我以前在node上放数据的时候出现C6011错误
[:取消引用空指针'NewNode']

所以我引用了https://docs.microsoft.com/ko-kr/visualstudio/code-quality/c6011?view=vs-2019

试图检查空值。但它没有用..

我检查了错误窗口的一些细节,如我添加的图片

他们说“NewNode 将为 NULL”,“即使取消引用,NewNode 仍将是 NULL”

enter image description here

代码如下

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
#endif

2.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://docs.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;
}

谢谢!

最佳答案

这是来自静态分析器的警告。编译器说可能有一些潜在的错误。

事实上,在 DLL_CreateNode 中有一个对 malloc 的调用可能会失败并返回一个 NULL。你在函数内部检查它,这很好。

但是如果 malloc 失败,你仍然会在 main.c 中向调用者返回一个 NULL。

NewNode = DLL_CreateNode(i);   //<-- this could return NULL
DLL_AppendNode(&List, NewNode);

在这种情况下,此 NULL 将作为第二个参数提供给 DLL_AppendNode。

如果此时有一个现有的头节点,则在 DLL_AppendNode 中的这一行:
NewNode->Prev = Tail;

它会给出一个 EXC_BAD_ACCESS 异常。

静态分析器因此正确地识别了潜在问题。

有几种方法可以处理它。一种解决方案是检查 NULL,在 stderr 上打印错误消息,然后以错误代码退出程序。

关于c - VS2019 C6011 错误取消引用空指针 'NewNode',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58152437/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com