访问链接列表中的NULL指针时发生异常 [英] Exception when accessing a NULL pointer in linked list

查看:109
本文介绍了访问链接列表中的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.

推荐答案

首先,除非存在,否则不要手动创建和使用自己的列表类型``这是一个很好的理由. std :: list是您的朋友.

其次,如果您要滚动自己的列表,请始终检查零指针-您真的不想弄乱操作系统异常(并且使代码无休止地减速),而这样做的速度要快得多在您的应用程序中.

最后,使用自制的变体保存不同的数据类型有点危险-您将失去C ++的主要优势之一,即强类型检查.尝试从类型管理中分离出列表功能,从长远来看,它将使您的设计更加整洁.

干杯,

Ash
First of all, don''t manually create and use your own list types unless there''s a very good reason. std::list is your friend.

Secondly if you''re rolling your own lists ALWAYS check for zero pointers - you really don''t want to be messing around with operating system exceptions (and slowing the code down no end) for something that''s far quicker to do in you application.

Finally using a home cooked variant to hold different data types is a bit dangerous - you''ll loose one of C++''s key advantages, which is strong type checking. Try separating out the list functionality from the type management, it''ll make your design cleaner in the long run.

Cheers,

Ash


取消引用NULL指针将导致 CPU 抛出通常由处理的硬件异常.操作系统,通常不是捕获这种异常的好编程风格,相反,最好的方法是在执行可能属于此类异常的代码之前测试条件.

但是,如果您想捕获这种异常,则可以通过三种方式来实现(所有都是 Microsoft特定的功能):

Dereferencing a NULL pointer will cause the CPU to throw an hardware exception that usually is handled by the operating system, and generally is not a good programming style to catch this kind of exception, instead the best is to test conditions before executing code that could fall in such an exception.

However, if you want to catch this kind of exception, you could do it in three ways (all are Microsoft specific features):



    • 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指针时发生异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