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.










share|improve this question


















  • 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 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




    "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















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.










share|improve this question


















  • 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 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




    "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













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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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




    "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














  • 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 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




    "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








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












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 );
}
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    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

























    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 );
    }
    }





    share|improve this answer



























      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 );
      }
      }





      share|improve this answer

























        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 );
        }
        }





        share|improve this answer














        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 );
        }
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 22 at 1:05

























        answered Nov 22 at 0:53









        Kingsley

        1,85611018




        1,85611018






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks