www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - byte* and int length -> byte[]

reply mimocrocodil <4denizzz gmail.com> writes:
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
next sibling parent David Nadlinger <see klickverbot.at> writes:
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
prev sibling next sibling parent Jimmy Cao <jcao219 gmail.com> writes:
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];
Aug 15 2011
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
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
prev sibling next sibling parent Mafi <mafi example.org> writes:
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
prev sibling next sibling parent Vijay Nayar <madric gmail.com> writes:
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
prev sibling parent mimocrocodil <4denizzz gmail.com> writes:
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