[.net] Pointer to Array

Started by
25 comments, last by Jonas B 17 years, 11 months ago
Pointers are allowed in "unsafe" blocks, in this case it's a pointer to a surface's GraphicsStream.InternalData.
Advertisement
Can you use Marshal.Copy() instead? A pointer isn't equivalent to an array under .Net, because arrays know how long they are, so you won't be able to do a simple cast and I can't see a method on the Array class that would allow you to create one.
Marshal.Copy() *almost* works - but this is from one unmanaged pointer to another, and all Marshal.Copy() methods involve Arrays in some form.

I can get by without it, it just annoys me to not have access to (possibly) optimized copy funtions...
then simply iterate through your array given by the pointer (you must know its length), and put it into an array:

byte* bptr = your pointer thingie;
int l = length of the array pointed by your byte* ptr;
byte[] myArray = new byte[l];
for(int i=0; i<l; ++i)
{
myArray = *bptr; //i dont know how to reference in c#
++bptr;
}

one more thing: if you cant tell the length of the array (l), then no God will tell you that :)
"Knowledge is no more expensive than ignorance, and at least as satisfying." -Barrin
Thanks, but that's the manual entry-by-entry solution I already have and am trying to avoid. No optimizations there.
you are moving data between a safe and an unsafe variable. thants the thing that .net doesnt really like and will not support with specific functions i think
"Knowledge is no more expensive than ignorance, and at least as satisfying." -Barrin
Sorry, my bad - your example was not like what I have. I have two unsafe pointers. Marshal.Copy() handles Pointer to Array copies, so that's allowed.
Are you sure you need to optimize that routine in the first place? Even though things look dirty, it doesn't mean it executes dirtly [smile].

And I'm curious of your more high level problem so to speak. What are you trying to do in a broad sense?

I am asking because I suspect you are a C/C++ guy just starting with C#, wanting to do things the C/C++ way when the prefered would be the C# way.
[s]--------------------------------------------------------[/s]chromecode.com - software with source code
Actually, C++ was a long time ago...
One of the uses is copying data from a sound buffer to a D3D surface (for effects processing on the GPU). At least in C++ I seem to remember that memcpy() can be significantly faster than the manual approach.
Quote:Original post by Enselic
Are you sure you need to optimize that routine in the first place? Even though things look dirty, it doesn't mean it executes dirtly [smile].

And I'm curious of your more high level problem so to speak. What are you trying to do in a broad sense?

I am asking because I suspect you are a C/C++ guy just starting with C#, wanting to do things the C/C++ way when the prefered would be the C# way.


I have to agree with Enselic. Just because there are pointers in C# doesn't mean you should use them.

Actually, C# implements object references similarly to Java. What is called an "object reference" is actually a pointer to that object, in C++ terminology. These would be equivalent blocks of code in both languages:
//C#void doSomething(MyClass a){   a.modify(); //calling method of doSomething will see changes to a   a = new MyClass(); //wasted effort, the object reference is only a copy}//C++void doSomething(MyClass* a){   a->modify(); //calling method of doSomething will see changes to a   a = new MyClass(); //wasted effort, the object pointer is only a copy}

The significance is the parameter copying semantics. If the C# method were copied verbatim as a C++ function, then calling the function will create copies of the objects that are passed to the function. In C#, copies of the object references are made. When the C++ version in the code above is called, the object pointers are copied before being passed to the function. In both cases, the underlying object is NOT copied.

If we want equivalant C# code to C++ pass-by-reference, then we use the same ampersand syntax
//C++ and C#void somethingElse(MyClass& a){    //blah!}


There is no equivalent C# code for C++ pass-by-value for objects. Pass-by-value is used for stack-types (primitives, enums, structs), but never for heap-types.

The difference between pointers and references in C# is that the former is unmanaged and the latter is managed. The *only* time you need pointers in C# is when you are interoping with unmanaged C++ assemblies that require pointer parameters. There is a reason why you need a "unsafe" code block as well as a compiler flag to use pointers in C# -- you're not supposed to use them for general purpose work, they are only for very special purposes.

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

This topic is closed to new replies.

Advertisement