How to use D without the GC ?
bachmeier
no at spam.net
Wed Jun 12 18:57:41 UTC 2024
On Wednesday, 12 June 2024 at 18:36:26 UTC, Vinod K Chandran
wrote:
> On Wednesday, 12 June 2024 at 15:33:39 UTC, bachmeier wrote:
>> A SafeRefCounted example with main marked @nogc:
>>
>> ```
>> import std;
>> import core.stdc.stdlib;
>>
>> struct Foo {
>> double[] data;
>> double * ptr;
>> alias data this;
>>
>> @nogc this(int n) {
>> ptr = cast(double*) malloc(n*double.sizeof);
>> data = ptr[0..n];
>> printf("Data has been allocated\n");
>> }
>> }
>>
>> ```
>>
> Why not just use `ptr` ? Why did you `data` with `ptr` ?
Try `foo[10] = 1.5` and `foo.ptr[10] = 1.5`. The first correctly
throws an out of bounds error. The second gives `Segmentation
fault (core dumped)`.
More information about the Digitalmars-d-learn
mailing list