473,508 Members | 2,240 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Arrays and pointers

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

Nov 15 '05 #1
5 2382
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;
}
}

--
William Stacey, MVP

"Fernando Cacciola" <fe***************@hotmail.com> wrote in message
news:Ok**************@tk2msftngp13.phx.gbl...
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

Nov 15 '05 #2
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
Nov 15 '05 #3
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
Nov 15 '05 #4
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

On Thu, 30 Oct 2003 17:10:55 -0300, "Fernando Cacciola"
<fe***************@hotmail.com> wrote:
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


Nov 15 '05 #5

"Austin Ehlers" <th***********************@hotmail.com> escribió en el
mensaje news:d7********************************@4ax.com...
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

Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
2822
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type >...
21
3897
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
11
6702
by: Linny | last post by:
Hi, I need some help in declaring an array of pointers to array of a certain fixed size. I want the pointers to point to arrays of fixed size only (should not work for variable sized arrays of the...
9
6653
by: Charles Banas | last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to get some opinions and comments on it, hopefully so i can gain some sort of insight as to why this works. at the top of the...
79
3333
by: Me | last post by:
Just a question/observation out of frustration. I read in depth the book by Peter Van Der Linden entitled "Expert C Programming" (Deep C Secrets). In particular the chapters entitled: 4: The...
8
2238
by: masood.iqbal | last post by:
All this time I was under the illusion that I understand the concept of multi-dimensional arrays well ---- however the following code snippet belies my understanding. I had assumed all along...
36
2798
by: raphfrk | last post by:
I have the following code: char buf; printf("%lp\n", buf); printf("%lp\n", &buf); printf("%lp\n", buf); printf("%lp\n", buf); printf("%d\n", buf-buf);
1
1997
by: Nathan Gilbert | last post by:
I have a function that is returning a 2D array (declared using double pointers) and I want to be able to append a row of data to the beginning and end of this array without having to create a new...
17
3219
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
4
2493
by: Christian Maier | last post by:
Hi After surfing a while I have still trouble with this array thing. I have the following function and recive a Segmentation fault, how must I code this right?? Thanks Christian Maier
0
7231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7132
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5059
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4720
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3196
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
432
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.