問題

你好?我正在研究Doubly連結列表

無論如何,當我以前將資料放在節點上時,有C6011錯誤 [:取消引用Null Pointer`NewNode']

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

試圖檢查null值.但它不起作用..

我在錯誤視窗上檢查了一些細節,如我新增的圖片

他們說’NewNode將為NULL’,’NewNode將仍然為NULL,即使它是dereferenced’

2001年12月31日終了的兩年期收入和支出及準備金和基金結餘變動報表 enter image description here

程式碼如下

1.header檔案(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
", 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
", 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上列印錯誤訊息,並使用錯誤程式碼退出程式.

  相同標籤的其他問題

cnullptr