Pointer to Pointer as function parameter for a getter












-2















Given some struct:



struct Foo {
// ...
};


I want to use a function from a lib with the following signature:



void getAllFoos(Foo** foos);


Basically, the function should return a vector of all Foos, but someone decided to use the pointer-to-pointer for the purpose of returning something. I have to give some sort of empty Foo** to the function, so it can be filled.



How do I initialize foos when I want to call that function? Will a "null-pointer" (Foo**) be sufficient, or do I have to get the amount of foos first and malloc enough memory myself (how do I do that for a pointer-to-pointer?)? And who is responsible for freeing the memory?










share|improve this question

























  • A vector and pointer to pointer are not compatible, do you mean an array of Foos?

    – Rishikesh Raje
    Nov 21 '18 at 8:35













  • How do you know how many Foos return? How about C++ list/vector and shared pointers ?

    – tunglt
    Nov 21 '18 at 8:36











  • The signature void getAllFoos(Foo** foos); suggests, that the function wants the address of a Foo* so it can allocate memory for the user: Foo *foos; getAllFoos(&foos);

    – Swordfish
    Nov 21 '18 at 8:37






  • 3





    Please limit your question to *one* language. C *or* C++. In C the function declaration as shown is a syntax error.

    – Swordfish
    Nov 21 '18 at 8:39






  • 1





    What getAllFoos is doing exactly? Returns already existing array of pointers or creates it or copy to it? Why the array length is not specified as a second parameter?

    – serge
    Nov 21 '18 at 10:45


















-2















Given some struct:



struct Foo {
// ...
};


I want to use a function from a lib with the following signature:



void getAllFoos(Foo** foos);


Basically, the function should return a vector of all Foos, but someone decided to use the pointer-to-pointer for the purpose of returning something. I have to give some sort of empty Foo** to the function, so it can be filled.



How do I initialize foos when I want to call that function? Will a "null-pointer" (Foo**) be sufficient, or do I have to get the amount of foos first and malloc enough memory myself (how do I do that for a pointer-to-pointer?)? And who is responsible for freeing the memory?










share|improve this question

























  • A vector and pointer to pointer are not compatible, do you mean an array of Foos?

    – Rishikesh Raje
    Nov 21 '18 at 8:35













  • How do you know how many Foos return? How about C++ list/vector and shared pointers ?

    – tunglt
    Nov 21 '18 at 8:36











  • The signature void getAllFoos(Foo** foos); suggests, that the function wants the address of a Foo* so it can allocate memory for the user: Foo *foos; getAllFoos(&foos);

    – Swordfish
    Nov 21 '18 at 8:37






  • 3





    Please limit your question to *one* language. C *or* C++. In C the function declaration as shown is a syntax error.

    – Swordfish
    Nov 21 '18 at 8:39






  • 1





    What getAllFoos is doing exactly? Returns already existing array of pointers or creates it or copy to it? Why the array length is not specified as a second parameter?

    – serge
    Nov 21 '18 at 10:45
















-2












-2








-2








Given some struct:



struct Foo {
// ...
};


I want to use a function from a lib with the following signature:



void getAllFoos(Foo** foos);


Basically, the function should return a vector of all Foos, but someone decided to use the pointer-to-pointer for the purpose of returning something. I have to give some sort of empty Foo** to the function, so it can be filled.



How do I initialize foos when I want to call that function? Will a "null-pointer" (Foo**) be sufficient, or do I have to get the amount of foos first and malloc enough memory myself (how do I do that for a pointer-to-pointer?)? And who is responsible for freeing the memory?










share|improve this question
















Given some struct:



struct Foo {
// ...
};


I want to use a function from a lib with the following signature:



void getAllFoos(Foo** foos);


Basically, the function should return a vector of all Foos, but someone decided to use the pointer-to-pointer for the purpose of returning something. I have to give some sort of empty Foo** to the function, so it can be filled.



How do I initialize foos when I want to call that function? Will a "null-pointer" (Foo**) be sufficient, or do I have to get the amount of foos first and malloc enough memory myself (how do I do that for a pointer-to-pointer?)? And who is responsible for freeing the memory?







c++ pointers






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 9:23







Kackao

















asked Nov 21 '18 at 8:31









KackaoKackao

3361414




