-
July 21st, 2008, 07:28 AM
#1
Trouble in serializing/deserializing int.
Hello Gurus,
I am facing trouble in serializing and deserializing int.
Code:
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.
Dont forget to rate my post if you find it useful.
-
July 21st, 2008, 07:46 AM
#2
Re: Trouble in serializing/deserializing int.
I think you just put an extra "&" in the prototype for DeserializeInt:
Code:
int DeserializeInt(int_8*& m_buff)
That's why, you're using the pointer to the integer instead of the value of the integer!
-
July 21st, 2008, 07:56 AM
#3
Re: Trouble in serializing/deserializing int.
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..
Dont forget to rate my post if you find it useful.
-
July 21st, 2008, 07:59 AM
#4
Re: Trouble in serializing/deserializing int.
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.
-
July 21st, 2008, 08:17 AM
#5
Re: Trouble in serializing/deserializing int.
yup you are right this is hieght of stupidity on my part..
Dont forget to rate my post if you find it useful.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|