First time, I coded like this (I was making a trie) and when I called add("911"); I got a segmentation fault at there when *str == '1'
struct Node {
int check, check2;
Node* chd[10];
Node() : check(0), check2(0), chd{0, } {}
~Node() {
for (int i = 0; i < 10; i++)
if (chd[i])
delete chd[i];
}
int add(char* str) {
if (!str[0])
return 0;
if (chd[*str-'0'] == 0) // here I got SIGSEGV
chd[*str-'0'] = new Node();
...
return chd[*str-'0']->add(++str);
}
}
then I fixed that part like this
int i = *str-'0';
if (chd[i] == 0)
chd[i] = new Node();
there was no error.
What's the difference? What made the first code make error?
(I was solving ACM-ICPC > Regionals > Europe > Northwestern European Regional Contest > Nordic Collegiate Programming Contest > NCPC 2007 A)