English 中文(简体)
Why inet_ntoa is designed to be a non-reentrant function?
原标题:

Glancing at the source code of GNU C Library,I found the inet_ntoa is implementated with

static __thread char buffer[18]

My question is, since there is a need to use reeentrant inet_ntoa,why do not the author of GNU C Library use malloc to implementate it?

thanks.

最佳回答

The reason it s not using the heap is to conform with standards (POSIX) and other systems. The interface is just not such that you are supposed to free the buffer returned. It assumes static storage..

But by declaring it as thread local (with __thread), two threads do not conflict with each other if they happen to both be calling the function. This is glibc s workaround for the brokenness of the interface.

It s true that this is not re-entrant or consistent with the spirit of that term. If you have a recursive function that calls it, you cannot rely on the buffer being the same between calls. But it can be used by multiple threads, which often is good enough.

EDIT: By the way, I just remembered, there is a newer version of this function that uses a caller-provided buffer. See inet_ntop().

问题回答

暂无回答




相关问题
Max Outgoing Socket Connections in .NET/Windows Server

I have a slightly unusual situation where I m needing to maintain CLIENT tcp connections to another server for thousands of mobile users on my servers (basically the mobile devices connect to my ...

Determine the remaining TTL of a multicast packet in Java

If I have a Java program that listens to multicast data, how can I tell what the remaining TTL of the incoming datagram is? I know how to set the TTL on the sending multicast socket, but I want to ...

Python socket not receiving anything

I m trying to receive a variable length stream from a camera with python, but get weird behaviour. This is Python 2.6.4 (r264:75706) on linux(Ubuntu 9.10) The message is supposed to come with a ...

Connecting to a Source game server in VB.NET

I m developing an application that detects Source-based games running on the LAN. Following up on the specifications provided by Valve, I have it narrowed down to exactly what I want: making a UDP ...

What can cause select to block in Python?

Here s a snippet of code I m using in a loop: while True: print loop rlist, wlist, xlist = select.select(readers, [], [], TIMEOUT) print selected # do stuff At a certain point, ...

Socket.UseOnlyOverlappedIO?

Does anyone know what Socket.UseOnlyOverlappedIO does, and if so, does it affect performance? There seems to be no real explanation on MSDN. Thanks in advance.

热门标签