From D struct to C struct

Maxim Fomin maxim at maxim-fomin.ru
Mon Nov 18 07:27:44 PST 2013


On Monday, 18 November 2013 at 14:15:02 UTC, Namespace wrote:
> On Monday, 18 November 2013 at 10:09:12 UTC, qznc wrote:
>> On Monday, 18 November 2013 at 08:32:11 UTC, Namespace wrote:
>>> I found another approach. It avoids the GC and the Heap: A 
>>> Circular Buffer:
>>> http://dpaste.dzfl.pl/cf1e7afb
>>>
>>> That should work.
>>
>> It is unsafe, but might work in your specific case.
>>
>> The problem is that future changes might exhibit memory 
>> corruption, if the limit of your buffer is too low. Would 
>> probably be a hell to debug.
>
> Yes, but I know I never need more than 4 C pointer at the same 
> time.

I think there is bigger problem (and bigger memory error) here. 
When you inside struct method load pointer to some field you 
cannot rely that after leaving the method, the pointer will be 
safe. Structs are almost often located in stack and there are 
movable, which means that struct stack 'this' pointer may change.

There is even issue in bugzilla with example when delegate 
touches struct field and is invoked at some point later which 
causes silent memory error because delegate pointer became 
invalid.


More information about the Digitalmars-d-learn mailing list