I am making a function that turns a list of words into an array to be used by other functions, but somehow I'm overwriting previous words. I check the memory address' and they seem different, but when I recheck once I'm done importing the words, they are all the same.
我正在创建一个函数,将一个单词列表转换为一个数组供其他函数使用,但不知怎的,我正在覆盖以前的单词。我检查内存地址'并且它们看起来不一样,但是当我重新检查一次我完成导入单词后,它们都是一样的。
static char **array;
//takes the name of a data file and reads it into an array
static void InitDictionary(char *fileName){
//slide 36, chap 3
FILE *file;
int count,i;
char dummy[30];
file = fopen(fileName, "r");
while( fscanf(file, "%s", dummy) == 1 ){//counting at first
count++;
}
fclose(file);
array = (char**) malloc(count * sizeof(char*) );
count = 0;
file = fopen(fileName, "r");
while( fscanf(file, "%s", dummy) == 1 ){//now putting values in array
char newEntry[30];
strcpy(newEntry,dummy);
array[count] = newEntry;
printf("%d - %s : %p \n",count, array[count], &array[count]);
count++;
}
fclose(file);
for(i=0;i<count;i++)
printf("%d - %s : %p\n",i, array[i], &array[count] );
}
Thanks
谢谢
3 个解决方案
#1
3
user470379 is correct, you aren't allocating space for each new word. One possible fix is to replace the three lines:
user470379是正确的,您没有为每个新单词分配空间。一种可能的解决方法是更换三条线:
char newEntry[30];
strcpy(newEntry,dummy);
array[count] = newEntry;
with
同
array[count] = strdup(dummy);
#2
5
You need to allocate new memory for newEntry
each time through the while loop. You're currently storing a pointer to the single newEntry
buffer multiple times.
您需要每次通过while循环为newEntry分配新内存。您当前正在多次存储指向单个newEntry缓冲区的指针。
When you said you've checked the addresses, what address did you check specifically?
当您说您已经检查过地址时,您具体检查了哪个地址?
And actually, what's probably technically happening here is that you're storing references to a variable that goes out of scope after each iteration of the while loop. Since it is out of scope, the compiler is then free to reuse that stack memory which it does for the next iteration of the loop.
实际上,这里技术上可能发生的事情是,在while循环的每次迭代之后,存储对超出范围的变量的引用。由于它超出了范围,因此编译器可以*地重用它为循环的下一次迭代所做的堆栈内存。
#3
0
One problem I see, count isn't initialized and you're using it in your malloc.
我看到的一个问题是,count未初始化,你在malloc中使用它。
#1
3
user470379 is correct, you aren't allocating space for each new word. One possible fix is to replace the three lines:
user470379是正确的,您没有为每个新单词分配空间。一种可能的解决方法是更换三条线:
char newEntry[30];
strcpy(newEntry,dummy);
array[count] = newEntry;
with
同
array[count] = strdup(dummy);
#2
5
You need to allocate new memory for newEntry
each time through the while loop. You're currently storing a pointer to the single newEntry
buffer multiple times.
您需要每次通过while循环为newEntry分配新内存。您当前正在多次存储指向单个newEntry缓冲区的指针。
When you said you've checked the addresses, what address did you check specifically?
当您说您已经检查过地址时,您具体检查了哪个地址?
And actually, what's probably technically happening here is that you're storing references to a variable that goes out of scope after each iteration of the while loop. Since it is out of scope, the compiler is then free to reuse that stack memory which it does for the next iteration of the loop.
实际上,这里技术上可能发生的事情是,在while循环的每次迭代之后,存储对超出范围的变量的引用。由于它超出了范围,因此编译器可以*地重用它为循环的下一次迭代所做的堆栈内存。
#3
0
One problem I see, count isn't initialized and you're using it in your malloc.
我看到的一个问题是,count未初始化,你在malloc中使用它。