A qusetion about code in bufferchunk

Topics: Developer Forum, User Forum
Jan 21, 2011 at 10:44 AM

HI

i have already donload source code of your project and work around it.

in BufferChunk class i found that you use unsafe code for this methode:

        private unsafe void _SetUInt64(int index, UInt64 data)
        {
            fixed(byte* pb = &buffer[this.index + index])
            {
                *((UInt64*)pb) = data;
            }
        }

this methode only return 1 byte .

my question is that why you use unsafe code? while we can only return 1 byte by using this solution:

private UInt64 _GetUInt64(int index)
  {
    return BitConverter.ToUInt64(buffer, this.index + index);
  }

Thanks
Coordinator
Jan 21, 2011 at 5:49 PM

Thanks for your comment.  My best guess is that Bufferchunk was written for .Net 1.0, before Bitconverter existed.  I agree that we could get rid of the unsafe code here.  I am confused about one aspect of your comment:  which method only returns one byte?  It looks like the Set* methods return void, and the Get* return different sized integers.

Also please note that we are trying to move the "official" conferenceXP forum over here:

http://cct.cs.washington.edu/conferencexp/forum

Cheers, Fred

 

Jan 22, 2011 at 6:58 AM

HI fvideon

i mean this code allways insert 1 byte of data into buffer - i think- isn't it?

*pb is a byte and data is a UInt64 even if we do this :

*((UInt64*)pb) = data;

it is insert only 1 byte of main data in to buffer.

 

amir110

Coordinator
Jan 24, 2011 at 6:14 PM

You can run this in the debugger to verify, but it looks to me like the cast to UInt64* will cause it to do the right thing and read/write 64 bits.

Fred