wilx
wilx

Reputation: 18238

How do I use _vsnprintf_r() correctly?

I am trying to learn something about re-entrant calls and I am trying to use _vsnprintf_r(). The first parameter seems to be some struct _reent *. On Cygwin, it seems that I could just use it like this: _vsnprintf_r (_REENT, /* rest of the arguments here */). Is this correct? Is this correct even outside Cygwin? There seems to be certain lack of documentation for this. My Google and Bing searches were not too fruitful.

Upvotes: 0

Views: 557

Answers (2)

jxh
jxh

Reputation: 70472

_vsnprintf_r is not a standard function. It was created by the cygwin development team for internal use.

The _vsnprintf_r is called by the regular vsnprintf, so I would stick with using the regular C version of this call on cygwin. There is no guarantee from cygwin that the _vsnprintf_r is reentrant anyway, especially if you cannot locate any documentation about it.

Since cygwin uses a dynamically loaded library that has shared state among all processes that link against it, it uses the _<xxx>_r versions of the calls to give each process its own instances of state that would normally be the default on a standard UNIX system. Since these functions are used by cygwin internally, you may find the cygwin-developers mailing list a better resource for your question.

This is in contrast to reentrant functions defined by POSIX (like gmtime_r). The non-reentrant versions of those returned pointers to static memory within the library, so reentrant versions were created to allow the function to use memory passed in by the caller instead.

Upvotes: 2

Josh
Josh

Reputation: 144

You probably don't want to "print" anything with re-entrant calls, cause you don't know what order or when things will be "printing".

In general terms, to write re-entrant functions, pass everything into the function (no static variables). Local (stack) variables are okay AFAIK.

Printing is a "classic" problem for semaphores (or some other locking mechanism). Otherwise,

Hello World

becomes

HeWlorllod

or at best

HellWorldo

Upvotes: -1

Related Questions