Link to home
Start Free TrialLog in
Avatar of baldrick
baldrickFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Reliable class design

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
Avatar of baldrick
baldrick
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Adjusted points to 200
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nietod
nietod

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.
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?
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.

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