digitalmars.D.learn - Converting void* to D array
- Craig Dillabaugh (13/13) Apr 14 2015 Hi.
- Daniel =?UTF-8?B?S296w6Fr?= via Digitalmars-d-learn (4/25) Apr 14 2015 On Wed, 15 Apr 2015 04:24:20 +0000
- Daniel =?UTF-8?B?S296w6Fr?= via Digitalmars-d-learn (7/28) Apr 14 2015 On Wed, 15 Apr 2015 04:24:20 +0000
- Daniel Kozak (3/32) Apr 14 2015 But in this case memory will be scan by GC. Which probably is not
- Steven Schveighoffer (9/42) Apr 15 2015 Depends on what GByte is. If it doesn't contain pointers (I'm assuming
- CraigDillabaugh (6/58) Apr 15 2015 Thanks for the pointers (no pun intended!)
- Steven Schveighoffer (6/18) Apr 15 2015 malloc returns void *, but I am applying the slice operator which turns
- CraigDillabaugh (6/32) Apr 15 2015 Nice. Thanks. I didn't realize you can slice a bare pointer
- Steven Schveighoffer (4/7) Apr 15 2015 No, druntime cannot know what the pointer actually points at. This would...
- CraigDillabaugh (5/8) Apr 15 2015 On Wednesday, 15 April 2015 at 11:18:03 UTC, Steven Schveighoffer
- Craig Dillabaugh (4/34) Apr 14 2015 Thank you very much. Works like a charm.
Hi. I want to call a C library function that returns a data buffer as a void*. How do I convert the resulting void* into something I can process in D? //I have the following function from the GDAL C library. extern(C) CPLErr GDALReadBlock( GDALRasterBandH, int, int, void* ); So I have (GByte is defined in the GDAL library): void* buffer = malloc( GByte.sizeof * x_block_size * y_block_size ); I fill the buffer (and ignore any errors :o) GDALReadBlock( AGDALRasterBandHInstance, xblock, yblock, buffer ); Now, how can I access the data in buffer?
Apr 14 2015
On Wed, 15 Apr 2015 04:24:20 +0000 Craig Dillabaugh via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:Hi. I want to call a C library function that returns a data buffer as a void*. How do I convert the resulting void* into something I can process in D? //I have the following function from the GDAL C library. extern(C) CPLErr GDALReadBlock( GDALRasterBandH, int, int, void* ); So I have (GByte is defined in the GDAL library): void* buffer = malloc( GByte.sizeof * x_block_size * y_block_size ); I fill the buffer (and ignore any errors :o) GDALReadBlock( AGDALRasterBandHInstance, xblock, yblock, buffer ); Now, how can I access the data in buffer?auto buf = (cast(GByte *)buffer)[0 .. xblock * yblock];
Apr 14 2015
On Wed, 15 Apr 2015 04:24:20 +0000 Craig Dillabaugh via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:Hi. I want to call a C library function that returns a data buffer as a void*. How do I convert the resulting void* into something I can process in D? //I have the following function from the GDAL C library. extern(C) CPLErr GDALReadBlock( GDALRasterBandH, int, int, void* ); So I have (GByte is defined in the GDAL library): void* buffer = malloc( GByte.sizeof * x_block_size * y_block_size ); I fill the buffer (and ignore any errors :o) GDALReadBlock( AGDALRasterBandHInstance, xblock, yblock, buffer ); Now, how can I access the data in buffer?Or you probably can do it like this: auto buffer = new GByte[xblock*yblock]; GDALReadBlock( AGDALRasterBandHInstance, xblock, yblock, (cast void*)buffer.ptr );
Apr 14 2015
On Wednesday, 15 April 2015 at 04:43:39 UTC, Daniel Kozák wrote:On Wed, 15 Apr 2015 04:24:20 +0000 Craig Dillabaugh via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:But in this case memory will be scan by GC. Which probably is not something what you want.Hi. I want to call a C library function that returns a data buffer as a void*. How do I convert the resulting void* into something I can process in D? //I have the following function from the GDAL C library. extern(C) CPLErr GDALReadBlock( GDALRasterBandH, int, int, void* ); So I have (GByte is defined in the GDAL library): void* buffer = malloc( GByte.sizeof * x_block_size * y_block_size ); I fill the buffer (and ignore any errors :o) GDALReadBlock( AGDALRasterBandHInstance, xblock, yblock, buffer ); Now, how can I access the data in buffer?Or you probably can do it like this: auto buffer = new GByte[xblock*yblock]; GDALReadBlock( AGDALRasterBandHInstance, xblock, yblock, (cast void*)buffer.ptr );
Apr 14 2015
On 4/15/15 12:47 AM, Daniel Kozak wrote:On Wednesday, 15 April 2015 at 04:43:39 UTC, Daniel Kozák wrote:Depends on what GByte is. If it doesn't contain pointers (I'm assuming its probably ubyte?), then it won't be scanned. If you still want to use malloc, but in a safe way, you can do: immutable blocksize = GByte.sizeof * x_block_size * y_block_size; auto buffer = malloc(blocksize)[0..blocksize]; Also, you don't need to cast pointers to void *. Should be able to do: GDALReadBlock(AGDALRasterBandHInstance, xblock, yblock, buffer.ptr); -SteveOn Wed, 15 Apr 2015 04:24:20 +0000 Craig Dillabaugh via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:But in this case memory will be scan by GC. Which probably is not something what you want.Hi. I want to call a C library function that returns a data buffer as a void*. How do I convert the resulting void* into something I can process in D? //I have the following function from the GDAL C library. extern(C) CPLErr GDALReadBlock( GDALRasterBandH, int, int, void* ); So I have (GByte is defined in the GDAL library): void* buffer = malloc( GByte.sizeof * x_block_size * y_block_size ); I fill the buffer (and ignore any errors :o) GDALReadBlock( AGDALRasterBandHInstance, xblock, yblock, buffer ); Now, how can I access the data in buffer?Or you probably can do it like this: auto buffer = new GByte[xblock*yblock]; GDALReadBlock( AGDALRasterBandHInstance, xblock, yblock, (cast void*)buffer.ptr );
Apr 15 2015
On Wednesday, 15 April 2015 at 11:18:03 UTC, Steven Schveighoffer wrote:On 4/15/15 12:47 AM, Daniel Kozak wrote:Thanks for the pointers (no pun intended!) Just out of curiosity, what is the type of 'buffer'? 'malloc' returns a void* but using the .ptr suggests 'buffer' is an array. Is the return of malloc automatically converted to an array?On Wednesday, 15 April 2015 at 04:43:39 UTC, Daniel Kozák wrote:Depends on what GByte is. If it doesn't contain pointers (I'm assuming its probably ubyte?), then it won't be scanned. If you still want to use malloc, but in a safe way, you can do: immutable blocksize = GByte.sizeof * x_block_size * y_block_size; auto buffer = malloc(blocksize)[0..blocksize]; Also, you don't need to cast pointers to void *. Should be able to do: GDALReadBlock(AGDALRasterBandHInstance, xblock, yblock, buffer.ptr); -SteveOn Wed, 15 Apr 2015 04:24:20 +0000 Craig Dillabaugh via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:But in this case memory will be scan by GC. Which probably is not something what you want.Hi. I want to call a C library function that returns a data buffer as a void*. How do I convert the resulting void* into something I can process in D? //I have the following function from the GDAL C library. extern(C) CPLErr GDALReadBlock( GDALRasterBandH, int, int, void* ); So I have (GByte is defined in the GDAL library): void* buffer = malloc( GByte.sizeof * x_block_size * y_block_size ); I fill the buffer (and ignore any errors :o) GDALReadBlock( AGDALRasterBandHInstance, xblock, yblock, buffer ); Now, how can I access the data in buffer?Or you probably can do it like this: auto buffer = new GByte[xblock*yblock]; GDALReadBlock( AGDALRasterBandHInstance, xblock, yblock, (cast void*)buffer.ptr );
Apr 15 2015
On 4/15/15 8:35 AM, CraigDillabaugh wrote:On Wednesday, 15 April 2015 at 11:18:03 UTC, Steven Schveighoffer wrote:malloc returns void *, but I am applying the slice operator which turns a pointer into a slice. I'll split it up: auto bufptr = malloc(blocksize); // typeof(bufptr) = void * auto buffer = bufptr[0..blocksize]; // typeof(buffer) = void[] -Steveimmutable blocksize = GByte.sizeof * x_block_size * y_block_size; auto buffer = malloc(blocksize)[0..blocksize]; Also, you don't need to cast pointers to void *. Should be able to do: GDALReadBlock(AGDALRasterBandHInstance, xblock, yblock, buffer.ptr);Thanks for the pointers (no pun intended!) Just out of curiosity, what is the type of 'buffer'? 'malloc' returns a void* but using the .ptr suggests 'buffer' is an array. Is the return of malloc automatically converted to an array?
Apr 15 2015
On Wednesday, 15 April 2015 at 14:02:38 UTC, Steven Schveighoffer wrote:On 4/15/15 8:35 AM, CraigDillabaugh wrote:Nice. Thanks. I didn't realize you can slice a bare pointer like that. Does druntime have any way of making sure that is safe, or are you on your own?On Wednesday, 15 April 2015 at 11:18:03 UTC, Steven Schveighoffer wrote:malloc returns void *, but I am applying the slice operator which turns a pointer into a slice. I'll split it up: auto bufptr = malloc(blocksize); // typeof(bufptr) = void * auto buffer = bufptr[0..blocksize]; // typeof(buffer) = void[] -Steveimmutable blocksize = GByte.sizeof * x_block_size * y_block_size; auto buffer = malloc(blocksize)[0..blocksize]; Also, you don't need to cast pointers to void *. Should be able to do: GDALReadBlock(AGDALRasterBandHInstance, xblock, yblock, buffer.ptr);Thanks for the pointers (no pun intended!) Just out of curiosity, what is the type of 'buffer'? 'malloc' returns a void* but using the .ptr suggests 'buffer' is an array. Is the return of malloc automatically converted to an array?
Apr 15 2015
On 4/15/15 11:02 AM, CraigDillabaugh wrote:Nice. Thanks. I didn't realize you can slice a bare pointer like that. Does druntime have any way of making sure that is safe, or are you on your own?No, druntime cannot know what the pointer actually points at. This would not work in safe code. It's on you to make sure it's valid. -Steve
Apr 15 2015
On Wednesday, 15 April 2015 at 11:18:03 UTC, Steven Schveighoffer wrote: clipDepends on what GByte is. If it doesn't contain pointers (I'm assuming its probably ubyte?), then it won't be scanned.clip-SteveYes, GByte is an alias for ubyte.
Apr 15 2015
On Wednesday, 15 April 2015 at 04:43:39 UTC, Daniel Kozák wrote:On Wed, 15 Apr 2015 04:24:20 +0000 Craig Dillabaugh via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:Thank you very much. Works like a charm. Make that last line: GDALReadBlock( AGDALRasterBandHInstance, xblock, yblock,Hi. I want to call a C library function that returns a data buffer as a void*. How do I convert the resulting void* into something I can process in D? //I have the following function from the GDAL C library. extern(C) CPLErr GDALReadBlock( GDALRasterBandH, int, int, void* ); So I have (GByte is defined in the GDAL library): void* buffer = malloc( GByte.sizeof * x_block_size * y_block_size ); I fill the buffer (and ignore any errors :o) GDALReadBlock( AGDALRasterBandHInstance, xblock, yblock, buffer ); Now, how can I access the data in buffer?Or you probably can do it like this: auto buffer = new GByte[xblock*yblock]; GDALReadBlock( AGDALRasterBandHInstance, xblock, yblock, (cast void*)buffer.ptr );cast(void*)buffer.ptr );
Apr 14 2015