0
分段錯誤(core dump),請大家幫忙。我不明白我做錯了什麼。代碼編譯,但我得到上面的錯誤。我知道代碼嘗試訪問它無法訪問的內存,但我不知道它發生了什麼。樹結構分段錯誤
#include <stdio.h>
#include <stdlib.h>
int freq[256] = {0};
struct Node
{
unsigned char m_ch;
int m_freq;
struct Node *m_ls,*m_rs;
struct Node *m_hls,*m_hrs;
};
struct Node* createNode(int freq,char ch);
void insertTree(struct Node **root,struct Node * n);
struct Node* getBinTree(FILE *fsrc);
void inorder(struct Node *root);
int main()
{
FILE *fsrc;
struct Node *tree=NULL;
fsrc = fopen("src.txt","rb");
tree=getBinTree(fsrc);
inorder(tree);
return 1;
}
struct Node* createNode(int freq,char ch)
{
struct Node *pNode=NULL;
pNode->m_freq=freq;
pNode->m_ch=ch;
return pNode;
}
void insertTree(struct Node **root,struct Node *n)
{
if(!(*root))
{
*root=n;
return;
}
if(n->m_freq<(*root)->m_freq)
{
insertTree(&(*root)->m_ls,n);
}
else
{
insertTree(&(*root)->m_rs,n);
}
}
struct Node* getBinTree(FILE *fsrc)
{
struct Node *temp=NULL;
struct Node **root=NULL;
int c,i;
while ((c = fgetc(fsrc)) != EOF)
{
freq[c]++;
}
freq[255]=1;
fclose(fsrc);
for(i=0;i<256;i++)
{
if(freq[i]>0)
{
temp=createNode(freq[i],i);
insertTree(root,temp);
}
}
}
void inorder(struct Node *root)
{
if(root != NULL)
{
inorder(root->m_ls);
printf(" %d\n",root->m_freq);
inorder(root->m_rs);
}
return;
}
您是否嘗試過使用您的調試器? –