Arrays and pointers

F

Fernando Cacciola

For fast image procesign code I need pointers to the elements of a two
dimensional array:

mData = new int[mH,mW];
mScan0 = &mData[0,0];

however, the above won't compile since AFAICT fixed blocks require "local"
pointers:

fixed ( int* lScan0 = &mData[0,0] )
{
// this works
}

while I need the pointer to be a data member of the class (since client
objects use the scanline pointer to access the image).

If the above cannot be done with arrays, are there other block memory
allocation methods?

I figure that this should be possible since
System.Drawing.Imaging.BitmapData contains a field Scan0 which is
effectively a pointer to the Bitmap data array.

TIA

Fernando Cacciola
SciSoft
 
W

William Stacey

If you need global mem (and not sure you do), here is a sample:

private void button30_Click(object sender, System.EventArgs e)
{
IntPtr savePtr = Marshal.AllocHGlobal(1024);
ClassWithPointer cwp = new ClassWithPointer();
unsafe
{
byte * p = (byte*)savePtr;
p++; //Use the pointer for stuff...
cwp.i = 118;
cwp.pointer = p; //save the pointer in your class.
//...
Console.WriteLine("cwp.i:"+cwp.i);
Console.WriteLine("cwp.pointer:"+(int)cwp.pointer );
}
Marshal.FreeHGlobal(savePtr); //Free the global memory.
}
public unsafe class ClassWithPointer
{
public int i; //Using public for quick example. These would be private
normally.
public byte* pointer; //byte pointer you hold in your class.
public ClassWithPointer()
{
i = 0;
pointer = null;
}
}
 
F

Fernando Cacciola

Great! This will do just fine.

BTW, I've been creating a dummy Bitmap just to get a BitmapData out of it.
I wanted to get rid of it since all I really needed is a global fixed memory
block.
I guess Bitmap.LockBits() must be doing something like this, but probably
also copying the image to the global block.

Thanks!

Fernando Cacciola
SciSoft
 
F

Fernando Cacciola

BTW, assuming the mem block is allocated during initialization (in the
ctor), and the IntPtr is stored as a data member, then during:
void Dispose( bool disposing )
I have to free the block even if disposing==false (since the block is
unmanaged), right?

TIA

Fernando Cacciola
SciSoft
 
A

Austin Ehlers

If you want fast access arrays, don't use multi-dim arrays, use jagged
arrays.

int[][] mData = new int[mH][];
for (int x=0;x<mH;x++)
{
mData[x]=new int[mW];
}

Austin Ehlers
 
F

Fernando Cacciola

Austin Ehlers said:
If you want fast access arrays, don't use multi-dim arrays, use jagged
arrays.

int[][] mData = new int[mH][];
for (int x=0;x<mH;x++)
{
mData[x]=new int[mW];
}
I see.
The problem with jagged arrays is that the memory is not contiguos.
There are typical idioms that shift a pointer in a given row +- the length
of the row in bytes to access neighboring rows.
This shifting won't work, and it would make otherwise simple code quite
akward
(consider for instance the classic: *(ptr+delta[dir]) to turn around a
pixel)

Fernando Cacciola
SciSoft
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top