Andy Lin
Andy Lin

Reputation: 461

Is this function re-entrant?

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.

Upvotes: 0

Views: 116

Answers (2)

0___________
0___________

Reputation: 68069

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

Upvotes: 1

iBug
iBug

Reputation: 37317

Yes.

From the tag excerpt of :

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.

Upvotes: 2

Related Questions