Pop Quiz what is wrong with this code?

Danni Coy danni.coy at gmail.com
Sun Jun 21 02:56:55 UTC 2020


On Sun, Jun 21, 2020 at 2:25 AM rikki cattermole via Digitalmars-d
<digitalmars-d at puremagic.com> wrote:
>
> On 21/06/2020 4:00 AM, Danni Coy wrote:
> > On Sun, Jun 21, 2020 at 1:15 AM rikki cattermole via Digitalmars-d
> > <digitalmars-d at puremagic.com> wrote:
> >>
> >> On 21/06/2020 3:08 AM, Danni Coy wrote:
> >>> ...
> >>> foreach (y, ref row; offsetMap)
> >>> {
> >>>      auto run = 0;
> >>>       auto nPixels = 0;
> >>>       foreach (x; 0..size.x)
> >>>       {
> >>>            immutable SDL_FPair offset  = { center.x - x, y - center.y };
> >>> ...
> >>>
> >>> and why does it violate the principle of making the right thing the
> >>> easy thing to do?
> >>>
> >>
> >> oo oo, is it that you didn't want x and y to be of type size_t?
> > more specifically I didn't want them to be unsigned
> > I am adapting the code from C++ code,
> > coming from other Algol syntax languages. That one is a real
> > gotcha. I tried explicitly making x and y ints and I got a depreciation warning.
>
> You are performing an operation on memory, size_t is indeed what you
> wanted. It is an offset into the address space past another offset (the
> pointer).

I am working with slices rather than with pointers directly.
In this case the maximum size of x or y I could possibly imagine being
useful is less than 64,000
realistic sizes for what I am doing are in the 10s or 100s.
This might not be the part I am most worried about.

> All D types are default initialized to a value.
>
> int in this case will default initialize to 0, which is what you are
> doing explicitly.

I can do
int nPixels;
or
auto nPixels = 0;


More information about the Digitalmars-d mailing list