Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C++
// 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;


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.
C++
ItemNode *pFirstItem;  // This points to the First ItemNode.
CString strTempString = pFirstItem->pElemList->pNextElem->pNextElem->szValue


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.
Posted
Updated 27-May-10 23:12pm
v4

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
 
Share this answer
 
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):




  1. translate Win32 exceptions to C++ exceptions using _set_se_translator and the C++ try/catch construct (see http://msdn.microsoft.com/en-us/library/5z4bw5h5(VS.80).aspx[^]); note that you must compile your code with the /EHa switch to make this method working
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900