Solved

Reliable class design

Posted on 2000-02-27
6
183 Views
Last Modified: 2013-12-03
Hi dudes,

I'm currently on the learning curve of C++. However, many years of regular C programming leave me with the notion that I may be up to some bad habits... I wonder if you could provide any suggestions...

I'm currently working on a simple hex dump class: It works, but is there a smarter, more bulletproof way of doing it? The way I've designed it is that it takes a constant pointer to a constant byte:

myDump.LookAt((reinterpret_cast<const char * const>(pBuffer)));

Pretty ugly, to say the least. Is it possible to, say, use void pointers to make the LookAt member function accept any type of pointer without the foul casting and no subsequent whining from the compiler?

In addition, the class has these two member functions:

void DumpClass::OutputHex(unsigned char *pszOutput, int nBytesToGet);
void DumpClass::OutputASCII(unsigned char *pszOutput, int nBytesToGet);

These take a pointer to a string and output the corresponding hex dump data to that string, eg.

const int nBytesToGet=16;
char outputbuff[64];
DumpClass myDump;
myDump.LookAt(pSomeLoc);
myDump.OutputHex(outputbuff, nBytesToGet);

It troubles me that it is up to the programmer to make sure that the output buffer is big enough to hold the dump output... What would be a better way?

Since this is really 2 questions, I've doubled the points...

Cheers
0
Comment
Question by:baldrick
  • 3
  • 3
6 Comments
 
LVL 3

Author Comment

by:baldrick
ID: 2563426
Adjusted points to 200
0
 
LVL 22

Accepted Solution

by:
nietod earned 200 total points
ID: 2563460
>> Pretty ugly, to say the least.
Well, there''s not to much that you can do about this.  You are--intentionally--breaking the rules by treating non-character-array data types as character arrays and that is goign to be messy and always a bit risky too.  But that is pretty much unavoidable.  You could use a C-style cast instead of the reinterpret_cast<>.  There is no difference between these except that the reinterpret_cast is uglier and that is intentional.  it is hard to miss and that was the goal.

>> it troubles me that it is up to the programmer
>> to make sure that the output buffer is big
>> enough to hold the dump
>> output... What would be a better way?
Good.  It should trouble you.  You shoudl be prepared to say goodbye to that sort of code and mor importantly to "char *" strings too!  Switch to using a string class, like the STL string.  (Or write your own string class, it is a great learnign experience.)

Then make this procedure return a string.  The string will be as long as is needed to represent the data.  1 character or 10,000 characters.  Much easier for the class's client to use your class that way!  And if the string is reference counted, it is still very efficient to return in this manner.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2563468
An example of this would be the STL's global getline() function this reads a line from a file and returns it in a string.  The string will be as long as is needed to return the line (assuming you don't run out of memory).  I might be 0 characters, 1 character, or 10,000 characters.

In your case you would do something like

string DumpClass::OutputHex(int nBytesToGet = 16)
{
  string S;
  const char *CnvAry = "0123456789ABCDEF";
  while (nBytesToGet--)
  {
     unsigned char Byt = *SomePtr++

     if (S.length())  // If this is not the first character.
        S += ' '; // Add a space after previous char.
     S +=  CnvAry[(Byt >> 4) & 0x0F];
     S+= CnvAry[Byt  & 0x0F];
  }
  return S;
}

Let me know if you have any questions.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 3

Author Comment

by:baldrick
ID: 2563669
Thank you for your prompt answer, which has certainly answered my question (I was afraid you'd recommend using the STL, something I've been a bit lax in learning about).

On the first point about getting rid of the casting: I changed the LookAt parameter type to a constant void pointer and the client no longer needs the cast. Is this a safe thing to do?
0
 
LVL 22

Expert Comment

by:nietod
ID: 2564603
Honestly, I wouldn't recommend rushing into the STL. If you are just starting out try doing some of it yourself.  You will learn more and you will also develope an appreciation for the STL if you decide to use it.  (I personally don't use it, except to answer questions here.)  I do recommend you learn ABOUT it at sometime, but not necessarily at the start.  I can't begin to say how important it is to have a good string class and possibly other container classes too, it has so many advantages it would take another 200 points for me to list them, but it doesn't have to be the STL ones.

>> Is this a safe thing to do?
It is not any less safe than what you had.   But basically what you are doing is intrinsically unsafe and may cause problems at times--though you really have no choice.

For example, If you are on a protected mode OS, then this design may be used to try access protected memory or invalid memory addresses and this will result in a crash.  Like if you allocate an object that is 16 bytes in length and try to display it for a length of 32 bytes you don't know what is in the 16 bytes after the object, it may be a protected memory segment and the OS will termiante your app.   Another example might be using this to display the contents of an object that has a virtual base class.  Such an object may not be stored in contiquous bytes in memory (i.e. it may be stored in seperate memory blocks with spaces between tham) so this could be displaying garbage (or again protected memory) when it tries to display parts of the object.
0
 
LVL 3

Author Comment

by:baldrick
ID: 2565883

Once again, thanks for the great advice and valuable insights.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IdTCPClient1->Disconnect(); not working 3 67
sorting efficency of sorting algorithm 30 113
Issues with C++ Class 19 92
Can I print examples of all of my installed fonts? 6 22
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

823 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