Fundamental Question:Error Handling

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?
jd9288Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
stefan73Connect With a Mentor Commented:
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
 
ankuratvbCommented:
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
 
griesshConnect With a Mentor Commented:
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
Live webcast with Pinal Dave

Pinal Dave will teach you tricks to help identify the real root cause of database problems rather than red herrings. Attendees will learn scripts that they can use in their environment to immediately figure out their performance Blame Shifters and fix them quickly.

 
ankuratvbCommented:
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
 
jd9288Author Commented:
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
 
ankuratvbConnect With a Mentor Commented:
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
 
stefan73Commented:
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
 
stefan73Commented:
And, did shooting the guy who wrote a DLL with such crappy error handling help? :-)))
0
 
jd9288Author Commented:
It didn't help the program, but it sure made me feel better.
0
All Courses

From novice to tech pro — start learning today.