Data buffering from a file. code not working as intended

100 Views Asked by At

I am not very experienced in C#, but have lots of experience from other languages.

I am doing a project in C# where I have to read and modify large files. For this I have coded a buffering scheme where I keep chunks of data in memory, and swap them to disk when I need to read more. I always eliminate the [0] element from the array, by moving the following elements back one position.

public struct TBBuffer
{
    public long offset;
    public short[] data;
    public GCHandle dataHandle;
}
//tb is a TBBuffer[], the data[] is initialized to 4096.

If I use a small sample file, where everything fits in the buffers allocated, everything works as intended.

Whenever I need to free up some memory for more data I do:

int bufIdx,bufNo;
for (bufIdx = 0; bufIdx < tb.buffer.Length - 1; bufIdx++)
{
    tb.buffer[bufIdx] = tb.buffer[bufIdx + 1];
}
bufNo = tb.Length - 1;

I have determined that the above code is the source of the problem, but I am unable to find out why that is so.

So my question is: Considering the TBBuffer struct, and its contents, does anybody have a clue why this is not working as expected ?

Is there a more efficient way to do this.

3

There are 3 best solutions below

2
Dmitry Bychenko On

Are you looking for array resize?

  Array.Resize(ref tb.buffer, newSize);

Just show up your intention to .Net and let it do the work (in the efficient way) for you.

1
VDN On

in for loop you use tb.Length. I think it should be tb.buffer.Length:

int bufIdx,bufNo;
for (bufIdx = 0; bufIdx < tb.buffer.Length - 1; bufIdx++)
{
    tb.buffer[bufIdx] = tb.buffer[bufIdx + 1];
}
bufNo = tb.Length - 1;
0
Dan Wulff On

I solved my problem... Rather embarrasing, but the problem was that the data[] of the very last buffer would point at the same as the next-to-last after I did my for () move stunt. After adding a tb.buffer[bufNo].data=new short[4096]; statement everything is back to normal. Thank you for your time, and all the constructive feedback. I will be looking into memory mapped files to see whether this feature will be a better option.