ip4addr_ntoa and non-reentrant functions

In the network, there is a function that converts an IP address to an ASCII string. The ASCII string pointed to by the return value of this function resides in static memory, so the function is not reentrant.

In layman's terms, in a multitasking system, when a task executes a call to run this function, other tasks cannot call and run this function. If you do this, then the return value of the function of the second call is the same. times value.

char*
ip4addr_ntoa(const ip4_addr_t *addr)
{
  static char str[IP4ADDR_STRLEN_MAX];
  return ip4addr_ntoa_r(addr, str, IP4ADDR_STRLEN_MAX);
}

The above also involves a problem, the function parameter is the difference between a structure and a structure pointer

When a structure is used as a function parameter, it is a formal parameter, so the value in the structure cannot be changed.

When a structure pointer is used as a function parameter, it is an actual parameter and can change the value in the structure.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324699384&siteId=291194637