Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ list Debugging
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;
 
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
 
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 27-May-10 22:23pm
Edited 28-May-10 0:12am
Moak11.4K
v4
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 406
1 Marcin Kozub 225
2 Sergey Alexandrovich Kryukov 205
3 Raul Iloc 170
4 Maciej Los 164
0 OriginalGriff 8,289
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,624
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 28 May 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100