CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2007
    Location
    Bangalore India
    Posts
    262

    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.

  2. #2
    Join Date
    Aug 2004
    Location
    Bucharest, Romania... sometimes
    Posts
    1,039

    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!
    Bogdan Apostol
    ESRI Developer Network

    Compilers demystified - Function pointers in Visual Basic 6.0
    Enables the use of function pointers in VB6 and shows how to embed native code in a VB application.

    Customize your R2H
    The unofficial board dedicated to ASUS R2H UMPC owners.

  3. #3
    Join Date
    May 2007
    Location
    Bangalore India
    Posts
    262

    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.

  4. #4
    Join Date
    May 2002
    Posts
    1,435

    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.

  5. #5
    Join Date
    May 2007
    Location
    Bangalore India
    Posts
    262

    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
  •  





Click Here to Expand Forum to Full Width

Featured