0

Screenshot of weird problem

I'm trying to serialize an object in C#. I got the object size and saved it in a variable, size1 on line 207 in above screenshot. Size1 has a value of 160. Then I used size1 to allocate an array of bytes called buf in line 210. Buf comes out to be a 2 byte array! How can this be?!

Thomas Nguyen
  • 464
  • 1
  • 5
  • 16
  • 2
    Try to post your code here instead of printing the image!! – Rahul Tripathi Aug 14 '13 at 18:12
  • At least what "line 210" is, is very obvious :P – Lews Therin Aug 14 '13 at 18:13
  • How do you know size1 has a value of 160? – paparazzo Aug 14 '13 at 18:14
  • 2
    The image also shows the values of local variables during runtime. As it is the runtime behavior that I want to illustrate, the image is appropriate. Also, no one is likely to believe me when I say byte[] buf = new byte[size1] results in buf having 2 bytes despite size1 has a value of 160! The image of runtime behaviors makes my case. – Thomas Nguyen Aug 14 '13 at 18:18
  • The image would be appropriate _if_ you include some sample source code that we can copy into our own IDEs and try it for ourselves. – Austin Salonen Aug 14 '13 at 18:20
  • What line is the program stopped at in the image – Taylor Tvrdy Aug 14 '13 at 18:21
  • The program throws an exception a few lines down when I used buf and it is not as long as expected. But that is not the real problem, which is why the allocated buf has a different length than it should be, especially when the allocation is done correctly, as far as I know. – Thomas Nguyen Aug 14 '13 at 18:27
  • And forgot to add, this problem is repeatable (not a one-off thing). size1 is always 160, buf is always 2 bytes, and buf2 is always 160 bytes, for all runs on my machine. – Thomas Nguyen Aug 14 '13 at 18:29

3 Answers3

8

The problem is here

byte[] buf = new byte[size1];
byte[] buf2 = new byte[16];
buf = b.ReadBytes(...); //<----

You are replacing buf with the result of ReadBytes. That throws away your original array and replaces it with the array that was returned from ReadBytes (which in your case was a two byte array)

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • Exactly! Can't believe I commit a classic newbie mistake, which is the values of the local variables as shown in the debug window are the state of the program at the point the program stops/breaks, not some arbitrary lines before the break point. I still have to figure out the problem with b.ReadBytes(...) but at least I will be going down the correct alley. Thanks Scott! – Thomas Nguyen Aug 14 '13 at 18:52
4

ReadBytes() returns a byte[]. When you write

buf = b.ReadBytes(Marshal.SizeOf(firstRecord));

then buf points at a completely different byte[] that equals whatever b.ReadBytes() returned.

Martin Smith
  • 438,706
  • 87
  • 741
  • 845
Rohan
  • 359
  • 2
  • 16
0

It looks like you're trying to convert some object into a byte array, this answer may help. Convert any object to a byte[]

Community
  • 1
  • 1
newb
  • 131
  • 1
  • 2