Segmentation fault error in C when trying to print to a file
up vote
1
down vote
favorite
I created a function which is supposed to to get a binary tree in argument, a filename in user input and inside that file print the binary tree in order to be later converted into a picture via graphviz.
The binary tree type provided is:
struct noeud_s;
typedef struct noeud_s noeud;
typedef noeud* arbre;
struct noeud_s{
char* valeur;
arbre gauche;
arbre droit;
};
The 2 functions I created are:
void create_dot(arbre racine)
{
FILE *f;
char file_name[100];
printf ("Nom du fichier a creer (Ajouter .dot a la fin): ");
scanf ("%s", file_name);
printf("Name: %sn", file_name);
printf ("Creation du fichier dotn");
f = fopen(file_name, "w");
if (f == NULL)
{
printf("NULLn");
}
fprintf(f, "digigraph tree {n");
write_to_dot(f, racine);
fprintf(f, "}");
fclose(f);
}
void write_to_dot(FILE *f, arbre racine)
{
if (racine == NULL)
{
return;
}
if (racine != NULL)
{
fprintf(f, "%s -> %s [label = "non"]n", racine -> valeur, racine -> gauche -> valeur);
fprintf(f, "%s -> %s [label = "oui"]n", racine -> valeur, racine -> droit -> valeur);
write_to_dot(f, racine -> gauche);
write_to_dot(f, racine -> droit);
}
return;
}
As far as debugging goes, I have deducted that my segmentation fault happens inside the write_to_dot function. But because I can't properly handle gdb, I would like you to help me find my segmentation fault and explain it please.
c segmentation-fault binary-tree
|
show 1 more comment
up vote
1
down vote
favorite
I created a function which is supposed to to get a binary tree in argument, a filename in user input and inside that file print the binary tree in order to be later converted into a picture via graphviz.
The binary tree type provided is:
struct noeud_s;
typedef struct noeud_s noeud;
typedef noeud* arbre;
struct noeud_s{
char* valeur;
arbre gauche;
arbre droit;
};
The 2 functions I created are:
void create_dot(arbre racine)
{
FILE *f;
char file_name[100];
printf ("Nom du fichier a creer (Ajouter .dot a la fin): ");
scanf ("%s", file_name);
printf("Name: %sn", file_name);
printf ("Creation du fichier dotn");
f = fopen(file_name, "w");
if (f == NULL)
{
printf("NULLn");
}
fprintf(f, "digigraph tree {n");
write_to_dot(f, racine);
fprintf(f, "}");
fclose(f);
}
void write_to_dot(FILE *f, arbre racine)
{
if (racine == NULL)
{
return;
}
if (racine != NULL)
{
fprintf(f, "%s -> %s [label = "non"]n", racine -> valeur, racine -> gauche -> valeur);
fprintf(f, "%s -> %s [label = "oui"]n", racine -> valeur, racine -> droit -> valeur);
write_to_dot(f, racine -> gauche);
write_to_dot(f, racine -> droit);
}
return;
}
As far as debugging goes, I have deducted that my segmentation fault happens inside the write_to_dot function. But because I can't properly handle gdb, I would like you to help me find my segmentation fault and explain it please.
c segmentation-fault binary-tree
1
You are dereferencing a null pointer here:fprintf(f, "%s -> %s [label = "non"]n", racine -> valeur, racine -> gauche -> valeur);
-->>fprintf, "%s -> %s [label = "non"]n", racine->valeur, rachine->gauche ? racine->gauche->valeur" : "Jamais!" );
– wildplasser
Nov 22 at 0:25
If you can run your program undergdb
, you can enter commands likeprint racine
,print racine->valeur
,print racine->gauche
, and so on. This will likely pinpoint the problem quickly. We can't do that because we have no idea how your code is callingcreate_dot
. Likely the problem is in the value passed forracine
and what it points to.
– David Schwartz
Nov 22 at 0:27
2
"But because I can't properly handle gdb" - then this would be an excellent opportunity for you to learn how to properly handle gdb. "Teach a man to fish" and all that stuff.
– paxdiablo
Nov 22 at 0:28
1
If I may, thetypedef noeud* arbre;
... I would like to suggest that typedef-ing pointer types to another name just confuses things. Unless of course "arbre" just means "pointer-to"
– Kingsley
Nov 22 at 0:34
@Kingsley I know that it's confusing but I have no choise. I was given that as a type definition and I can't change it
– Sotiris Kettenis
Nov 22 at 1:01
|
show 1 more comment
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I created a function which is supposed to to get a binary tree in argument, a filename in user input and inside that file print the binary tree in order to be later converted into a picture via graphviz.
The binary tree type provided is:
struct noeud_s;
typedef struct noeud_s noeud;
typedef noeud* arbre;
struct noeud_s{
char* valeur;
arbre gauche;
arbre droit;
};
The 2 functions I created are:
void create_dot(arbre racine)
{
FILE *f;
char file_name[100];
printf ("Nom du fichier a creer (Ajouter .dot a la fin): ");
scanf ("%s", file_name);
printf("Name: %sn", file_name);
printf ("Creation du fichier dotn");
f = fopen(file_name, "w");
if (f == NULL)
{
printf("NULLn");
}
fprintf(f, "digigraph tree {n");
write_to_dot(f, racine);
fprintf(f, "}");
fclose(f);
}
void write_to_dot(FILE *f, arbre racine)
{
if (racine == NULL)
{
return;
}
if (racine != NULL)
{
fprintf(f, "%s -> %s [label = "non"]n", racine -> valeur, racine -> gauche -> valeur);
fprintf(f, "%s -> %s [label = "oui"]n", racine -> valeur, racine -> droit -> valeur);
write_to_dot(f, racine -> gauche);
write_to_dot(f, racine -> droit);
}
return;
}
As far as debugging goes, I have deducted that my segmentation fault happens inside the write_to_dot function. But because I can't properly handle gdb, I would like you to help me find my segmentation fault and explain it please.
c segmentation-fault binary-tree
I created a function which is supposed to to get a binary tree in argument, a filename in user input and inside that file print the binary tree in order to be later converted into a picture via graphviz.
The binary tree type provided is:
struct noeud_s;
typedef struct noeud_s noeud;
typedef noeud* arbre;
struct noeud_s{
char* valeur;
arbre gauche;
arbre droit;
};
The 2 functions I created are:
void create_dot(arbre racine)
{
FILE *f;
char file_name[100];
printf ("Nom du fichier a creer (Ajouter .dot a la fin): ");
scanf ("%s", file_name);
printf("Name: %sn", file_name);
printf ("Creation du fichier dotn");
f = fopen(file_name, "w");
if (f == NULL)
{
printf("NULLn");
}
fprintf(f, "digigraph tree {n");
write_to_dot(f, racine);
fprintf(f, "}");
fclose(f);
}
void write_to_dot(FILE *f, arbre racine)
{
if (racine == NULL)
{
return;
}
if (racine != NULL)
{
fprintf(f, "%s -> %s [label = "non"]n", racine -> valeur, racine -> gauche -> valeur);
fprintf(f, "%s -> %s [label = "oui"]n", racine -> valeur, racine -> droit -> valeur);
write_to_dot(f, racine -> gauche);
write_to_dot(f, racine -> droit);
}
return;
}
As far as debugging goes, I have deducted that my segmentation fault happens inside the write_to_dot function. But because I can't properly handle gdb, I would like you to help me find my segmentation fault and explain it please.
c segmentation-fault binary-tree
c segmentation-fault binary-tree
asked Nov 22 at 0:19
Sotiris Kettenis
212
212
1
You are dereferencing a null pointer here:fprintf(f, "%s -> %s [label = "non"]n", racine -> valeur, racine -> gauche -> valeur);
-->>fprintf, "%s -> %s [label = "non"]n", racine->valeur, rachine->gauche ? racine->gauche->valeur" : "Jamais!" );
– wildplasser
Nov 22 at 0:25
If you can run your program undergdb
, you can enter commands likeprint racine
,print racine->valeur
,print racine->gauche
, and so on. This will likely pinpoint the problem quickly. We can't do that because we have no idea how your code is callingcreate_dot
. Likely the problem is in the value passed forracine
and what it points to.
– David Schwartz
Nov 22 at 0:27
2
"But because I can't properly handle gdb" - then this would be an excellent opportunity for you to learn how to properly handle gdb. "Teach a man to fish" and all that stuff.
– paxdiablo
Nov 22 at 0:28
1
If I may, thetypedef noeud* arbre;
... I would like to suggest that typedef-ing pointer types to another name just confuses things. Unless of course "arbre" just means "pointer-to"
– Kingsley
Nov 22 at 0:34
@Kingsley I know that it's confusing but I have no choise. I was given that as a type definition and I can't change it
– Sotiris Kettenis
Nov 22 at 1:01
|
show 1 more comment
1
You are dereferencing a null pointer here:fprintf(f, "%s -> %s [label = "non"]n", racine -> valeur, racine -> gauche -> valeur);
-->>fprintf, "%s -> %s [label = "non"]n", racine->valeur, rachine->gauche ? racine->gauche->valeur" : "Jamais!" );
– wildplasser
Nov 22 at 0:25
If you can run your program undergdb
, you can enter commands likeprint racine
,print racine->valeur
,print racine->gauche
, and so on. This will likely pinpoint the problem quickly. We can't do that because we have no idea how your code is callingcreate_dot
. Likely the problem is in the value passed forracine
and what it points to.
– David Schwartz
Nov 22 at 0:27
2
"But because I can't properly handle gdb" - then this would be an excellent opportunity for you to learn how to properly handle gdb. "Teach a man to fish" and all that stuff.
– paxdiablo
Nov 22 at 0:28
1
If I may, thetypedef noeud* arbre;
... I would like to suggest that typedef-ing pointer types to another name just confuses things. Unless of course "arbre" just means "pointer-to"
– Kingsley
Nov 22 at 0:34
@Kingsley I know that it's confusing but I have no choise. I was given that as a type definition and I can't change it
– Sotiris Kettenis
Nov 22 at 1:01
1
1
You are dereferencing a null pointer here:
fprintf(f, "%s -> %s [label = "non"]n", racine -> valeur, racine -> gauche -> valeur);
-->> fprintf, "%s -> %s [label = "non"]n", racine->valeur, rachine->gauche ? racine->gauche->valeur" : "Jamais!" );
– wildplasser
Nov 22 at 0:25
You are dereferencing a null pointer here:
fprintf(f, "%s -> %s [label = "non"]n", racine -> valeur, racine -> gauche -> valeur);
-->> fprintf, "%s -> %s [label = "non"]n", racine->valeur, rachine->gauche ? racine->gauche->valeur" : "Jamais!" );
– wildplasser
Nov 22 at 0:25
If you can run your program under
gdb
, you can enter commands like print racine
, print racine->valeur
, print racine->gauche
, and so on. This will likely pinpoint the problem quickly. We can't do that because we have no idea how your code is calling create_dot
. Likely the problem is in the value passed for racine
and what it points to.– David Schwartz
Nov 22 at 0:27
If you can run your program under
gdb
, you can enter commands like print racine
, print racine->valeur
, print racine->gauche
, and so on. This will likely pinpoint the problem quickly. We can't do that because we have no idea how your code is calling create_dot
. Likely the problem is in the value passed for racine
and what it points to.– David Schwartz
Nov 22 at 0:27
2
2
"But because I can't properly handle gdb" - then this would be an excellent opportunity for you to learn how to properly handle gdb. "Teach a man to fish" and all that stuff.
– paxdiablo
Nov 22 at 0:28
"But because I can't properly handle gdb" - then this would be an excellent opportunity for you to learn how to properly handle gdb. "Teach a man to fish" and all that stuff.
– paxdiablo
Nov 22 at 0:28
1
1
If I may, the
typedef noeud* arbre;
... I would like to suggest that typedef-ing pointer types to another name just confuses things. Unless of course "arbre" just means "pointer-to"– Kingsley
Nov 22 at 0:34
If I may, the
typedef noeud* arbre;
... I would like to suggest that typedef-ing pointer types to another name just confuses things. Unless of course "arbre" just means "pointer-to"– Kingsley
Nov 22 at 0:34
@Kingsley I know that it's confusing but I have no choise. I was given that as a type definition and I can't change it
– Sotiris Kettenis
Nov 22 at 1:01
@Kingsley I know that it's confusing but I have no choise. I was given that as a type definition and I can't change it
– Sotiris Kettenis
Nov 22 at 1:01
|
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
The code is printing out a binary tree. There is no code showing how nodes are constructed, but in typical binary trees, the leaf nodes have NULL
left & right child-nodes (or gauche
and droit
as it were).
The function write_to_dot
will fail at the first leaf-node (if not at the empty-side of an intermediate branch-node), because racine->gauche
and racine->droit
would be NULL
, yet they are still de-referenced - racine->gauche->valeur
without any checking.
While I don't have all the code, at least testing for this condition will solve one of the issues:
void write_to_dot ( FILE *f, arbre racine )
{
if ( racine != NULL )
{
if (racine->gauche != NULL)
fprintf ( f, "%s -> %s [label = "non"]n", racine->valeur, racine->gauche->valeur );
else
fprintf ( f, "%s -> NULL [label = "non"]n", racine->valeur );
if (racine->droit != NULL)
fprintf ( f, "%s -> %s [label = "oui"]n", racine->valeur, racine->droit->valeur );
else
fprintf ( f, "%s -> NULL [label = "oui"]n", racine->valeur );
write_to_dot ( f, racine->gauche );
write_to_dot ( f, racine->droit );
}
}
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
The code is printing out a binary tree. There is no code showing how nodes are constructed, but in typical binary trees, the leaf nodes have NULL
left & right child-nodes (or gauche
and droit
as it were).
The function write_to_dot
will fail at the first leaf-node (if not at the empty-side of an intermediate branch-node), because racine->gauche
and racine->droit
would be NULL
, yet they are still de-referenced - racine->gauche->valeur
without any checking.
While I don't have all the code, at least testing for this condition will solve one of the issues:
void write_to_dot ( FILE *f, arbre racine )
{
if ( racine != NULL )
{
if (racine->gauche != NULL)
fprintf ( f, "%s -> %s [label = "non"]n", racine->valeur, racine->gauche->valeur );
else
fprintf ( f, "%s -> NULL [label = "non"]n", racine->valeur );
if (racine->droit != NULL)
fprintf ( f, "%s -> %s [label = "oui"]n", racine->valeur, racine->droit->valeur );
else
fprintf ( f, "%s -> NULL [label = "oui"]n", racine->valeur );
write_to_dot ( f, racine->gauche );
write_to_dot ( f, racine->droit );
}
}
add a comment |
up vote
2
down vote
accepted
The code is printing out a binary tree. There is no code showing how nodes are constructed, but in typical binary trees, the leaf nodes have NULL
left & right child-nodes (or gauche
and droit
as it were).
The function write_to_dot
will fail at the first leaf-node (if not at the empty-side of an intermediate branch-node), because racine->gauche
and racine->droit
would be NULL
, yet they are still de-referenced - racine->gauche->valeur
without any checking.
While I don't have all the code, at least testing for this condition will solve one of the issues:
void write_to_dot ( FILE *f, arbre racine )
{
if ( racine != NULL )
{
if (racine->gauche != NULL)
fprintf ( f, "%s -> %s [label = "non"]n", racine->valeur, racine->gauche->valeur );
else
fprintf ( f, "%s -> NULL [label = "non"]n", racine->valeur );
if (racine->droit != NULL)
fprintf ( f, "%s -> %s [label = "oui"]n", racine->valeur, racine->droit->valeur );
else
fprintf ( f, "%s -> NULL [label = "oui"]n", racine->valeur );
write_to_dot ( f, racine->gauche );
write_to_dot ( f, racine->droit );
}
}
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
The code is printing out a binary tree. There is no code showing how nodes are constructed, but in typical binary trees, the leaf nodes have NULL
left & right child-nodes (or gauche
and droit
as it were).
The function write_to_dot
will fail at the first leaf-node (if not at the empty-side of an intermediate branch-node), because racine->gauche
and racine->droit
would be NULL
, yet they are still de-referenced - racine->gauche->valeur
without any checking.
While I don't have all the code, at least testing for this condition will solve one of the issues:
void write_to_dot ( FILE *f, arbre racine )
{
if ( racine != NULL )
{
if (racine->gauche != NULL)
fprintf ( f, "%s -> %s [label = "non"]n", racine->valeur, racine->gauche->valeur );
else
fprintf ( f, "%s -> NULL [label = "non"]n", racine->valeur );
if (racine->droit != NULL)
fprintf ( f, "%s -> %s [label = "oui"]n", racine->valeur, racine->droit->valeur );
else
fprintf ( f, "%s -> NULL [label = "oui"]n", racine->valeur );
write_to_dot ( f, racine->gauche );
write_to_dot ( f, racine->droit );
}
}
The code is printing out a binary tree. There is no code showing how nodes are constructed, but in typical binary trees, the leaf nodes have NULL
left & right child-nodes (or gauche
and droit
as it were).
The function write_to_dot
will fail at the first leaf-node (if not at the empty-side of an intermediate branch-node), because racine->gauche
and racine->droit
would be NULL
, yet they are still de-referenced - racine->gauche->valeur
without any checking.
While I don't have all the code, at least testing for this condition will solve one of the issues:
void write_to_dot ( FILE *f, arbre racine )
{
if ( racine != NULL )
{
if (racine->gauche != NULL)
fprintf ( f, "%s -> %s [label = "non"]n", racine->valeur, racine->gauche->valeur );
else
fprintf ( f, "%s -> NULL [label = "non"]n", racine->valeur );
if (racine->droit != NULL)
fprintf ( f, "%s -> %s [label = "oui"]n", racine->valeur, racine->droit->valeur );
else
fprintf ( f, "%s -> NULL [label = "oui"]n", racine->valeur );
write_to_dot ( f, racine->gauche );
write_to_dot ( f, racine->droit );
}
}
edited Nov 22 at 1:05
answered Nov 22 at 0:53
Kingsley
1,85611018
1,85611018
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422242%2fsegmentation-fault-error-in-c-when-trying-to-print-to-a-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You are dereferencing a null pointer here:
fprintf(f, "%s -> %s [label = "non"]n", racine -> valeur, racine -> gauche -> valeur);
-->>fprintf, "%s -> %s [label = "non"]n", racine->valeur, rachine->gauche ? racine->gauche->valeur" : "Jamais!" );
– wildplasser
Nov 22 at 0:25
If you can run your program under
gdb
, you can enter commands likeprint racine
,print racine->valeur
,print racine->gauche
, and so on. This will likely pinpoint the problem quickly. We can't do that because we have no idea how your code is callingcreate_dot
. Likely the problem is in the value passed forracine
and what it points to.– David Schwartz
Nov 22 at 0:27
2
"But because I can't properly handle gdb" - then this would be an excellent opportunity for you to learn how to properly handle gdb. "Teach a man to fish" and all that stuff.
– paxdiablo
Nov 22 at 0:28
1
If I may, the
typedef noeud* arbre;
... I would like to suggest that typedef-ing pointer types to another name just confuses things. Unless of course "arbre" just means "pointer-to"– Kingsley
Nov 22 at 0:34
@Kingsley I know that it's confusing but I have no choise. I was given that as a type definition and I can't change it
– Sotiris Kettenis
Nov 22 at 1:01