p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Professional C#, 2nd and 3rd Editions (http://p2p.wrox.com/forumdisplay.php?f=44)
-   -   Take the address of a stack based array (http://p2p.wrox.com/showthread.php?t=28243)

Obnox April 18th, 2005 06:10 AM

Take the address of a stack based array
 
Consider the following code:
Code:

public unsafe class PointerArray
    {
        private int size;
        private long* pArray;

        public PointerArray(int size)
        {
            this.size = size;
            long* pArray = stackalloc long [size];
            this.pArray = &(pArray);
        }
    }

I want to assign the address of the stack based array to the pointer, using
Code:

this.pArray = &(pArray);
During compile I get the following error message:
Quote:

quote:Cannot implicitly convert type long** to long*
What am I doing wrong? Is there another way of keeping a stack based array within the scope of a class[?]

Thnx!

Obnox April 21st, 2005 08:12 AM

I solved the problem myself however not completely satisfied. Here's wy: I've changed the code to:
Code:

    public unsafe class PointerArray
    {
        private int size;
        public long* pArray;

        public PointerArray(int size)
        {
            this.size = size;
        }
    }

Now, I use the pArray as a field, rather than a property. Obviously I have to make it public, which is the exact fact that i do not like this solution. However it does work, the only thing I need to do after instantiating the class is assign a pointer reference to the property like this:
Code:

int size = 10
long* pArray = stackalloc long [size];
PointerArray pa = new PointerArray(size);
pa.pArray = pArray;

Note also that I do not assign the address anymore but simply assign the reference.

I'm a bit mixed up on this topick. Can anyone help with a proper solution?


All times are GMT -4. The time now is 09:05 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.