What it the preferred method to write a class to a file?

Jarrett Billingsley kb3ctd2 at yahoo.com
Mon Jul 24 17:15:03 PDT 2006


"Regan Heath" <regan at netwin.co.nz> wrote in message 
news:optc56yyzv23k2f5 at nrage...

> Option #1
> Note: .dup is required the local 'i' becomes invalid after the function 
> returns
> char[] uint_to_char(uint i)
> {
> char[] str = new char[4];
> *cast(uint*)str.ptr = i;
> return str.dup;
> }

I don't think a .dup is required here; when you write "*cast(uint*)str.ptr = 
i", it's copying the data from i to the array's memory, not setting the 
array's pointer to point to the integer.  Option 2 is actually semantically 
the same.  Would be kind of interesting to see what machine code each 
version produces :) 





More information about the Digitalmars-d-learn mailing list