digitalmars.D.learn - Convert C array pointer to a D slice without data copy
- ParticlePeter (4/4) May 18 2015 I get the point to an array from a c function, the data size from
- tcak (7/11) May 18 2015 char* dataPtr;
- ParticlePeter (3/14) May 18 2015 Thanks, works. Should be in the "Interfacing to C" Reference
- John Colvin (3/14) May 18 2015 No need to worry about the GC here, it only scans the stack and
- Marco Leise (9/11) May 18 2015 And even if you add a root it wont free anything it did not
I get the point to an array from a c function, the data size from another function. The data should be only readable at the D side, but I would like to use it as a D slice without copying the data. Is this possible ?
May 18 2015
On Monday, 18 May 2015 at 09:18:33 UTC, ParticlePeter wrote:I get the point to an array from a c function, the data size from another function. The data should be only readable at the D side, but I would like to use it as a D slice without copying the data. Is this possible ?char* dataPtr; size_t dataLen; auto data = dataPtr[0 .. dataLen]; This doesn't do any copying. BUT I am not sure what GC would be doing about it. After you use it, you might want to set `data` to null in case of a problem.
May 18 2015
On Monday, 18 May 2015 at 09:23:26 UTC, tcak wrote:On Monday, 18 May 2015 at 09:18:33 UTC, ParticlePeter wrote:Thanks, works. Should be in the "Interfacing to C" Reference Article.I get the point to an array from a c function, the data size from another function. The data should be only readable at the D side, but I would like to use it as a D slice without copying the data. Is this possible ?char* dataPtr; size_t dataLen; auto data = dataPtr[0 .. dataLen]; This doesn't do any copying. BUT I am not sure what GC would be doing about it. After you use it, you might want to set `data` to null in case of a problem.
May 18 2015
On Monday, 18 May 2015 at 09:23:26 UTC, tcak wrote:On Monday, 18 May 2015 at 09:18:33 UTC, ParticlePeter wrote:No need to worry about the GC here, it only scans the stack and its own heap (unless you specifically add a new root).I get the point to an array from a c function, the data size from another function. The data should be only readable at the D side, but I would like to use it as a D slice without copying the data. Is this possible ?char* dataPtr; size_t dataLen; auto data = dataPtr[0 .. dataLen]; This doesn't do any copying. BUT I am not sure what GC would be doing about it. After you use it, you might want to set `data` to null in case of a problem.
May 18 2015
Am Mon, 18 May 2015 09:51:48 +0000 schrieb "John Colvin" <john.loughran.colvin gmail.com>:No need to worry about the GC here, it only scans the stack and its own heap (unless you specifically add a new root).And even if you add a root it wont free anything it did not allocate itself! You could even append to your C array. It will check how much capacity the slice still has on the GC heap and after realizing the .ptr is not even in one of its pools, allocate a GC copy of the C array right away. -- Marco
May 18 2015