我有这样的数据结构:
struct WordCounter
{
char *word;
int word_count;
struct WordCounter *pNext; /* Pointer to the next word counter in the list */
};
我需要它来记住从文件中读取的单词以及它们出现的次数。这个结构体有以下功能:
int giveCounter (struct WordCounter *pCounter){ // give the occurrence of the current word in the linked list
int tempcount = pCounter -> word_count;
return tempcount;
}
int lengthOfCurrentWord(struct WordCounter *pWordCounter) // length of the current string
{
return strlen(pWordCounter->word)+1; // for the null terminator string
}
char* giveWord(struct WordCounter *pWordcounter){ // return current word of the linked list
return pWordcounter->word;
}
我应该做的是,将列表中的所有单词放入一个数组中,每个单词用终止符分隔。代码是这样的,在我解释了问题在哪里之后:
pCounter = pStart; // go to the first element in the linked list
num_car=0; // in total how much character in all words
while(pCounter != NULL){
num_car += lengthOfCurrentWord(pCounter);
pCounter = pCounter -> pNext;
}
printf("total number of chars in all words is: %d n ", num_car);
好了,现在我们有了大数组的维度,它必须保存所有以终止符分隔的单词。
char *exactly_word; // im forced to initialize like this and after i can give the exactly number of elements
exactly_word = malloc(sizeof(char)*num_car); // The big array
memset(exactly_word,0,num_car);
让我们开始将链表中的单词复制到大数组中:
char wordy[100] = {0}; // an array for the temp word
pCounter = pStart; // go to the first element
int indice = 0; // used for loop through single word
int indice_parole = 0; // It is used to indicate in which position of the large array to insert the character
for(int x=0; x<readed_nd_word;x++){ // readed_nd_words is the number of element in the linked list
strcat(wordy,giveWord(pCounter)); // copy the current word into wordy array
printf("WORDY: %s n",wordy); // yeah correct in each iteration
while(wordy[indice]!= 0){
printf("Character to add to exactly word: %c, at position: %d n",wordy[indice],indice_parole);
exactly_word[indice_parole] = wordy[indice]; // copy character
indice_parole++;
indice++;
}
pCounter = pCounter -> pNext; // move into next word in the linked list
memset(wordy,0,100); // reset for a new word
indice = 0;
exactly_word[indice_parole] =0; // terminator for separating words
printf("Terminator at position %d",indice_parole);
indice_parole++;
}
问题在哪里?好的,在while循环中,exactly_word以正确的方式填充,但是在这段代码之后,如果我尝试打印exactly_word[0],输出是:",同样的位置1,2的exactly_word。我可以用一个例子更好地解释这个问题,我用输出来描述它:
words in the linked list:
wut 1
sut 1
ov 1
word 2
ciao 1
helo 1
cor 1
fal 1
vermalen 1
total number of chars in all words is: 43 (countered the terminator too)
WORDY: wut
Character to add to exactly word: w, at position: 0
Character to add to exactly word: u, at position: 1
Character to add to exactly word: t, at position: 2
terminator at position 3
WORDY: sut
Character to add to exactly word: s, at position: 4
Character to add to exactly word: u, at position: 5
Character to add to exactly word: t, at position: 6
terminator at position 7
对于其余的单词,输出是正确的,但是在这段代码之后,正如我所说的,如果我打印exactly_word[0],我得到一个",与1,2相同,而不是在exactly_word[4] = "源自单词sut。如果我打印所有的数组,我得到的是:"sutovwordciaohelocorfalvermalen"
注。错误并不是每次运行都会出现,有时会出现,有时不会,当错误没有完全出现时,单词就会被更正。是不是有什么我看不见的无防卫行为?请帮忙:
在评论中,您向我们展示了exactly_word
的十六进制转储
01 00 00 00 73 75 74 00 6f 76 00 77 6f 72 64 ...
表示您覆盖了exactly_word
的前3个(可能是4个)字节的内存。看起来整数值1
已写入数组的开始(小端序),但这只是一个猜测,因为:它不会发生在发布的代码。它一定是在你没有给我们看的代码里。
也就是说,你现在的代码太复杂了。简化如下:
exactly_word = malloc(num_car);
char* dst = exactly_word;
pCounter = pStart;
while(pCounter){
strcpy(dst, giveWord(pCounter));
dst += lengthOfCurrentWord(pCounter); // which includes termination char
pCounter = pCounter -> pNext;
}