Is this function re-entrant?












0















DNM_Manager.c



struct DNM_s
{
uint32_t Addr;
uint32_t SerialNum;
uint32_t SubnetMask;
uint16_t Tick;
uint8_t Name[NAME_SIZE];
}DNMTemp;

pDNM DNMManager_New(void) //reentrant?
{
return &DNMTemp;
}


GeeksForGeeks says: To be reentrant, the function may not use global and static data.



I do use global data "DNMTemp" in this case. However, DNMTemp's value is not changed and function only return its address.










share|improve this question




















  • 3





    This function is reentrant, but whatever you call it from probably isn't.

    – Matt Timmermans
    Jan 19 at 3:40











  • I don't understand it. Can you give me an example?

    – Andy Lin
    Jan 19 at 4:04






  • 1





    You are handing out the same pointer for every call, so everyone who calls it will be accessing the same object. Whatever function calls this one, it will probably use that pointer for something that could be messed up by a reentrant call to the same function.

    – Matt Timmermans
    Jan 19 at 4:17











  • What global data do you think it uses? It doesn't use the value of DNMTemp.

    – David Schwartz
    Jan 19 at 8:58
















0















DNM_Manager.c



struct DNM_s
{
uint32_t Addr;
uint32_t SerialNum;
uint32_t SubnetMask;
uint16_t Tick;
uint8_t Name[NAME_SIZE];
}DNMTemp;

pDNM DNMManager_New(void) //reentrant?
{
return &DNMTemp;
}


GeeksForGeeks says: To be reentrant, the function may not use global and static data.



I do use global data "DNMTemp" in this case. However, DNMTemp's value is not changed and function only return its address.










share|improve this question




















  • 3





    This function is reentrant, but whatever you call it from probably isn't.

    – Matt Timmermans
    Jan 19 at 3:40











  • I don't understand it. Can you give me an example?

    – Andy Lin
    Jan 19 at 4:04






  • 1





    You are handing out the same pointer for every call, so everyone who calls it will be accessing the same object. Whatever function calls this one, it will probably use that pointer for something that could be messed up by a reentrant call to the same function.

    – Matt Timmermans
    Jan 19 at 4:17











  • What global data do you think it uses? It doesn't use the value of DNMTemp.

    – David Schwartz
    Jan 19 at 8:58














0












0








0








DNM_Manager.c



struct DNM_s
{
uint32_t Addr;
uint32_t SerialNum;
uint32_t SubnetMask;
uint16_t Tick;
uint8_t Name[NAME_SIZE];
}DNMTemp;

pDNM DNMManager_New(void) //reentrant?
{
return &DNMTemp;
}


GeeksForGeeks says: To be reentrant, the function may not use global and static data.



I do use global data "DNMTemp" in this case. However, DNMTemp's value is not changed and function only return its address.










share|improve this question
















DNM_Manager.c



struct DNM_s
{
uint32_t Addr;
uint32_t SerialNum;
uint32_t SubnetMask;
uint16_t Tick;
uint8_t Name[NAME_SIZE];
}DNMTemp;

pDNM DNMManager_New(void) //reentrant?
{
return &DNMTemp;
}


GeeksForGeeks says: To be reentrant, the function may not use global and static data.



I do use global data "DNMTemp" in this case. However, DNMTemp's value is not changed and function only return its address.







c reentrancy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 19 at 3:30







Andy Lin

















asked Jan 19 at 3:25









Andy LinAndy Lin

517




517








  • 3





    This function is reentrant, but whatever you call it from probably isn't.

    – Matt Timmermans
    Jan 19 at 3:40











  • I don't understand it. Can you give me an example?

    – Andy Lin
    Jan 19 at 4:04






  • 1





    You are handing out the same pointer for every call, so everyone who calls it will be accessing the same object. Whatever function calls this one, it will probably use that pointer for something that could be messed up by a reentrant call to the same function.

    – Matt Timmermans
    Jan 19 at 4:17











  • What global data do you think it uses? It doesn't use the value of DNMTemp.

    – David Schwartz
    Jan 19 at 8:58














  • 3





    This function is reentrant, but whatever you call it from probably isn't.

    – Matt Timmermans
    Jan 19 at 3:40











  • I don't understand it. Can you give me an example?

    – Andy Lin
    Jan 19 at 4:04






  • 1





    You are handing out the same pointer for every call, so everyone who calls it will be accessing the same object. Whatever function calls this one, it will probably use that pointer for something that could be messed up by a reentrant call to the same function.

    – Matt Timmermans
    Jan 19 at 4:17











  • What global data do you think it uses? It doesn't use the value of DNMTemp.

    – David Schwartz
    Jan 19 at 8:58








3




3





This function is reentrant, but whatever you call it from probably isn't.

– Matt Timmermans
Jan 19 at 3:40





This function is reentrant, but whatever you call it from probably isn't.

– Matt Timmermans
Jan 19 at 3:40













I don't understand it. Can you give me an example?

– Andy Lin
Jan 19 at 4:04





