Regarding following function, my debugger shows me __stack_chk_fail when finishing this function.
My system is Mac OS.
It is because my stack overflowed itself by checking references.
Also based on my experiment, if set vocab_size = 30000 it shows __stack_chk_fail error but when vocab_size = 20000 it is fine.
So I believe
vocab = (struct vocab_word *)malloc ((size_t) ((vocab_size + 1) * sizeof(struct vocab_word)));
is the issue. But malloc allocate memory on the heap rather than stack, so I am wondering where I goes wrong?
void populate_vocab(){
FILE *fin;
fin = fopen(word_file, "rb");
vocab = (struct vocab_word *)malloc ((size_t) ((vocab_size + 1) * sizeof(struct vocab_word)));
char word[MAX_STRING];
int word_idx = 0;
int num = 0;
boolean word_mode = 1;
long long cur_vocab_size = 0;
while (!feof(fin)) {
ch = fgetc(fin);
if(ch == ' '){
word_mode = 0;
}else if(ch == '\n'){
word_mode = 1;
word[word_idx] = 0;
vocab[cur_vocab_size].word = (char *)calloc(word_idx, sizeof(char));
strcpy(vocab[cur_vocab_size].word,word);
vocab[cur_vocab_size].cn = num;
cur_vocab_size++;
if (cur_vocab_size >= vocab_size){
break;
}
//fresh var
word_idx = 0;
num = 0;
}else{
if(word_mode){
word[word_idx] = ch;
word_idx ++;
}else{
num = num * 10;
num += ch - '0';
}
}
}
fclose(fin);
}
__stack_chk_fail , a callback function that is invoked when a stack buffer overflow is detected. This function shall abort the function that called it with a message that a stack buffer overflow has been detected, and then halt the program via exit , abort , or a custom panic handler.
One method to prevent stack overflow is to track the stack pointer with test and measurement methods. Use timer interrupts that periodically check the location of the stack pointer, record the largest value, and watch that it does not grow beyond that value.
Stack overflow is a software bug which occurs when a program tries to access more memory than the available stack size, which results in the crashing of the program. Stack is Last in First Out data structure (LIFO).
Based on comments, I figured out the reason. One of words exceed MAX_STRING which cause stack overflowed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With