digitalmars.D.learn - byte* and int length -> byte[]
- mimocrocodil (9/9) Aug 15 2011 I obtain immutable(byte)* and int where contained length of bytes block ...
- David Nadlinger (3/7) Aug 15 2011 Simply slice the pointer: p[0 .. size]
- Jimmy Cao (3/13) Aug 15 2011 Yes:
- Steven Schveighoffer (10/20) Aug 15 2011 It's even easier:
- Mafi (5/14) Aug 15 2011 Pointers support indexing (like C) but additionally also slicing so if
- Vijay Nayar (28/43) Aug 15 2011 The question presented here really has two parts:
- mimocrocodil (6/6) Aug 15 2011 to Vijay Nayar:
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 blockSimply slice the pointer: p[0 .. size] David
Aug 15 2011
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 libraryYes: byte[] b = p[0 .. size];
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 libraryIt'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 libraryPointers 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 libraryThe 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
to Vijay Nayar: Thanks! Heh... Inside the my code this data also not be changed, so that I would agree to obtain something like immutable byte[] :)
Aug 15 2011