I don't understand it. Can you give me an example?

– Andy Lin
Jan 19 at 4:04




1




1





You are handing out the same pointer for every call, so everyone who calls it will be accessing the same object. Whatever function calls this one, it will probably use that pointer for something that could be messed up by a reentrant call to the same function.

– Matt Timmermans
Jan 19 at 4:17





You are handing out the same pointer for every call, so everyone who calls it will be accessing the same object. Whatever function calls this one, it will probably use that pointer for something that could be messed up by a reentrant call to the same function.

– Matt Timmermans
Jan 19 at 4:17













What global data do you think it uses? It doesn't use the value of DNMTemp.

– David Schwartz
Jan 19 at 8:58





What global data do you think it uses? It doesn't use the value of DNMTemp.

– David Schwartz
Jan 19 at 8:58












2 Answers
2






active

oldest

votes


















2














Yes.



From the tag excerpt of reentrancy:




A subroutine is considered reentrant if it can be safely called before a previous call has completed.




In your case, since the function only returns the address of a global (static) variable, which should remain constant after the program starts, the function is well re-entrant.



IMO, reentrant function can access global and static data, without changing any, so obtaining the address of a global variable isn't bad for a reentrant function.






share|improve this answer
























  • To your IMO, if global/static data is being changed then what happens?

    – Andy Lin
    Jan 19 at 6:16











  • @AndyLin Consider this minimal example: f() { return ++x; } If it's called concurrently by two threads, the result may possibly be both threads getting x+2 as the result, when one of them would be expecting x+1.

    – iBug
    Jan 19 at 6:53











  • I meant, although reentrant function doesn't change data, but may reads data that is being changed(and still not complete) by other thread and result in wrong reading.

    – Andy Lin
    Jan 19 at 7:05













  • @AndyLin If the data is changed by something other than this function itself, it doesn't affect the reentrancy of this very function.

    – iBug
    Jan 19 at 7:10



















1














When considering the if the function is reentrant or not the example has to be a bit less trivial than this one.



pDNM DNMManager_New(void)    //reentrant?
{
return &DNMTemp;
}


But the reference to the DNMTemp (its address) will remain the same during the program execution so this function is reentrant.



But if you access any real data it will not.



