Freeing memory from C

Chris wendlec at tcd.ie
Tue Dec 3 12:21:42 PST 2013


On Tuesday, 3 December 2013 at 16:52:29 UTC, Ali Çehreli wrote:
> On 12/03/2013 06:45 AM, Chris wrote:
>
> > I became aware of uninitialized variable. I think that the
> latter
> > behavior is the correct one (segfault > crash). But why did
> it work
> > correctly in the other D program, how did the C variable get
> initialized?
>
> Undefined behavior sometimes manifests itself as working 
> correctly. :)

True. The weird thing was that it seemed to work correctly in one 
program, i.e. during the test runs it calculated the right value 
of "int length;" and returned the string that was expected while 
it wasn't even initialized (!), and failed immediately in the 
other program defaulting to value -5180 or something (which is 
correct).

> Regarding your original question, I suggest that the C library 
> provides a function that frees the memory itself. The D code 
> should just call that function with the pointer at hand.
>
> Ali

You are right of course. Especially because I will need the C 
library for other languages like Python and I don't think they 
can handle it the way D can. Today the "hack" of freeing it from 
D worked well. The memory was freed correctly.


More information about the Digitalmars-d-learn mailing list