C 为什么这部分给出了分段错误?
这是一个不完整的代码C 为什么这部分给出了分段错误?,c,pointers,segmentation-fault,C,Pointers,Segmentation Fault,这是一个不完整的代码 #include <stdio.h> #include <stdlib.h> #include <string.h> #define W 1031 #define B 256 struct FileCoordinates{ int x; /*line number*/ int y; /*word number*/ struct FileCoordinates *next; }; struct FileS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define W 1031
#define B 256
struct FileCoordinates{
int x; /*line number*/
int y; /*word number*/
struct FileCoordinates *next;
};
struct FileStruct{
char *filename;
struct FileCoordinates *coordinates;
struct FileStruct *next;
};
struct WordStruct{
char *word;
struct WordStruct *left;
struct WordStruct *right;
struct FileStruct *files;
};
typedef struct FileCoordinates *CoorPtr;
typedef struct FileStruct *FilePtr;
typedef struct WordStruct *WordPtr;
WordPtr HashTable[W];
long int power(int a, long b){
long int pow, i;
pow = 1;
for (i = 0; i < b; i++){
pow = pow*a;
}
return pow;
}
int hashvalue (char *word){
long int i, value=0, n;
n = strlen(word);
for (i=0; i<n; i++){
value = value + power(B,n-i-1) * word[i];
}
return(value%W);
}
void putPosition(int x, int y, FilePtr *currfile){
CoorPtr currcors = (*currfile)->coordinates;
while (currcors!=NULL){
currcors = currcors->next;
}
currcors = (CoorPtr)malloc(sizeof(struct FileCoordinates));
currcors->x=x;
currcors->y=y;
}
void putFile(char *filename, WordPtr *currWord, int x, int y){
FilePtr currfile = (*currWord)->files;
while(currfile != NULL && strcmp(currfile->filename,filename)!=0){
currfile=currfile->next;
}
if (strcmp(currfile->filename,filename)==0){
putPosition(x, y, &currfile);
}
else{
currfile = (FilePtr)malloc(sizeof(struct FileStruct));
currfile->filename = filename;
putPosition(x, y, &currfile);
}
}
void insert(char *word, WordPtr *leaf, char *filename, int x, int y)
{
if( *leaf == NULL )
{
*leaf = (WordPtr) malloc( sizeof( struct WordStruct ) );
(*leaf)->word = word;
putFile(filename, &(*leaf), x, y);
/* initialize the children to null */
(*leaf)->left = 0;
(*leaf)->right = 0;
}
else if(word < (*leaf)->word)
{
insert( word, &(*leaf)->left, filename, x, y);
}
else if(word > (*leaf)->word)
{
insert( word, &(*leaf)->right, filename, x, y);
}
else if(word == (*leaf)->word){
putFile(filename, &(*leaf), x, y);
}
}
int main(int argc, char *argv[]){
int i, words, lines, value;
char *filename, *word, c;
FILE *fp;
word = (char *)malloc(21*sizeof(char));
if (argc<2){
perror("no files were inserted");
}
for (i=1; i<argc; i++){
words=1;
lines=1;
fp = fopen(argv[i], "r");
if (fp==NULL){
printf("Could not open file named %s! \n", argv[i]);
return 2;
}
filename = malloc( strlen( argv[i] ) + 1 );
strcpy( filename, argv[i] );
fscanf(fp, "%s", word);
value=hashvalue(word);
c=getc(fp);
insert(word, &HashTable[value], filename, lines, words);
if (c==' '){
words = words+1;
}
else if(c=='\n'){
lines=lines+1;
words=1;
}
}
system("PAUSE");
return 0;
}
代码的原因是获取文本文件作为参数,将单词排序到哈希表中的二叉树中,然后通过搜索关键字来显示它显示的坐标
无论如何,我知道这是一个非常新手的代码,但我正在努力理解。在哪里初始化
文件?(提示:你没有)。这意味着它是一个未定义的值(不一定为空)。在调用putFile
之前,您需要初始化文件
如注释中所述,一旦修复了该问题,您需要对FileCoordinates
和FileStruct
中的next
以及FileStruct
中的coordinates
执行相同的操作
同样如注释中所述,您可以重用字
缓冲区,这意味着树中的所有节点都将具有相同的字符串。当字符串存储在树中时,应该为其分配一个新的缓冲区。可能使用strdup
(*leaf)->word = strdup(word);
一旦修复了这个问题,您还需要进行字符串比较if(word==(*leaf)->word)
比较指针,而不是指针的内容。如果要比较实际字符串数据,需要使用strcmp
else if(strcmp(word, (*leaf)->word) == 0){
创建对象时,忘记将currfile->next
设置为NULL
:
currfile = (FilePtr)malloc(sizeof(struct FileStruct));
currfile->filename = filename;
使用calloc
而不是malloc
保留空间,或添加:
currfile->next = NULL;
currfile是否有下一个初始化的文件?(*leaf)->word=word代码>这不会复制字符串。在循环结束时,所有的叶子都指向同一个内存块。默认情况下它们不是空的吗?@Drew McGowen你的意思是我应该使用strcpy((*leaf)->word,word)@derpoverflow,否。malloc
中的内存未初始化。如果没有更多代码,“无更改”将毫无帮助。我怎么知道你做的这些更改是正确的呢?事实上我只是这样更改了它:我所要做的就是将其他字段也设置为空
currfile->next = NULL;