3361414













  • A vector and pointer to pointer are not compatible, do you mean an array of Foos?

    – Rishikesh Raje
    Nov 21 '18 at 8:35













  • How do you know how many Foos return? How about C++ list/vector and shared pointers ?

    – tunglt
    Nov 21 '18 at 8:36











  • The signature void getAllFoos(Foo** foos); suggests, that the function wants the address of a Foo* so it can allocate memory for the user: Foo *foos; getAllFoos(&foos);

    – Swordfish
    Nov 21 '18 at 8:37






  • 3





    Please limit your question to *one* language. C *or* C++. In C the function declaration as shown is a syntax error.

    – Swordfish
    Nov 21 '18 at 8:39






  • 1





    What getAllFoos is doing exactly? Returns already existing array of pointers or creates it or copy to it? Why the array length is not specified as a second parameter?

    – serge
    Nov 21 '18 at 10:45





















  • A vector and pointer to pointer are not compatible, do you mean an array of Foos?

    – Rishikesh Raje
    Nov 21 '18 at 8:35













  • How do you know how many Foos return? How about C++ list/vector and shared pointers ?

    – tunglt
    Nov 21 '18 at 8:36











  • The signature void getAllFoos(Foo** foos); suggests, that the function wants the address of a Foo* so it can allocate memory for the user: Foo *foos; getAllFoos(&foos);

    – Swordfish
    Nov 21 '18 at 8:37






  • 3





    Please limit your question to *one* language. C *or* C++. In C the function declaration as shown is a syntax error.

    – Swordfish
    Nov 21 '18 at 8:39






  • 1





    What getAllFoos is doing exactly? Returns already existing array of pointers or creates it or copy to it? Why the array length is not specified as a second parameter?

    – serge
    Nov 21 '18 at 10:45



















A vector and pointer to pointer are not compatible, do you mean an array of Foos?

– Rishikesh Raje
Nov 21 '18 at 8:35







A vector and pointer to pointer are not compatible, do you mean an array of Foos?

– Rishikesh Raje
Nov 21 '18 at 8:35















How do you know how many Foos return? How about C++ list/vector and shared pointers ?

– tunglt
Nov 21 '18 at 8:36





How do you know how many Foos return? How about C++ list/vector and shared pointers ?

– tunglt
Nov 21 '18 at 8:36













The signature void getAllFoos(Foo** foos); suggests, that the function wants the address of a Foo* so it can allocate memory for the user: Foo *foos; getAllFoos(&foos);

– Swordfish
Nov 21 '18 at 8:37





The signature void getAllFoos(Foo** foos); suggests, that the function wants the address of a Foo* so it can allocate memory for the user: Foo *foos; getAllFoos(&foos);

– Swordfish
Nov 21 '18 at 8:37




3




3





Please limit your question to *one* language. C *or* C++. In C the function declaration as shown is a syntax error.

– Swordfish
Nov 21 '18 at 8:39





Please limit your question to *one* language. C *or* C++. In C the function declaration as shown is a syntax error.

– Swordfish
Nov 21 '18 at 8:39




1




1





What getAllFoos is doing exactly? Returns already existing array of pointers or creates it or copy to it? Why the array length is not specified as a second parameter?

– serge
Nov 21 '18 at 10:45







What getAllFoos is doing exactly? Returns already existing array of pointers or creates it or copy to it? Why the array length is not specified as a second parameter?

– serge
Nov 21 '18 at 10:45














3 Answers
3






active

oldest

votes


















3














foos does not need to be initialized if getAllFoos can be assumed to work something like this -- it will initialize foos it for you:



void getAllFoos(Foo** pfoos) {
Foo* the_pointer_to_all_the_foos;
// (1) maybe the_pointer_to_all_the_foos = malloc(10 * sizeof(Foo));
// (2) maybe the_pointer_to_all_the_foos = new Foo[10];
// (3) maybe the_pointer_to_all_the_foos is a static Foo[10]
*pfoos = the_pointer_to_all_the_foos;
}


With the API above, there is no reason for getallFoos to read the value of pfoos.



To use it:



// in C, you should write: struct Foo* foos;
Foo* foos;
getAllFoos(&foos); // note: foos is Foo*, pfoos is Foo**


In general, the API documentation for getAllFoos must document what you need to do with foos in the end. Depending on what the code does [(1), (2), (3) from above or something else], you might need to free(foos), delete foos, do nothing, or do something else. We cannot answer that with the information given in the question.






