开发者

Convert native buffer to MemoryStream

开发者 https://www.devze.com 2023-01-23 07:36 出处:网络
In my C++\\CLI i have this piece of code: array<Byte>^ out_buf = gcnew array<Byte>(stream_size);

In my C++\CLI i have this piece of code:

array<Byte>^ out_buf = gcnew array<Byte>(stream_size);
Marshal::Copy(IntPtr(buf), out_buf, 0, Int32(stream_size)); 
System::IO::MemoryStream^ stream = gcnew MemoryStream(out_buf);

in MemoryStream(out_buf), does memory stream allocate memory again or just take ownership of the out_buf?

if MemoryStream 开发者_StackOverflowdoes allocate memory again, is there way to convert native buffer to MemoryStream?


It allows you to treat out_buf (i.e., it does not allocate a new buffer) as a stream, so you have nothing to worry about another buffer being allocated.


MemoryStream(out_buf) does not allocate memory, nor does it take ownership. The GC will clean it up.

0

精彩评论

暂无评论...
验证码 换一张
取 消