[Issue 5258] [CTFE] Stack overflow with struct by ref

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri May 6 02:55:26 PDT 2011


http://d.puremagic.com/issues/show_bug.cgi?id=5258


bearophile_hugs at eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |regression


--- Comment #1 from bearophile_hugs at eml.cc 2011-05-06 02:51:16 PDT ---
This CTFE bug is present in DMD v1.068beta still, but it seems absent in DMD
v1.042.

A longer example that gives the correct answer with DMD 1.042 but produces
stack overflow with DMD v1.068beta:


import std.c.stdio: printf;

bool test(int k, int j, ulong diag45, ulong diag135, ulong cols) {
    return ((cols    & (1UL << j)) +
            (diag135 & (1UL << (j + k))) +
            (diag45  & (1UL << (32 + j - k))) ) == 0;
}

void mark(int k, int j, ref ulong diag45, ref ulong diag135, ref ulong cols) {
    cols    ^= (1UL << j);
    diag135 ^= (1UL << (j + k));
    diag45  ^= (1UL << (32 + j - k));
}

uint solve(int niv, int dx, ref ulong diag45, ref ulong diag135, ref ulong
cols) {
    uint solutions_found;

    if (niv) {
        for (int i = 0; i < dx; i++)
            if (test(niv, i, diag45, diag135, cols)) {
                mark(niv, i, diag45, diag135, cols);
                solutions_found += solve(niv - 1, dx, diag45, diag135, cols);
                mark(niv, i, diag45, diag135, cols);
            }
    } else {
        for (int i = 0; i < dx; i++)
            solutions_found += test(0, i, diag45, diag135, cols);
    }

    return solutions_found;
}

ulong nqueen(int n) {
    // masques
    ulong diag45  = 0; // / diagonal bitboard
    ulong diag135 = 0; // \ diagonal bitboard
    ulong cols    = 0; // column bitboard

    return solve(n - 1, n, diag45, diag135, cols);
}

const ulong result = nqueen(9);

void main() {
    // NQUEENS: 1, 0, 0, 2, 10, 4, 40, 92, 352, 724, 2_680, 14_200, 73_712,
365_596
    printf("%lld\n", result);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list