Please take a look

Lukas Pinkowski Lukas.Pinkowski at web.de
Fri Dec 21 01:42:29 PST 2007


bearophile wrote:

> I have implemented a struct, with methods, able to manage 24-bit unsigned
> integers. They are slower than an uint, so they may be useful only in
> quite uncommon situations when you must save RAM. In the attach you can
> find a stripped down version where I have removed unittests, speed tests,
> most Ddoc comments, etc. Now I know some D, but surely there are *lot* of
> things I know little about still (about memory management, operator
> overloading, casting, the best way to use the CPU, etc). If you have a bit
> of free time can you please take a look at that code in attach and tell me
> if you like it, if you can see some things that can be done in a better
> way in D, in a faster way, more D-onic, if you can solve some of the
> little problems you can find in the comments, etc.
> 
> Bye and thank you,
> bearophile


Taking a look at it, I see, you've done the following:

uint opCast() {
        //return (up << 16) | low; // worse?
        return *(cast(uint*)(cast(void*)this));
    }

You really should uncomment the first line and remove the second, since
uint* is a pointer to a 4-byte type, while your struct holds 3 bytes. Thus
dereferencing the uint* will lead to a 4th byte with an arbitrary value and
finally to errors.


More information about the Digitalmars-d-learn mailing list