On Friday, 15 May 2015 at 11:44:32 UTC, John Colvin wrote:
On Friday, 15 May 2015 at 11:08:06 UTC, Rob Pieké wrote:
Working my way through Ali Çehreli's rather amazing e-book, I've hit a snag where some code I've written is pretty crashy. I consistently get "Segmentation fault: 11" (dmd 2.067.1, OSX).

I can't figure out where things are going wrong, because any attempt I make to debug via extra print statements causes the program to run successfully. Same if I try to compile with "-gc" ... it suddenly starts working, so I can't debug with gdb.

Putting aside any "that's probably not a great solution to the problem you're tying to solve" thoughts, can anyone offer me the "eureka" moment I'm missing to understand why the code below doesn't work?

Many thanks in advance!

* * *

import std.stdio;

enum Suit {
        HEARTS, DIAMONDS, CLUBS, SPADES
}

enum Value {
ACE = 1, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING
}

struct Card {
        Value value;
        Suit suit;
}

void printCard(in Card card) {
        final switch(card.value) {
                case Value.ACE:
                        write("A");
                        break;
case Value.TWO, Value.THREE, Value.FOUR, Value.FIVE, Value.SIX, Value.SEVEN, Value.EIGHT, Value.NINE, Value.TEN:
                        writef("%d", card.value);
                        break;
                case Value.JACK:
                        write("J");
                        break;
                case Value.QUEEN:
                        write("Q");
                        break;
                case Value.KING:
                        write("K");
                        break;
        }
        final switch(card.suit) {
                case Suit.HEARTS:
                        write("♡");
                        break;
                case Suit.DIAMONDS:
                        write("♢");
                        break;
                case Suit.CLUBS:
                        write("♣");
                        break;
                case Suit.SPADES:
                        write("♠");
                        break;
        }
        write("\n");
}

int main() {
        auto card = Card(Value.JACK, Suit.CLUBS);
        printCard(card);
        return 0;
}

It seems to be DMD specific, it works fine with ldc. If you're a homebrew user, brew install ldc and try it for yourself.

P.s. you can use the `with` statement to make things less verbose:

import std.stdio;

enum Suit {
        HEARTS, DIAMONDS, CLUBS, SPADES
}

enum Value {
        ACE = 1, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN,
JACK, QUEEN, KING
}

struct Card {
        Value value;
        Suit suit;
}

void printCard(in Card card) {
        final switch(card.value) with(Value) {
                case ACE:
                        write("A");
                        break;
                case TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN:
                        writef("%d", card.value);
                        break;
                case JACK:
                        write("J");
                        break;
                case QUEEN:
                        write("Q");
                        break;
                case KING:
                        write("K");
                        break;
        }
        final switch(card.suit) with(Suit) {
                case HEARTS:
                        write("♡");
                        break;
                case DIAMONDS:
                        write("♢");
                        break;
                case CLUBS:
                        write("♣");
                        break;
                case SPADES:
                        write("♠");
                        break;
        }
        write("\n");
}

void main() {
        auto card = Card(Value.JACK, Suit.CLUBS);
        printCard(card);
}

Please submit a bug report at issues.dlang.org

Reply via email to