Re: Release version generated unexpected results
Devang wrote:
It works fine with debug version ,but in release version "dwValueRet"
gets some junk value.
and that causes my program to crash.
Can anyone help regarding this problem.
bool DeSerializeULONG (CHAR_I8 *ppcBuffer , LONG_UI32 &dwValueRet )
{
dwValueRet = ntohl (*(DWORD *)ppcBuffer);
return (TRUE);
}
This code is broken and bad in several ways:
1. The buffer must be suitably aligned, which isn't guaranteed as it is. Use
memcpy() instead of casting. This is likely the cause of your problems.
2. Don't use C style casts, they only serve to hide errors.
3. Make the input buffer constant unless you modify it (basic C++ 101 const
correctness!).
4. A long is not guaranteed to be 32 bit, just call the whole thing
DeserializeUINT32 and use a UINT32 as parameter so it is explicit what you
mean.
5. 'return' is not a function, no brackets necessary.
6. 'TRUE' is an 'int', the returntype is a 'bool'. Use 'true' instead.
Generally, since your function doesn't return any meaningful information but
just the constant 'true', you could also change the returntype to void. If
you lateron find that you need to report an error (like e.g. a buffer
overrun, which isn't checked yet), you can still use an exception, which
makes _much_ cleaner code. You could also make this function a template and
specialise it for the types you need or create a set of overloaded
functions. Both would make code maintenance and extension simpler and
cleaner.
Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite
Sator Laser GmbH
Gesch??ftsf??hrer: Thorsten F??cking, Amtsgericht Hamburg HR B62 932