Segmentation fault (core dumped), please help. I don't understand what I did wrong. The code compiles but I get the error above. I understand that the code tries to access memory that it can't, but I don't see where it is happening.
#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;
}
struct Node *pNode=NULL;
pNode->m_freq=freq;
One of the reasons is dereferencing the NULL pointer in the quoted code block above.
Memory must be allocated before using the pointer. Like this:
struct Node *pNode = malloc (sizeof *pNode);
Related
I have this function and I want to test it and see if it does the job but it doesn't return
and when I try to change its return type to (t_list*) and make it return (node)
the compiler gives me segmentation fault
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct s_list
{
struct s_list *next;
void *data;
} t_list;
void ft_list_remove_if(t_list **begin_list, void *data_ref, int (*cmp)())
{
t_list *root;
t_list *node;
t_list *next;
root = *begin_list;
node = *begin_list;
while (root)
{
next = (*root).next;
if ((cmp)(root->data, data_ref) == 0)
{
if (root == *begin_list)
{
*begin_list = next;
}
node->next = next;
free(root);
}
node = root;
root = next;
}
}
in the main I created the "data_ref" and I made 5 structures some of them has "data_ref" and I linked them together
but when I call the function it gives me
segmentation fault
t_list *res;
res = ft_list_remove_if(ptr, str, strcmp);
ptr has the adress of the head of the list and str has the data_ref
Code for linked list using memory allocation:
#include<stdio.h>
struct node
{
int val;
struct node *point;
};
int main()
{
struct node *head;
head=(struct node* ) malloc(sizeof(struct node ));
void create(struct node* start);
void print(struct node* start);
create(head);
print(head);
return 0;
}
void create(struct node* start)
{
int test,a=1;
struct node* cur;
cur=start;
while(scanf("%d",&test)!=EOF)
{ if(a!=1)
{
cur=cur->point;
}
cur->point=(struct node* ) malloc(sizeof(struct node ));
cur->val=test;
a++;
printf("---------------");
printf("%d\n",cur);
printf("%d\n",cur->val);
printf("%d\n",cur->point);
printf("%d\n",a);
printf("---------------");
}
cur->point=NULL;
return;
}
void print(struct node* start)
{
struct node* cur;
cur=start;
while(cur!=NULL)
{
printf("%d ",cur->val);
cur=cur->point;
}
return;
}
Code for linked list using struct variable:
#include<stdio.h>
struct node
{
int val;
struct node *point;
};
int main()
{
struct node *head,link;
head=&link;
void create(struct node* start);
void print(struct node* start);
create(head);
print(head);
return 0;
}
void create(struct node* start)
{
int test,a=1;
struct node* cur;
cur=start;
while(scanf("%d",&test)!=EOF)
{ if(a!=1)
{
cur=cur->point;
}
struct node link;
cur->point=&link;
cur->val=test;
a++;
printf("---------------");
printf("%d\n",cur);
printf("%d\n",cur->val);
printf("%d\n",cur->point);
printf("%d\n",&link);
printf("%d\n",a);
printf("---------------");
}
cur->point=NULL;
return;
}
void print(struct node* start)
{
struct node* cur;
cur=start;
while(cur!=NULL)
{
printf("%d ",cur->val);
cur=cur->point;
}
return;
}
When I use 2nd code it doesn't work correctly. It prints just two values.
In while loop of create() I use struct node type variable declaration.
That's why it should it would declare a new struct node type link variable and a new memory (struct node link) address should be seen in every execution.
So what's the problem?
I am a newbie learning data structure using C. Please explain in simple language.
I created the following library to insert,delete,search and print nodes in a binary tree.
#include <stdlib.h>
struct NODE
{
int code;
char subject[20];
struct NODE *left;
struct NODE *right;
};
void InOrder(struct NODE *R)
{
if (R==NULL)
return;
InOrder(R->left);
printf("%d %s\n",R->code,R->subject);
InOrder(R->right);
}
void PreOrder(struct NODE *R)
{
if (R==NULL)
return;
printf("%d %s\n",R->code,R->subject);
InOrder(R->left);
InOrder(R->right);
}
void PostOrder(struct NODE *R)
{
if (R==NULL)
return;
InOrder(R->left);
InOrder(R->right);
printf("%d %s\n",R->code,R->subject);
}
struct NODE *Search(struct NODE *R,int CODE,struct NODE **father)
{
if(R==NULL)
return NULL;
if(R->code==CODE)
{
*father=R;
return R;
}
if (CODE<R->code)
return Search(R->left,CODE,father);
else
return Search(R->right,CODE,father);
}
struct NODE * CreateNode(struct NODE T)
{
struct NODE *tmp;
tmp=(struct NODE *)malloc(sizeof(T));
*tmp=T;
tmp->left=tmp->right=NULL;
return tmp;
}
int Insert(struct NODE **R,struct NODE ND)
{
struct NODE *cur,*fath=NULL;
cur=Search(*R,ND.code,&fath);
if (cur)
return 0;
cur=CreateNode(ND);
if(fath==NULL)
*R=cur;
else
if(fath->code>ND.code)
fath->left=cur;
else
fath->right=cur;
return 1;
}
struct NODE *MinOfMax (struct NODE *ND)
{
struct NODE *tmp;
if (ND==NULL)
return NULL;
if(ND->right==NULL)
return NULL;
tmp=ND->right;
while(tmp->left!=NULL)
tmp=tmp->left;
return tmp;
}
struct NODE* Delete(struct NODE *R, int code)
{
if (R==NULL)
return R;
if (code<R->code)
R->left=Delete(R->left,code);
else if (code>R->code)
R->right=Delete(R->right,code);
else
{
if (R->left==NULL)
{
struct NODE *temp=R->right;
free(R);
return temp;
}
else if (R->right==NULL)
{
struct NODE *temp=R->left;
free(R);
return temp;
}
struct NODE *temp=MinOfMax(R->right);
R->code=temp->code;
R->right=Delete(R->right,temp->code);
}
return R;
}
When i try to insert a node in the binary tree,the program crashes.Here is my main:
int main(int argc,char* argv[])
{
typedef struct NODE NODE;
NODE *root=NULL;
NODE tmp;
Insert(&root,tmp);
return 0;
}
I tried to assign static values (for example code=100 and subject="Physics") but still the program crashes.Should i malloc something,change anything in my header file or do something entirely different?I'm stuck here for hours without finding any solution.Most insert functions out there assume that i only have one integer as data in the node,but i need to pass the entire node.
Your code basically does nothing. It seems you copy-pasted it from somewhere. I tried to figure it out and here's a code example. Basically you've to initializate a new node in the main when you try to insert it.
Note that's just an example, i didn't a full test.
int main(int argc,char* argv[])
{
typedef struct NODE NODE;
NODE *root=NULL;
NODE *tmp = malloc(sizeof(struct NODE));
tmp->code = 1; /*Just a number*/
strcpy(tmp->subject,"prova"); /*Put something in it*/
Insert(&root,*tmp); /* Try to insert it*/
PreOrder(root); /*Try to see if it has been inserted*/
return 0;
}
Your tmp node, which is going to be the newly inserted node is used uninitialized in your main(). Your compiler could have warned you for this, if you had used -Wall flag.
So let's take a look in your insert function:
int Insert(struct NODE **R, struct NODE ND)
{
struct NODE *cur,*fath=NULL;
cur = Search(*R, ND.code, &fath); // ND.code is junk, since ND is uninitialized
...
return 1;
}
which likely causes the segmentation fault.
root is too, you could initialize it to NULL in main().
Not the cause of your problem, but Do I cast the result of malloc? No.
Hey i am starting to work on Huffman coding and I have a bit of a problem I getting this error
Segmentation fault (core dumped)
I understand it is caused by trying to reach memory you are not allow to but I can not realize what is the problem in my code, thank in advnace for the help!
src.txt - http://pastebin.com/kDf8nEhV
#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 = (struct Node*)malloc(sizeof(struct Node));
fsrc = fopen("src.txt", "rb");
tree = getBinTree(fsrc);
inorder(tree);
return 1;
}
struct Node* createNode(int freq, char ch) {
struct Node* pNode = (struct Node*)malloc(sizeof(struct Node));
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 = (struct Node*)malloc(sizeof(struct Node));
struct Node** root = (struct Node**)malloc(sizeof(struct Node*));
*root = (struct Node*)malloc(sizeof(struct Node));
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;
}
Your,
struct Node* getBinTree(FILE *fsrc)
is not returning anything.
You should enable compiler warnings, the compiler would have told you about that, the function is not returning a value, but you still assign it to tree in main().
But apart from that there is a serious problem with your code, a simple example
int main()
{
FILE *fsrc;
struct Node *tree = (struct Node*)malloc (sizeof(struct Node));
fsrc = fopen("src.txt","rb");
tree=getBinTree(fsrc);
inorder(tree);
return 1;
}
This single function has many problems,
You allocate new memory and point to it with tree, but you never use it because you overwrite the pointer here
tree = getBinTree(fsrc);
that causes a memory leak.
You don't check if the file was succesfuly opened, you must check
fsrc = fopen("src.txt", "rb");
if (fsrc == NULL)
return -1;
You don't need to cast malloc()
struct Node *tree = malloc(sizeof(*tree));
this is more robust, and easier to read.
You have to make sure that malloc() did not fail to allocate memory, when it does NULL is returned, so you need this when you do a malloc() that is actually required, for example in getBinTree()
struct Node *tree = malloc(sizeof(*tree));
if (tree == NULL)
return NULL;
i.e., copy malloc()'s behavior by returning NULL on failure.
I'm fairly new to Pointers and the memory model so excuse me if this is obvious, but I am writing a program to test a function reverse that reverses a list. Anyway I have it in three files, C5.c, C5-driver.c, and C5.h. Here they are in that order:
#include "C5.h"
#include <stdlib.h>
#include <stdio.h>
struct node *cons(int fst, struct node *rst) {
struct node *new = malloc(sizeof(struct node));
if (new == NULL) {
printf("cons: out of memory\n");
abort();
}
(*new).first = fst; /* same as (*new).first = fst */
(*new).rest = rst;
return new;
}
struct node *reverse(struct node *lst) {
struct node *ans = NULL;
while (lst != NULL) {
ans = cons((*lst).first, ans);
lst = (*lst).rest;
}
return ans;
}
void free_list(struct node *lst) {
struct node *p;
while (lst != NULL) {
p = lst->rest;
free(lst);
lst = p;
}
}
void print_list(struct node *lst) {
printf("( ");
while (lst != NULL) {
printf("%d ", (*lst).first);
lst = (*lst).rest;
}
printf(")\n");
}
C5-driver.c
#include <stdlib.h>
#include <stdio.h>
#include "C5.h"
int main() {
struct node *lst1 = cons(5, NULL);
struct node *lst2 = cons(3, lst1);
struct node *lst3 = cons(1, lst2);
print_list(lst3);
lst3 = reverse(lst3);
print_list(lst3);
free_list(lst3);
}
C5.h
struct node {
int first;
struct node *rest;
};
struct node *cons(int ,struct node *);
struct node *reverse(struct node *);
void print_list(struct node *);
void free_list(struct node *);
However I'm told by XCode that there are memory leaks.
I'm assuming it's after cons is used however I've tried creating a new struct node *ans = new and free(new); with return ans; but that doesn't work. I've also tried free_list as you can see above.
Thanks~
The reverse function calls cons which allocates memory, then it overwrites the lst3 pointer. The memory leak is that lst3 is overwritten which makes it impossible to recover that memory.
You should probably make a new variable like struct node *lst3_reverse and lst3_reverse = reverse(lst3). Then you can safely do free_list(lst3) and free_list(lst3_reverse) to free the memory.