uint32_t DNMManager_read(void)    //reentrant?
{
return SerialNum;
}





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%2f54263819%2fis-this-function-re-entrant%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Yes.



    From the tag excerpt of reentrancy:




    A subroutine is considered reentrant if it can be safely called before a previous call has completed.




    In your case, since the function only returns the address of a global (static) variable, which should remain constant after the program starts, the function is well re-entrant.



    IMO, reentrant function can access global and static data, without changing any, so obtaining the address of a global variable isn't bad for a reentrant function.






    share|improve this answer
























    • To your IMO, if global/static data is being changed then what happens?

      – Andy Lin
      Jan 19 at 6:16











    • @AndyLin Consider this minimal example: f() { return ++x; } If it's called concurrently by two threads, the result may possibly be both threads getting x+2 as the result, when one of them would be expecting x+1.

      – iBug
      Jan 19 at 6:53











    • I meant, although reentrant function doesn't change data, but may reads data that is being changed(and still not complete) by other thread and result in wrong reading.

      – Andy Lin
      Jan 19 at 7:05













    • @AndyLin If the data is changed by something other than this function itself, it doesn't affect the reentrancy of this very function.

      – iBug
      Jan 19 at 7:10
















    2














    Yes.



    From the tag excerpt of reentrancy:




    A subroutine is considered reentrant if it can be safely called before a previous call has completed.




    In your case, since the function only returns the address of a global (static) variable, which should remain constant after the program starts, the function is well re-entrant.



    IMO, reentrant function can access global and static data, without changing any, so obtaining the address of a global variable isn't bad for a reentrant function.






    share|improve this answer
























    • To your IMO, if global/static data is being changed then what happens?

      – Andy Lin
      Jan 19 at 6:16











    • @AndyLin Consider this minimal example: f() { return ++x; } If it's called concurrently by two threads, the result may possibly be both threads getting x+2 as the result, when one of them would be expecting x+1.

      – iBug
      Jan 19 at 6:53











    • I meant, although reentrant function doesn't change data, but may reads data that is being changed(and still not complete) by other thread and result in wrong reading.

      – Andy Lin
      Jan 19 at 7:05













    • @AndyLin If the data is changed by something other than this function itself, it doesn't affect the reentrancy of this very function.

      – iBug
      Jan 19 at 7:10














    2












    2








    2







    Yes.



    From the tag excerpt of reentrancy:




    A subroutine is considered reentrant if it can be safely called before a previous call has completed.




    In your case, since the function only returns the address of a global (static) variable, which should remain constant after the program starts, the function is well re-entrant.



    IMO, reentrant function can access global and static data, without changing any, so obtaining the address of a global variable isn't bad for a reentrant function.






    share|improve this answer













    Yes.



    From the tag excerpt of reentrancy:




    A subroutine is considered reentrant if it can be safely called before a previous call has completed.




    In your case, since the function only returns the address of a global (static) variable, which should remain constant after the program starts, the function is well re-entrant.



    IMO, reentrant function can access global and static data, without changing any, so obtaining the address of a global variable isn't bad for a reentrant function.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 19 at 3:28









    iBugiBug

    20.4k63864




    20.4k63864













    • To your IMO, if global/static data is being changed then what happens?

      – Andy Lin
      Jan 19 at 6:16











    • @AndyLin Consider this minimal example: f() { return ++x; } If it's called concurrently by two threads, the result may possibly be both threads getting x+2 as the result, when one of them would be expecting x+1.

      – iBug
      Jan 19 at 6:53











    • I meant, although reentrant function doesn't change data, but may reads data that is being changed(and still not complete) by other thread and result in wrong reading.

      – Andy Lin
      Jan 19 at 7:05













    • @AndyLin If the data is changed by something other than this function itself, it doesn't affect the reentrancy of this very function.

      – iBug
      Jan 19 at 7:10



















    • To your IMO, if global/static data is being changed then what happens?

      – Andy Lin
      Jan 19 at 6:16











    • @AndyLin Consider this minimal example: f() { return ++x; } If it's called concurrently by two threads, the result may possibly be both threads getting x+2 as the result, when one of them would be expecting x+1.

      – iBug
      Jan 19 at 6:53











    • I meant, although reentrant function doesn't change data, but may reads data that is being changed(and still not complete) by other thread and result in wrong reading.

      – Andy Lin
      Jan 19 at 7:05













    • @AndyLin If the data is changed by something other than this function itself, it doesn't affect the reentrancy of this very function.

      – iBug
      Jan 19 at 7:10

















    To your IMO, if global/static data is being changed then what happens?

    – Andy Lin
    Jan 19 at 6:16





    To your IMO, if global/static data is being changed then what happens?

    – Andy Lin
    Jan 19 at 6:16













    @AndyLin Consider this minimal example: f() { return ++x; } If it's called concurrently by two threads, the result may possibly be both threads getting x+2 as the result, when one of them would be expecting x+1.

    – iBug
    Jan 19 at 6:53





    @AndyLin Consider this minimal example: f() { return ++x; } If it's called concurrently by two threads, the result may possibly be both threads getting x+2 as the result, when one of them would be expecting x+1.

    – iBug
    Jan 19 at 6:53













    I meant, although reentrant function doesn't change data, but may reads data that is being changed(and still not complete) by other thread and result in wrong reading.

    – Andy Lin
    Jan 19 at 7:05







    I meant, although reentrant function doesn't change data, but may reads data that is being changed(and still not complete) by other thread and result in wrong reading.

    – Andy Lin
    Jan 19 at 7:05















    @AndyLin If the data is changed by something other than this function itself, it doesn't affect the reentrancy of this very function.

    – iBug
    Jan 19 at 7:10





    @AndyLin If the data is changed by something other than this function itself, it doesn't affect the reentrancy of this very function.

    – iBug
    Jan 19 at 7:10













    1














    When considering the if the function is reentrant or not the example has to be a bit less trivial than this one.



    pDNM DNMManager_New(void)    //reentrant?
    {
    return &DNMTemp;
    }


    But the reference to the DNMTemp (its address) will remain the same during the program execution so this function is reentrant.



    But if you access any real data it will not.



    uint32_t DNMManager_read(void)    //reentrant?
    {
    return SerialNum;
    }





    share|improve this answer




























      1














      When considering the if the function is reentrant or not the example has to be a bit less trivial than this one.



      pDNM DNMManager_New(void)    //reentrant?
      {
      return &DNMTemp;
      }


      But the reference to the DNMTemp (its address) will remain the same during the program execution so this function is reentrant.



      But if you access any real data it will not.



      uint32_t DNMManager_read(void)    //reentrant?
      {
      return SerialNum;
      }





      share|improve this answer


























        1












        1








        1







        When considering the if the function is reentrant or not the example has to be a bit less trivial than this one.



        pDNM DNMManager_New(void)    //reentrant?
        {
        return &DNMTemp;
        }


        But the reference to the DNMTemp (its address) will remain the same during the program execution so this function is reentrant.



        But if you access any real data it will not.



        uint32_t DNMManager_read(void)    //reentrant?
        {
        return SerialNum;
        }





        share|improve this answer













        When considering the if the function is reentrant or not the example has to be a bit less trivial than this one.



        pDNM DNMManager_New(void)    //reentrant?
        {
        return &DNMTemp;
        }


        But the reference to the DNMTemp (its address) will remain the same during the program execution so this function is reentrant.



        But if you access any real data it will not.



        uint32_t DNMManager_read(void)    //reentrant?
        {
        return SerialNum;
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 19 at 8:50









        P__J__P__J__

        10.2k2723




        10.2k2723






























            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%2f54263819%2fis-this-function-re-entrant%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

            Liquibase includeAll doesn't find base path

            How to use setInterval in EJS file?

            Petrus Granier-Deferre