🔒 Locked
[.net] Pointer to Array
Advertisement
Original Post
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.
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 {
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 :)
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
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 :)
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.
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.
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.
I'm already a strong believer of the Way of the Managed Languages, but thanks anyway!
Pointers are very useful when processing certain types of data. Have you tried C# Image manipulation using Get/SetPixel()? Dog slow, the only decent way is to lock it and go unsafe with the pointer.
Pointers are very useful when processing certain types of data. Have you tried C# Image manipulation using Get/SetPixel()? Dog slow, the only decent way is to lock it and go unsafe with the pointer.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement