[.net] Pointer to Array

Started by
25 comments, last by Jonas B 17 years, 11 months ago
I need to speed up some memory copying by using Buffer.BlockCopy(), but I can't find a way to cast my *byte to the required byte[]. Any tips?
Advertisement
i dont understand the problem. could you explain it?
"Knowledge is no more expensive than ignorance, and at least as satisfying." -Barrin
Buffer.BlockCopy() only accepts Arrays, and all I've got is a pointer. I thought Arrays and Pointers were similar enough to allow casting between them, but

byte* ptr = null;
byte[] array = (byte[])ptr;

doesn't compile.
You can try something like this:

byte test = 'a'; //Example bytebyte* ptr = &test; //Pointer to Example bytebyte array[0]; //Array of bytesarray[0] = *ptr; //Makes array[0] equal to the contents of ptrcout << array[0] << endl; //Prints contents of array[0]
But that would mean copying the data byte by byte manually, which is what I'm trying to avoid...
Quote:
byte* ptr = null;
byte[] array = (byte[])ptr;


Are you sure a cast is required at all? Have youy tried passing "ptr" directly?
Its been a while since i wrote C/C++ code but isnt an array always a pointer sort of? Just try what bakery2k1 adviced you to do.
if you have a byte[] varname, the expression 'varname' is a pointer to the first element of the array (if its allocated), so its type is byte*.
"Knowledge is no more expensive than ignorance, and at least as satisfying." -Barrin
Sorry folks, I forgot to specify that the language is C#. The cast should work in C++ (and probably C++/CLI), so I (still) hope there's a way to do it in C# as well.
I'll try to remember that .NET != C# next time!

So, any clues for C#?
What do you have a pointer to? If you're using C# I thought it didn't have pointers?

Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development

This topic is closed to new replies.

Advertisement