digitalmars.D.learn - byte* and int length -> byte[]
- mimocrocodil <4denizzz gmail.com> Aug 15 2011
- David Nadlinger <see klickverbot.at> Aug 15 2011
- Jimmy Cao <jcao219 gmail.com> Aug 15 2011
- "Steven Schveighoffer" <schveiguy yahoo.com> Aug 15 2011
- Mafi <mafi example.org> Aug 15 2011
- Vijay Nayar <madric gmail.com> Aug 15 2011
I obtain immutable(byte)* and int where contained length of bytes block from C library. Can I convert this into byte[] without explict copying etc. Something like: byte* p; // bytes int size; // size of bytes block byte[] b; b.length = size; b.ptr = p; // now b contains bytes from library
Aug 15 2011
On 8/15/11 7:16 PM, mimocrocodil wrote:Can I convert this into byte[] without explict copying etc. […] byte* p; // bytes int size; // size of bytes block
Simply slice the pointer: p[0 .. size] David
Aug 15 2011
--00235427500080502404aa8e7ec7 Content-Type: text/plain; charset=ISO-8859-1 On Mon, Aug 15, 2011 at 12:16 PM, mimocrocodil <4denizzz gmail.com> wrote:I obtain immutable(byte)* and int where contained length of bytes block from C library. Can I convert this into byte[] without explict copying etc. Something like: byte* p; // bytes int size; // size of bytes block byte[] b; b.length = size; b.ptr = p; // now b contains bytes from library
Yes: byte[] b = p[0 .. size]; --00235427500080502404aa8e7ec7 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div><div class=3D"gmail_quote">On Mon, Aug 15, 2011 at 12:16 PM, mimocroco= dil <span dir=3D"ltr"><<a href=3D"mailto:4denizzz gmail.com">4denizzz gm= ail.com</a>></span> wrote:<br><blockquote class=3D"gmail_quote" style=3D= "margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"> I obtain immutable(byte)* and int where contained length of bytes block fro= m C library.<br> <br> Can I convert this into byte[] without explict copying etc.<br> <br> Something like:<br> <br> byte* p; // bytes<br> int size; // size of bytes block<br> <br> byte[] b;<br> b.length =3D size;<br> b.ptr =3D p;<br> <br> // now b contains bytes from library<br></blockquote><div><br></div><div><b= r></div><div>Yes:</div><div><br></div></div></div><blockquote class=3D"webk= it-indent-blockquote" style=3D"margin: 0 0 0 40px; border: none; padding: 0= px;"> <div><div class=3D"gmail_quote"><div>byte[] b =3D p[0 .. size];=A0</div></d= iv></div></blockquote><div><br></div> --00235427500080502404aa8e7ec7--
Aug 15 2011
On Mon, 15 Aug 2011 13:16:54 -0400, mimocrocodil <4denizzz gmail.com> wrote:I obtain immutable(byte)* and int where contained length of bytes block from C library. Can I convert this into byte[] without explict copying etc. Something like: byte* p; // bytes int size; // size of bytes block byte[] b; b.length = size; b.ptr = p; // now b contains bytes from library
It's even easier: auto b = p[0..size]; // b is of type byte[] I.e. you can use a slice operation on a pointer to create a correctly-typed slice. Keep in mind, the size is the number of *elements* for the slice, not the number of *bytes*. In your case it happens to be identical, but for larger element types it would be different. -Steve
Aug 15 2011
Am 15.08.2011 19:16, schrieb mimocrocodil:I obtain immutable(byte)* and int where contained length of bytes block from C library. Can I convert this into byte[] without explict copying etc. Something like: byte* p; // bytes int size; // size of bytes block byte[] b; b.length = size; b.ptr = p; // now b contains bytes from library
Pointers support indexing (like C) but additionally also slicing so if you have a byte* b and a int/long/size_t (or similiar) len then b[0.. len] is a slice starting b conatining len elements of type typeof(*b)[].
Aug 15 2011
On Mon, 15 Aug 2011 17:16:54 +0000, mimocrocodil wrote:I obtain immutable(byte)* and int where contained length of bytes block from C library. Can I convert this into byte[] without explict copying etc. Something like: byte* p; // bytes int size; // size of bytes block byte[] b; b.length = size; b.ptr = p; // now b contains bytes from library
The question presented here really has two parts: 1) Can you declare a new symbol without duplicating the original data? 2) Can a mutable symbol reference an immutable pointer? The short answer to (1) is yes, if you do not need mutability. The short answer to (2) is no. The following small program demonstrates this: import std.stdio; void main() { // Reference data to play with. immutable(byte)[] data = [0, 1, 2, 3, 4, 5, 6, 7]; // These two lines simulate having an immutable byte pointer. immutable(byte)* ptr = data.ptr; auto ptr_len = data.length; // Making an immutable copy is easy, 'auto' works here too. immutable(byte)[] immutableCopy = ptr[2 .. 6]; // Error! Cannot implicitly convert to immutable. //byte[] badMutableCopy = ptr[2 .. 6]; byte[] goodMutableCopy = immutableCopy.dup; // Make a simple edit to our mutable copy. foreach (ref b; goodMutableCopy) { b++; } // Output is: 2,3 3,4 4,5 5,6 for (auto i=0; i < immutableCopy.length; i++) { writeln(immutableCopy[i], ',', goodMutableCopy[i]); } }
Aug 15 2011









David Nadlinger <see klickverbot.at> 