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
Solved

Fundamental Question:Error Handling

Posted on 2004-04-08
9
239 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

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…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

856 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