Solved

Fundamental Question:Error Handling

Posted on 2004-04-08
9
238 Views
Last Modified: 2010-04-15
I have a complex, nested struct. Under certain conditions,
some pointers in that struct may become bad, and
memory access error occurs.

The name of the struct is pSeg, and now I just do
if ( ! pSeg ) return;
but that doesn't protect me from, let's say, pSeg->Line[0] being inaccessible.

How do I verify the struct and its members before trying
to access it?
0
Comment
Question by:jd9288
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10783118
The only way u can verify the struct is by accessing it.

What u can do is replace the malloc fn. by a wrapper say mymalloc()
Then in this mymalloc() u can set flags according to whether the struct has been allocated or not.

Then,using thses flags,u can verify the struct before accessing it.

0
 
LVL 11

Assisted Solution

by:griessh
griessh earned 50 total points
ID: 10783134
Hi jd9288,

>> some pointers in that struct may become bad
Before you use these pointers you have to test them for validity.
Or
Make sure when tey 'become bad' they are set to NULL so you can test like

if (ptr) {
  do something;
else
  return error;
}

Now you only have to define what 'become bad' really means :-)

======
Werner
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10783135
U can set these flags when the pointers are allocated.
The flags would be part of the struct itself so that each struct object would have its own flag.

U can have one variable and use bitwise operations to have 1 bit for 1 pointer rather than having 1 variable for 1 pointer.
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:jd9288
ID: 10783280
A little more on how the struct is created:

I get that struct from a DLL, it's being populated by reading and parsing several
text files. If errors occur during that parsing, some struct members and pointers are
never initialized. There are no flags to indicate that a specific part of the struct is not valid,
and I have no control over the process.
0
 
LVL 9

Assisted Solution

by:ankuratvb
ankuratvb earned 100 total points
ID: 10783351
Can u put in a function of ur own that sets the struct values before the parsing starts.

If that can be done,u can initialize the pointers to NULL and the members to some specific value which can be checked to show that this member is uninitialized after the parsing process.
0
 
LVL 12

Accepted Solution

by:
stefan73 earned 100 total points
ID: 10783368
Hi jd9288,
> If errors occur during that parsing, some struct members and pointers are
> never initialized. There are no flags to indicate that a specific part
> of the struct is not valid,
> and I have no control over the process.

Shoot the guy who wrote a DLL with such crappy error handling :-/
Bad luck.


Cheers,
Stefan
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10783397
Assuming you provide the memory which is then filled by the DLL with data, you could try initializing your memory with zeros first. Then you know which pointers have been initialized, because they're non-zero.

However, you should still shoot the guy who wrote a DLL with such crappy error handling.
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10784625
And, did shooting the guy who wrote a DLL with such crappy error handling help? :-)))
0
 

Author Comment

by:jd9288
ID: 10785197
It didn't help the program, but it sure made me feel better.
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question