Click to See Complete Forum and Search --> : Trouble in serializing/deserializing int.


code_carnage
July 21st, 2008, 07:28 AM
Hello Gurus,

I am facing trouble in serializing and deserializing int.


typedef char int_8;

int_8* SerializeInt(int value)
{
int_8* buff = new int_8[4];
*( buff++ ) = ( int_8 )( value >> 24 );
*( buff++ ) = ( int_8 )( value >> 16 );
*( buff++ ) = ( int_8 )( value >> 8 );
*( buff++ ) = ( int_8 )( value );
return buff;
}


int DeserializeInt(int_8*& m_buff)
{
int temp = *( m_buff++ );
temp = ( temp << 8 | *( m_buff++ ) );
temp = ( temp << 8 | *( m_buff++ ) );
temp = ( temp << 8 | *( m_buff++ ) );
return temp;
}


Problem is when I use SerializeInt( value) function and pass any value to be serialized and when I deserialized the returned value I get -3 always.

I am not able to locate the problem and debugger also is not of great help here..

Thanks in advance

Regards
Prashant.

Bornish
July 21st, 2008, 07:46 AM
I think you just put an extra "&" in the prototype for DeserializeInt:int DeserializeInt(int_8*& m_buff)That's why, you're using the pointer to the integer instead of the value of the integer!

code_carnage
July 21st, 2008, 07:56 AM
Hello Bornish,

"&" was put so that the pointer itself is updated and point to 4 places up after the function DeserializeInt returns.

And even if I remove it seems there is no improvement..

0xC0000005
July 21st, 2008, 07:59 AM
There are several problems with this code but the most serious is that you are returning the wrong pointer from SerializeInt(). You allocate an array, increment the pointer four times, and return an invalid pointer.

code_carnage
July 21st, 2008, 08:17 AM
yup you are right this is hieght of stupidity on my part..:D