本文介绍了访问链接列表中的NULL指针时发生异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在开发一种工具,可以从文件中读取数据并将其存储在链接列表中.我访问链接列表,然后根据某些条件将数据写入另一个文件.

此操作所需的基本结构为:

Hello all,

I am developing a tool to read the data from a file and store in a linked list. I access the linked list and write the data into another file depending on some conditions.

The base structures needed for this operation are:

// Structure to store the data of an Element.
typedef struct ElemNode
{
    CString     szID;       // Stores the Element ID.
    WORD        wLength;    // Stores the Length of the 'Value' field.
    ValueType   eType;      // Stores the type of the 'Value' field.
    CString     szValue;    // Stores the Value of the Element.
    ElemNode    *pNextElem; // Pointer to the Next Element.
}ElemNode;

// Structure to store the data of an Item.
typedef struct ItemNode
{
    CString     szID;           // Stores the Element ID.
    WORD        wLength;        // Stores the Length of the 'Value' field.
    ValueType   eType;          // Stores the type of the 'Value' field.
    CString     szValue;        // Stores the Value of the Element.
    ElemNode    *pElemList;     // Pointer to the Element List of the particular Item.
    ItemNode    *pNextItem;     // Pointer to the Next Item.
}ItemNode;



读取数据后的链表结构类似于:

ItemNode-> ElemNode-> ElemNode-> ElemNode -> NULL
|
ItemNode-> NULL
|
ItemNode-> ElemNode-> ElemNode-> ElemNode ...(包含13个ElemNodes)
|
ItemNode-> ElemNode-> ...(包含16个ElemNode).
|
NULL

我需要在第一个ItemNode结构中访问第三个ElemNode的szValue字段(以粗体显示).我直接使用以下语句.



The Linked list structure after reading the data is something like :

ItemNode -> ElemNode -> ElemNode -> ElemNode -> NULL
|
ItemNode -> NULL
|
ItemNode -> ElemNode -> ElemNode -> ElemNode ... (conatins 13 ElemNodes)
|
ItemNode -> ElemNode -> ... (contains 16 ElemNodes).
|
NULL

I need to access the szValue field of the 3rd ElemNode (shown in bold)in 1st ItemNode structure. I directly use the following statement.

ItemNode *pFirstItem;  // This points to the First ItemNode.
CString strTempString = pFirstItem->pElemList->pNextElem->pNextElem->szValue



如果缺少ElemNode之一,则此语句将导致应用程序崩溃.我知道使用UserException并将其抛出.但是问题是,我不能放自己的代码来检查NULL情况并抛出UserException.

有什么方法可以通过引发异常来避免崩溃?换句话说,在这种情况下系统抛出的异常是什么?

我正在使用MFC并在Visual Studio 2005中开发此工具.
如果需要任何与项目相关的设置,请告诉我.



If one of the ElemNode is missing, then this statement will cause the app to crash. I know of using UserException and throwing them. But the problem is, I can''t go putting my own code to check for the NULL cases and throw the UserException.

Is there any way I can avoid the crash by throwing an exception? In other words, what is the exception thrown by the system in such cases?

I am using MFC and developing this tool in Visual-Studio 2005.
If there is any project related settings that is needed, please tell me.

推荐答案




    • use the __try/__except construct (see http://msdn.microsoft.com/en-us/library/s58ftw19(VS.80).aspx[^])
  • use SEH (Structured Exception Handling) through AddVectoredExceptionHandler and related API (see http://msdn.microsoft.com/en-us/library/ms679274(VS.85).aspx[^])


这篇关于访问链接列表中的NULL指针时发生异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 01:24