share|improve this answer

































    0














    You need to know in advance how many Foo's you're going to get. There is no getting around this if you get all the Foos at once, and this is hard-wired in the interface. What you can do, though, if you're responsible for allocating memory, is to use a std::vector as a recipient, and give its data() member as an argument:



    std::vector<Foo> flexible_buffer(Foo_nb); // or reserve later
    getAllFoos(&(flexible_buffer.data());





    share|improve this answer
























    • if the function allocates memory somehow and tries to modify the pointer you pass it all hell breaks loose

      – Swordfish
      Nov 21 '18 at 8:45











    • You need to know in advance how many Foo's you're going to get. There is no getting around this Yes, there is. But without knowing which function of which library that is, its all guesswork.

      – Swordfish
      Nov 21 '18 at 8:53





















    0














    You should call that function by passing reference of pointer of type struct Foo.



    struct Foo *foo = NULL;
    getAllFoos(&foo);


    You can free foo as below



    free(foo); //Assuming getAllFoos allocated the memory and filled.






    Will a "null-pointer" (Foo**) be sufficient




    No FOO * should point to valid memory.



    struct Foo **foo = NULL;
    getAllFoos(foo);



    If getAllFoos tries to dereference foo You will have undefined behavior.






    share|improve this answer





















    • 1





      @DavidC.Rankin I believe what kiranBiradar tried to say with FOO * should point to valid memory. is that the parameter foo derecerenced once shouldn't be NULL.

      – Swordfish
      Nov 21 '18 at 8:47













    • Maybe you can reformulate your answer so it is clearer. Also, passing reference is a little misleading. &foo takes the address of foo and passes it as a value to getAllFoos(). No references involved.

      – Swordfish
      Nov 21 '18 at 8:50











    • So the answer was to simply say that "If it was NULL and the function dreferenced it -- then that would cause problems? I'll give it the benefit of the doubt, but I read the answer so say "NO you can't pass *foos as a NULL pointer. So I guess at best it was unclear either way. (I didn't ding for it -- so no harm there)

      – David C. Rankin
      Nov 21 '18 at 8:51








    • 2





      I see it the other way now, thanks @Swordfish, above the horizontal he is talking about struct Foo *foo = NULL;, below he is taking about an initial setting of struct Foo **foo = NULL; (and that is presumably in the caller) and then yes, I agree in part, NOT that "FOO * should point to valid memory." but that FOO * should have a valid address." (i.e. that it is a valid declared pointer itself)

      – David C. Rankin
      Nov 21 '18 at 8:55













    • All we came up with is speculation about OPs function at best. For all we know the function could set the passed pointer to the first node of a linked list.

      – Swordfish
      Nov 21 '18 at 8:59











    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',
    autoActivateHeartbeat: false,
    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%2f53407957%2fpointer-to-pointer-as-function-parameter-for-a-getter%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    foos does not need to be initialized if getAllFoos can be assumed to work something like this -- it will initialize foos it for you:



    void getAllFoos(Foo** pfoos) {
    Foo* the_pointer_to_all_the_foos;
    // (1) maybe the_pointer_to_all_the_foos = malloc(10 * sizeof(Foo));
    // (2) maybe the_pointer_to_all_the_foos = new Foo[10];
    // (3) maybe the_pointer_to_all_the_foos is a static Foo[10]
    *pfoos = the_pointer_to_all_the_foos;
    }


    With the API above, there is no reason for getallFoos to read the value of pfoos.



    To use it:



    // in C, you should write: struct Foo* foos;
    Foo* foos;
    getAllFoos(&foos); // note: foos is Foo*, pfoos is Foo**


    In general, the API documentation for getAllFoos must document what you need to do with foos in the end. Depending on what the code does [(1), (2), (3) from above or something else], you might need to free(foos), delete foos, do nothing, or do something else. We cannot answer that with the information given in the question.






    share|improve this answer






























      3














      foos does not need to be initialized if getAllFoos can be assumed to work something like this -- it will initialize foos it for you:



      void getAllFoos(Foo** pfoos) {
      Foo* the_pointer_to_all_the_foos;
      // (1) maybe the_pointer_to_all_the_foos = malloc(10 * sizeof(Foo));
      // (2) maybe the_pointer_to_all_the_foos = new Foo[10];
      // (3) maybe the_pointer_to_all_the_foos is a static Foo[10]
      *pfoos = the_pointer_to_all_the_foos;
      }


      With the API above, there is no reason for getallFoos to read the value of pfoos.



      To use it:



      // in C, you should write: struct Foo* foos;
      Foo* foos;
      getAllFoos(&foos); // note: foos is Foo*, pfoos is Foo**


      In general, the API documentation for getAllFoos must document what you need to do with foos in the end. Depending on what the code does [(1), (2), (3) from above or something else], you might need to free(foos), delete foos, do nothing, or do something else. We cannot answer that with the information given in the question.






      share|improve this answer




























        3












        3








        3







        foos does not need to be initialized if getAllFoos can be assumed to work something like this -- it will initialize foos it for you:



        void getAllFoos(Foo** pfoos) {
        Foo* the_pointer_to_all_the_foos;
        // (1) maybe the_pointer_to_all_the_foos = malloc(10 * sizeof(Foo));
        // (2) maybe the_pointer_to_all_the_foos = new Foo[10];
        // (3) maybe the_pointer_to_all_the_foos is a static Foo[10]
        *pfoos = the_pointer_to_all_the_foos;
        }


        With the API above, there is no reason for getallFoos to read the value of pfoos.



        To use it:



        // in C, you should write: struct Foo* foos;
        Foo* foos;
        getAllFoos(&foos); // note: foos is Foo*, pfoos is Foo**


        In general, the API documentation for getAllFoos must document what you need to do with foos in the end. Depending on what the code does [(1), (2), (3) from above or something else], you might need to free(foos), delete foos, do nothing, or do something else. We cannot answer that with the information given in the question.






        share|improve this answer















        foos does not need to be initialized if getAllFoos can be assumed to work something like this -- it will initialize foos it for you:



        void getAllFoos(Foo** pfoos) {
        Foo* the_pointer_to_all_the_foos;
        // (1) maybe the_pointer_to_all_the_foos = malloc(10 * sizeof(Foo));
        // (2) maybe the_pointer_to_all_the_foos = new Foo[10];
        // (3) maybe the_pointer_to_all_the_foos is a static Foo[10]
        *pfoos = the_pointer_to_all_the_foos;
        }


        With the API above, there is no reason for getallFoos to read the value of pfoos.



        To use it:



        // in C, you should write: struct Foo* foos;
        Foo* foos;
        getAllFoos(&foos); // note: foos is Foo*, pfoos is Foo**


        In general, the API documentation for getAllFoos must document what you need to do with foos in the end. Depending on what the code does [(1), (2), (3) from above or something else], you might need to free(foos), delete foos, do nothing, or do something else. We cannot answer that with the information given in the question.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 21 '18 at 10:25

























        answered Nov 21 '18 at 9:00









        palotasbpalotasb

        2,37111420




        2,37111420

























            0














            You need to know in advance how many Foo's you're going to get. There is no getting around this if you get all the Foos at once, and this is hard-wired in the interface. What you can do, though, if you're responsible for allocating memory, is to use a std::vector as a recipient, and give its data() member as an argument:



            std::vector<Foo> flexible_buffer(Foo_nb); // or reserve later
            getAllFoos(&(flexible_buffer.data());





            share|improve this answer
























            • if the function allocates memory somehow and tries to modify the pointer you pass it all hell breaks loose

              – Swordfish
              Nov 21 '18 at 8:45











            • You need to know in advance how many Foo's you're going to get. There is no getting around this Yes, there is. But without knowing which function of which library that is, its all guesswork.

              – Swordfish
              Nov 21 '18 at 8:53


















            0














            You need to know in advance how many Foo's you're going to get. There is no getting around this if you get all the Foos at once, and this is hard-wired in the interface. What you can do, though, if you're responsible for allocating memory, is to use a std::vector as a recipient, and give its data() member as an argument:



            std::vector<Foo> flexible_buffer(Foo_nb); // or reserve later
            getAllFoos(&(flexible_buffer.data());





            share|improve this answer
























            • if the function allocates memory somehow and tries to modify the pointer you pass it all hell breaks loose

              – Swordfish
              Nov 21 '18 at 8:45











            • You need to know in advance how many Foo's you're going to get. There is no getting around this Yes, there is. But without knowing which function of which library that is, its all guesswork.

              – Swordfish
              Nov 21 '18 at 8:53
















            0












            0








            0







            You need to know in advance how many Foo's you're going to get. There is no getting around this if you get all the Foos at once, and this is hard-wired in the interface. What you can do, though, if you're responsible for allocating memory, is to use a std::vector as a recipient, and give its data() member as an argument:



            std::vector<Foo> flexible_buffer(Foo_nb); // or reserve later
            getAllFoos(&(flexible_buffer.data());





            share|improve this answer













            You need to know in advance how many Foo's you're going to get. There is no getting around this if you get all the Foos at once, and this is hard-wired in the interface. What you can do, though, if you're responsible for allocating memory, is to use a std::vector as a recipient, and give its data() member as an argument:



            std::vector<Foo> flexible_buffer(Foo_nb); // or reserve later
            getAllFoos(&(flexible_buffer.data());






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 21 '18 at 8:38









            papagagapapagaga

            507311




            507311













            • if the function allocates memory somehow and tries to modify the pointer you pass it all hell breaks loose

              – Swordfish
              Nov 21 '18 at 8:45











            • You need to know in advance how many Foo's you're going to get. There is no getting around this Yes, there is. But without knowing which function of which library that is, its all guesswork.

              – Swordfish
              Nov 21 '18 at 8:53





















            • if the function allocates memory somehow and tries to modify the pointer you pass it all hell breaks loose

              – Swordfish
              Nov 21 '18 at 8:45











            • You need to know in advance how many Foo's you're going to get. There is no getting around this Yes, there is. But without knowing which function of which library that is, its all guesswork.

              – Swordfish
              Nov 21 '18 at 8:53



















            if the function allocates memory somehow and tries to modify the pointer you pass it all hell breaks loose

            – Swordfish
            Nov 21 '18 at 8:45





            if the function allocates memory somehow and tries to modify the pointer you pass it all hell breaks loose

            – Swordfish
            Nov 21 '18 at 8:45













            You need to know in advance how many Foo's you're going to get. There is no getting around this Yes, there is. But without knowing which function of which library that is, its all guesswork.

            – Swordfish
            Nov 21 '18 at 8:53







            You need to know in advance how many Foo's you're going to get. There is no getting around this Yes, there is. But without knowing which function of which library that is, its all guesswork.

            – Swordfish
            Nov 21 '18 at 8:53













            0














            You should call that function by passing reference of pointer of type struct Foo.



            struct Foo *foo = NULL;
            getAllFoos(&foo);


            You can free foo as below



            free(foo); //Assuming getAllFoos allocated the memory and filled.






            Will a "null-pointer" (Foo**) be sufficient




            No FOO * should point to valid memory.



            struct Foo **foo = NULL;
            getAllFoos(foo);



            If getAllFoos tries to dereference foo You will have undefined behavior.






            share|improve this answer





















            • 1





              @DavidC.Rankin I believe what kiranBiradar tried to say with FOO * should point to valid memory. is that the parameter foo derecerenced once shouldn't be NULL.

              – Swordfish
              Nov 21 '18 at 8:47













            • Maybe you can reformulate your answer so it is clearer. Also, passing reference is a little misleading. &foo takes the address of foo and passes it as a value to getAllFoos(). No references involved.

              – Swordfish
              Nov 21 '18 at 8:50











            • So the answer was to simply say that "If it was NULL and the function dreferenced it -- then that would cause problems? I'll give it the benefit of the doubt, but I read the answer so say "NO you can't pass *foos as a NULL pointer. So I guess at best it was unclear either way. (I didn't ding for it -- so no harm there)

              – David C. Rankin
              Nov 21 '18 at 8:51








            • 2





              I see it the other way now, thanks @Swordfish, above the horizontal he is talking about struct Foo *foo = NULL;, below he is taking about an initial setting of struct Foo **foo = NULL; (and that is presumably in the caller) and then yes, I agree in part, NOT that "FOO * should point to valid memory." but that FOO * should have a valid address." (i.e. that it is a valid declared pointer itself)

              – David C. Rankin
              Nov 21 '18 at 8:55













            • All we came up with is speculation about OPs function at best. For all we know the function could set the passed pointer to the first node of a linked list.

              – Swordfish
              Nov 21 '18 at 8:59
















            0














            You should call that function by passing reference of pointer of type struct Foo.



            struct Foo *foo = NULL;
            getAllFoos(&foo);


            You can free foo as below



            free(foo); //Assuming getAllFoos allocated the memory and filled.






            Will a "null-pointer" (Foo**) be sufficient




            No FOO * should point to valid memory.



            struct Foo **foo = NULL;
            getAllFoos(foo);



            If getAllFoos tries to dereference foo You will have undefined behavior.






            share|improve this answer





















            • 1





              @DavidC.Rankin I believe what kiranBiradar tried to say with FOO * should point to valid memory. is that the parameter foo derecerenced once shouldn't be NULL.

              – Swordfish
              Nov 21 '18 at 8:47













            • Maybe you can reformulate your answer so it is clearer. Also, passing reference is a little misleading. &foo takes the address of foo and passes it as a value to getAllFoos(). No references involved.

              – Swordfish
              Nov 21 '18 at 8:50











            • So the answer was to simply say that "If it was NULL and the function dreferenced it -- then that would cause problems? I'll give it the benefit of the doubt, but I read the answer so say "NO you can't pass *foos as a NULL pointer. So I guess at best it was unclear either way. (I didn't ding for it -- so no harm there)

              – David C. Rankin
              Nov 21 '18 at 8:51








            • 2





              I see it the other way now, thanks @Swordfish, above the horizontal he is talking about struct Foo *foo = NULL;, below he is taking about an initial setting of struct Foo **foo = NULL; (and that is presumably in the caller) and then yes, I agree in part, NOT that "FOO * should point to valid memory." but that FOO * should have a valid address." (i.e. that it is a valid declared pointer itself)

              – David C. Rankin
              Nov 21 '18 at 8:55













            • All we came up with is speculation about OPs function at best. For all we know the function could set the passed pointer to the first node of a linked list.

              – Swordfish
              Nov 21 '18 at 8:59














            0












            0








            0







            You should call that function by passing reference of pointer of type struct Foo.



            struct Foo *foo = NULL;
            getAllFoos(&foo);


            You can free foo as below



            free(foo); //Assuming getAllFoos allocated the memory and filled.






            Will a "null-pointer" (Foo**) be sufficient




            No FOO * should point to valid memory.



            struct Foo **foo = NULL;
            getAllFoos(foo);



            If getAllFoos tries to dereference foo You will have undefined behavior.






            share|improve this answer















            You should call that function by passing reference of pointer of type struct Foo.



            struct Foo *foo = NULL;
            getAllFoos(&foo);


            You can free foo as below



            free(foo); //Assuming getAllFoos allocated the memory and filled.






            Will a "null-pointer" (Foo**) be sufficient




            No FOO * should point to valid memory.



            struct Foo **foo = NULL;
            getAllFoos(foo);



            If getAllFoos tries to dereference foo You will have undefined behavior.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 21 '18 at 9:00

























            answered Nov 21 '18 at 8:37









            kiran Biradarkiran Biradar

            5,5122927




            5,5122927








            • 1





              @DavidC.Rankin I believe what kiranBiradar tried to say with FOO * should point to valid memory. is that the parameter foo derecerenced once shouldn't be NULL.

              – Swordfish
              Nov 21 '18 at 8:47













            • Maybe you can reformulate your answer so it is clearer. Also, passing reference is a little misleading. &foo takes the address of foo and passes it as a value to getAllFoos(). No references involved.

              – Swordfish
              Nov 21 '18 at 8:50











            • So the answer was to simply say that "If it was NULL and the function dreferenced it -- then that would cause problems? I'll give it the benefit of the doubt, but I read the answer so say "NO you can't pass *foos as a NULL pointer. So I guess at best it was unclear either way. (I didn't ding for it -- so no harm there)

              – David C. Rankin
              Nov 21 '18 at 8:51








            • 2





              I see it the other way now, thanks @Swordfish, above the horizontal he is talking about struct Foo *foo = NULL;, below he is taking about an initial setting of struct Foo **foo = NULL; (and that is presumably in the caller) and then yes, I agree in part, NOT that "FOO * should point to valid memory." but that FOO * should have a valid address." (i.e. that it is a valid declared pointer itself)

              – David C. Rankin
              Nov 21 '18 at 8:55













            • All we came up with is speculation about OPs function at best. For all we know the function could set the passed pointer to the first node of a linked list.

              – Swordfish
              Nov 21 '18 at 8:59














            • 1





              @DavidC.Rankin I believe what kiranBiradar tried to say with FOO * should point to valid memory. is that the parameter foo derecerenced once shouldn't be NULL.

              – Swordfish
              Nov 21 '18 at 8:47













            • Maybe you can reformulate your answer so it is clearer. Also, passing reference is a little misleading. &foo takes the address of foo and passes it as a value to getAllFoos(). No references involved.

              – Swordfish
              Nov 21 '18 at 8:50











            • So the answer was to simply say that "If it was NULL and the function dreferenced it -- then that would cause problems? I'll give it the benefit of the doubt, but I read the answer so say "NO you can't pass *foos as a NULL pointer. So I guess at best it was unclear either way. (I didn't ding for it -- so no harm there)

              – David C. Rankin
              Nov 21 '18 at 8:51








            • 2





              I see it the other way now, thanks @Swordfish, above the horizontal he is talking about struct Foo *foo = NULL;, below he is taking about an initial setting of struct Foo **foo = NULL; (and that is presumably in the caller) and then yes, I agree in part, NOT that "FOO * should point to valid memory." but that FOO * should have a valid address." (i.e. that it is a valid declared pointer itself)

              – David C. Rankin
              Nov 21 '18 at 8:55













            • All we came up with is speculation about OPs function at best. For all we know the function could set the passed pointer to the first node of a linked list.

              – Swordfish
              Nov 21 '18 at 8:59








            1




            1





            @DavidC.Rankin I believe what kiranBiradar tried to say with FOO * should point to valid memory. is that the parameter foo derecerenced once shouldn't be NULL.

            – Swordfish
            Nov 21 '18 at 8:47







            @DavidC.Rankin I believe what kiranBiradar tried to say with FOO * should point to valid memory. is that the parameter foo derecerenced once shouldn't be NULL.

            – Swordfish
            Nov 21 '18 at 8:47















            Maybe you can reformulate your answer so it is clearer. Also, passing reference is a little misleading. &foo takes the address of foo and passes it as a value to getAllFoos(). No references involved.

            – Swordfish
            Nov 21 '18 at 8:50





            Maybe you can reformulate your answer so it is clearer. Also, passing reference is a little misleading. &foo takes the address of foo and passes it as a value to getAllFoos(). No references involved.

            – Swordfish
            Nov 21 '18 at 8:50













            So the answer was to simply say that "If it was NULL and the function dreferenced it -- then that would cause problems? I'll give it the benefit of the doubt, but I read the answer so say "NO you can't pass *foos as a NULL pointer. So I guess at best it was unclear either way. (I didn't ding for it -- so no harm there)

            – David C. Rankin
            Nov 21 '18 at 8:51







            So the answer was to simply say that "If it was NULL and the function dreferenced it -- then that would cause problems? I'll give it the benefit of the doubt, but I read the answer so say "NO you can't pass *foos as a NULL pointer. So I guess at best it was unclear either way. (I didn't ding for it -- so no harm there)

            – David C. Rankin
            Nov 21 '18 at 8:51






            2




            2





            I see it the other way now, thanks @Swordfish, above the horizontal he is talking about struct Foo *foo = NULL;, below he is taking about an initial setting of struct Foo **foo = NULL; (and that is presumably in the caller) and then yes, I agree in part, NOT that "FOO * should point to valid memory." but that FOO * should have a valid address." (i.e. that it is a valid declared pointer itself)

            – David C. Rankin
            Nov 21 '18 at 8:55







            I see it the other way now, thanks @Swordfish, above the horizontal he is talking about struct Foo *foo = NULL;, below he is taking about an initial setting of struct Foo **foo = NULL; (and that is presumably in the caller) and then yes, I agree in part, NOT that "FOO * should point to valid memory." but that FOO * should have a valid address." (i.e. that it is a valid declared pointer itself)

            – David C. Rankin
            Nov 21 '18 at 8:55















            All we came up with is speculation about OPs function at best. For all we know the function could set the passed pointer to the first node of a linked list.

            – Swordfish
            Nov 21 '18 at 8:59





            All we came up with is speculation about OPs function at best. For all we know the function could set the passed pointer to the first node of a linked list.

            – Swordfish
            Nov 21 '18 at 8:59


















            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407957%2fpointer-to-pointer-as-function-parameter-for-a-getter%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

            mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

            How to change which sound is reproduced for terminal bell?

            Can I use Tabulator js library in my java Spring + Thymeleaf project?