Jump to content
  • Advertisement
Sign in to follow this  
Jonas B

[.net] Pointer to Array

This topic is 4442 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
You can try something like this:

byte test = 'a'; //Example byte
byte* ptr = &test; //Pointer to Example byte
byte array[0]; //Array of bytes
array[0] = *ptr; //Makes array[0] equal to the contents of ptr
cout << array[0] << endl; //Prints contents of array[0]

Share this post


Link to post
Share on other sites
Quote:

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


Are you sure a cast is required at all? Have youy tried passing "ptr" directly?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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*.

Share this post


Link to post
Share on other sites
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#?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!