www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - SIG11 crashing - can't figure it out

reply "Rob =?UTF-8?B?UGlla8OpIg==?= <robpieke gmail.com> writes:
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;
}
May 15 2015
next sibling parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
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); }
May 15 2015
parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
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
May 15 2015
parent reply "Rob =?UTF-8?B?UGlla8OpIg==?= <robpieke gmail.com> writes:
Thanks John, I'll log the issue and start brewing ldc. I'm 
"happy" that I wasn't doing something obviously stupid with my 
code (again, in terms of crashing, not design).

And also thanks for the "with" tip!

- Rob
May 15 2015
parent "Rob =?UTF-8?B?UGlla8OpIg==?= <robpieke gmail.com> writes:
https://issues.dlang.org/show_bug.cgi?id=14587

And confirmed that ldc2 seems to work, thanks again :)
May 15 2015
prev sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 5/15/15 7:08 AM, "Rob =?UTF-8?B?UGlla8OpIg==?= <robpieke gmail.com>" 
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).
Using dustmite (and 2.067.0), I reduced it to this: enum Suit { DIAMONDS, CLUBS} enum Value { ACE , TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING } struct Card { Value value; Suit suit; } void printCard(Card card) { final switch(card.value) { case Value.ACE: ; case Value.TWO, Value.THREE, Value.FOUR, Value.FIVE, Value.SIX, Value.SEVEN, Value.EIGHT, Value.NINE, Value.TEN: ; case Value.JACK: ; case Value.QUEEN: ; case Value.KING: ; } } int main() { auto card = Card(Value.JACK, Suit.CLUBS); printCard(card); return 0; } But before I figured out how to use dustmite (it was my first time), I hand reduced it to this: struct Card { int value; int suit; } void foo(Card card) { switch(card.value) { case 4: case 5: case 6: case 11: break; default: } } void main() { auto card = Card(11, 1); foo(card); } I see you filed a bug, I'll update with the reduced case. -Steve
May 15 2015