How to know if a function will be called for the last time












-2















I'm working on a self-made malloc(), realloc() and free() for a school project.



I would like to know if there is a way for my function free() to know if it's his last call in the program.



Edit : I don't have main() under control, it's for a library.



I would like to use sbrk() to free just one time at the end, because for now i'm calling it after all free() and it's not optimised.



(I deleted the piece of code because i don't want to be strike by my school)



Seems that it's not possible.. Thanks for your reply.










share|improve this question




















  • 5





    What are you trying to achieve with that knowledge?

    – Fildor
    Nov 15 '18 at 12:26






  • 4





    Sounds equivalent to the Halting Problem.

    – aschepler
    Nov 15 '18 at 12:28






  • 2





    Not really sure what you're asking, or why you'd need to know that. Do you mean that if you have free(); if(rand()%2) free(); the first free() should somehow know if the second one will be called or not?

    – JJJ
    Nov 15 '18 at 12:31






  • 1





    Knowing the "last potential call" is practically worthless – either the cleanup code will sometimes not be called, which leads to bugs that are hard to debug, or if it doesn't matter if the cleanup code is sometimes not called, why include it at all?

    – JJJ
    Nov 15 '18 at 12:43








  • 1





    @JJJ Even worse, what if the "last call" isn't? For example, an exit() handler registered with atexit() calls free() after the supposed-to-be "last" call.

    – Andrew Henle
    Nov 15 '18 at 12:48
















-2















I'm working on a self-made malloc(), realloc() and free() for a school project.



I would like to know if there is a way for my function free() to know if it's his last call in the program.



Edit : I don't have main() under control, it's for a library.



I would like to use sbrk() to free just one time at the end, because for now i'm calling it after all free() and it's not optimised.



(I deleted the piece of code because i don't want to be strike by my school)



Seems that it's not possible.. Thanks for your reply.










share|improve this question




















  • 5





    What are you trying to achieve with that knowledge?

    – Fildor
    Nov 15 '18 at 12:26






  • 4





    Sounds equivalent to the Halting Problem.

    – aschepler
    Nov 15 '18 at 12:28






  • 2





    Not really sure what you're asking, or why you'd need to know that. Do you mean that if you have free(); if(rand()%2) free(); the first free() should somehow know if the second one will be called or not?

    – JJJ
    Nov 15 '18 at 12:31






  • 1





    Knowing the "last potential call" is practically worthless – either the cleanup code will sometimes not be called, which leads to bugs that are hard to debug, or if it doesn't matter if the cleanup code is sometimes not called, why include it at all?

    – JJJ
    Nov 15 '18 at 12:43








  • 1





    @JJJ Even worse, what if the "last call" isn't? For example, an exit() handler registered with atexit() calls free() after the supposed-to-be "last" call.

    – Andrew Henle
    Nov 15 '18 at 12:48














-2












-2








-2








I'm working on a self-made malloc(), realloc() and free() for a school project.



I would like to know if there is a way for my function free() to know if it's his last call in the program.



Edit : I don't have main() under control, it's for a library.



I would like to use sbrk() to free just one time at the end, because for now i'm calling it after all free() and it's not optimised.



(I deleted the piece of code because i don't want to be strike by my school)



Seems that it's not possible.. Thanks for your reply.










share|improve this question
















I'm working on a self-made malloc(), realloc() and free() for a school project.



I would like to know if there is a way for my function free() to know if it's his last call in the program.



Edit : I don't have main() under control, it's for a library.



I would like to use sbrk() to free just one time at the end, because for now i'm calling it after all free() and it's not optimised.



(I deleted the piece of code because i don't want to be strike by my school)



Seems that it's not possible.. Thanks for your reply.







c






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 12:50







William Choukroun

















asked Nov 15 '18 at 12:25









William ChoukrounWilliam Choukroun

11




11








  • 5





    What are you trying to achieve with that knowledge?

    – Fildor
    Nov 15 '18 at 12:26






  • 4





    Sounds equivalent to the Halting Problem.

    – aschepler
    Nov 15 '18 at 12:28






  • 2





    Not really sure what you're asking, or why you'd need to know that. Do you mean that if you have free(); if(rand()%2) free(); the first free() should somehow know if the second one will be called or not?

    – JJJ
    Nov 15 '18 at 12:31






  • 1





    Knowing the "last potential call" is practically worthless – either the cleanup code will sometimes not be called, which leads to bugs that are hard to debug, or if it doesn't matter if the cleanup code is sometimes not called, why include it at all?

    – JJJ
    Nov 15 '18 at 12:43








  • 1





    @JJJ Even worse, what if the "last call" isn't? For example, an exit() handler registered with atexit() calls free() after the supposed-to-be "last" call.

    – Andrew Henle
    Nov 15 '18 at 12:48














  • 5





    What are you trying to achieve with that knowledge?

    – Fildor
    Nov 15 '18 at 12:26






  • 4





    Sounds equivalent to the Halting Problem.

    – aschepler
    Nov 15 '18 at 12:28






  • 2





    Not really sure what you're asking, or why you'd need to know that. Do you mean that if you have free(); if(rand()%2) free(); the first free() should somehow know if the second one will be called or not?

    – JJJ
    Nov 15 '18 at 12:31






  • 1





    Knowing the "last potential call" is practically worthless – either the cleanup code will sometimes not be called, which leads to bugs that are hard to debug, or if it doesn't matter if the cleanup code is sometimes not called, why include it at all?

    – JJJ
    Nov 15 '18 at 12:43








  • 1





    @JJJ Even worse, what if the "last call" isn't? For example, an exit() handler registered with atexit() calls free() after the supposed-to-be "last" call.

    – Andrew Henle
    Nov 15 '18 at 12:48








5




5





What are you trying to achieve with that knowledge?

– Fildor
Nov 15 '18 at 12:26





What are you trying to achieve with that knowledge?

– Fildor
Nov 15 '18 at 12:26




4




4





Sounds equivalent to the Halting Problem.

– aschepler
Nov 15 '18 at 12:28





Sounds equivalent to the Halting Problem.

– aschepler
Nov 15 '18 at 12:28




2




2





Not really sure what you're asking, or why you'd need to know that. Do you mean that if you have free(); if(rand()%2) free(); the first free() should somehow know if the second one will be called or not?

– JJJ
Nov 15 '18 at 12:31





Not really sure what you're asking, or why you'd need to know that. Do you mean that if you have free(); if(rand()%2) free(); the first free() should somehow know if the second one will be called or not?

– JJJ
Nov 15 '18 at 12:31




1




1





Knowing the "last potential call" is practically worthless – either the cleanup code will sometimes not be called, which leads to bugs that are hard to debug, or if it doesn't matter if the cleanup code is sometimes not called, why include it at all?

– JJJ
Nov 15 '18 at 12:43







Knowing the "last potential call" is practically worthless – either the cleanup code will sometimes not be called, which leads to bugs that are hard to debug, or if it doesn't matter if the cleanup code is sometimes not called, why include it at all?

– JJJ
Nov 15 '18 at 12:43






1




1





@JJJ Even worse, what if the "last call" isn't? For example, an exit() handler registered with atexit() calls free() after the supposed-to-be "last" call.

– Andrew Henle
Nov 15 '18 at 12:48





@JJJ Even worse, what if the "last call" isn't? For example, an exit() handler registered with atexit() calls free() after the supposed-to-be "last" call.

– Andrew Henle
Nov 15 '18 at 12:48












3 Answers
3






active

oldest

votes


















0














No, there is, in general, no way to know whether a function is being called for the last time in a program execution.



A program could be designed to convey this knowledge, by passing an argument indicating it. You would have to build this into all functions in the call chain. Even so, it may cause some code to be awkward, as the last iterations of some loops may need special treatment.



Generally, it is preferable to call separate clean-up code at suitable times rather than trying to incorporate it automatically into routine operations in the way you seem to be attempting.






share|improve this answer































    0














    No you cannot.



    Example :




    • A GUI will call your function every time the user clicks "ok".

    • There is no way for the program to known if the user will click again or switch-off the computer after a click.


    A little joke:
    The only possiblity for you, to achieve what you want, is have your function free() abort the whole program, so that you are sure it will not be called again.






    share|improve this answer































      0














      No, it's not possible, as other answers have stated.



      The closest thing you can do is register an atexit handler to perform whatever cleanup you want to do:



      static int init = 0;

      void cleanup()
      {
      // last call to sbrk to clean up
      }

      void *my_malloc(size_t size)
      {
      if (!init) {
      init = 1;
      atexit(cleanup);
      }
      ...
      }





      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',
        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%2f53319480%2fhow-to-know-if-a-function-will-be-called-for-the-last-time%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









        0














        No, there is, in general, no way to know whether a function is being called for the last time in a program execution.



        A program could be designed to convey this knowledge, by passing an argument indicating it. You would have to build this into all functions in the call chain. Even so, it may cause some code to be awkward, as the last iterations of some loops may need special treatment.



        Generally, it is preferable to call separate clean-up code at suitable times rather than trying to incorporate it automatically into routine operations in the way you seem to be attempting.






        share|improve this answer




























          0














          No, there is, in general, no way to know whether a function is being called for the last time in a program execution.



          A program could be designed to convey this knowledge, by passing an argument indicating it. You would have to build this into all functions in the call chain. Even so, it may cause some code to be awkward, as the last iterations of some loops may need special treatment.



          Generally, it is preferable to call separate clean-up code at suitable times rather than trying to incorporate it automatically into routine operations in the way you seem to be attempting.






          share|improve this answer


























            0












            0








            0







            No, there is, in general, no way to know whether a function is being called for the last time in a program execution.



            A program could be designed to convey this knowledge, by passing an argument indicating it. You would have to build this into all functions in the call chain. Even so, it may cause some code to be awkward, as the last iterations of some loops may need special treatment.



            Generally, it is preferable to call separate clean-up code at suitable times rather than trying to incorporate it automatically into routine operations in the way you seem to be attempting.






            share|improve this answer













            No, there is, in general, no way to know whether a function is being called for the last time in a program execution.



            A program could be designed to convey this knowledge, by passing an argument indicating it. You would have to build this into all functions in the call chain. Even so, it may cause some code to be awkward, as the last iterations of some loops may need special treatment.



            Generally, it is preferable to call separate clean-up code at suitable times rather than trying to incorporate it automatically into routine operations in the way you seem to be attempting.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 15 '18 at 12:35









            Eric PostpischilEric Postpischil

            78.2k883166




            78.2k883166

























                0














                No you cannot.



                Example :




                • A GUI will call your function every time the user clicks "ok".

                • There is no way for the program to known if the user will click again or switch-off the computer after a click.


                A little joke:
                The only possiblity for you, to achieve what you want, is have your function free() abort the whole program, so that you are sure it will not be called again.






                share|improve this answer




























                  0














                  No you cannot.



                  Example :




                  • A GUI will call your function every time the user clicks "ok".

                  • There is no way for the program to known if the user will click again or switch-off the computer after a click.


                  A little joke:
                  The only possiblity for you, to achieve what you want, is have your function free() abort the whole program, so that you are sure it will not be called again.






                  share|improve this answer


























                    0












                    0








                    0







                    No you cannot.



                    Example :




                    • A GUI will call your function every time the user clicks "ok".

                    • There is no way for the program to known if the user will click again or switch-off the computer after a click.


                    A little joke:
                    The only possiblity for you, to achieve what you want, is have your function free() abort the whole program, so that you are sure it will not be called again.






                    share|improve this answer













                    No you cannot.



                    Example :




                    • A GUI will call your function every time the user clicks "ok".

                    • There is no way for the program to known if the user will click again or switch-off the computer after a click.


                    A little joke:
                    The only possiblity for you, to achieve what you want, is have your function free() abort the whole program, so that you are sure it will not be called again.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 15 '18 at 12:38









                    user803422user803422

                    9472623




                    9472623























                        0














                        No, it's not possible, as other answers have stated.



                        The closest thing you can do is register an atexit handler to perform whatever cleanup you want to do:



                        static int init = 0;

                        void cleanup()
                        {
                        // last call to sbrk to clean up
                        }

                        void *my_malloc(size_t size)
                        {
                        if (!init) {
                        init = 1;
                        atexit(cleanup);
                        }
                        ...
                        }





                        share|improve this answer




























                          0














                          No, it's not possible, as other answers have stated.



                          The closest thing you can do is register an atexit handler to perform whatever cleanup you want to do:



                          static int init = 0;

                          void cleanup()
                          {
                          // last call to sbrk to clean up
                          }

                          void *my_malloc(size_t size)
                          {
                          if (!init) {
                          init = 1;
                          atexit(cleanup);
                          }
                          ...
                          }





                          share|improve this answer


























                            0












                            0








                            0







                            No, it's not possible, as other answers have stated.



                            The closest thing you can do is register an atexit handler to perform whatever cleanup you want to do:



                            static int init = 0;

                            void cleanup()
                            {
                            // last call to sbrk to clean up
                            }

                            void *my_malloc(size_t size)
                            {
                            if (!init) {
                            init = 1;
                            atexit(cleanup);
                            }
                            ...
                            }





                            share|improve this answer













                            No, it's not possible, as other answers have stated.



                            The closest thing you can do is register an atexit handler to perform whatever cleanup you want to do:



                            static int init = 0;

                            void cleanup()
                            {
                            // last call to sbrk to clean up
                            }

                            void *my_malloc(size_t size)
                            {
                            if (!init) {
                            init = 1;
                            atexit(cleanup);
                            }
                            ...
                            }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 15 '18 at 12:57









                            dbushdbush

                            101k13105142




                            101k13105142






























                                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%2f53319480%2fhow-to-know-if-a-function-will-be-called-for-the-last-time%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

                                Bressuire

                                Vorschmack

                                Quarantine