?
Solved

Fundamental Question:Error Handling

Posted on 2004-04-08
9
Medium Priority
?
242 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 400 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 400 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses
Course of the Month11 days, 21 hours left to enroll

752 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