[libvirt] how to print size_t in LGPLv2+ program

Bruno Haible bruno at clisp.org
Tue Aug 17 22:33:33 UTC 2010


Hi Eric,

> My understanding is that on 64-bit windows,
> sizeof(long)==4 but sizeof(void*)==8; and ... sizeof(size_t) is also 8.

Yes, correct.

> Which means you _can't_ use "%lu",(unsigned long)size_t_val.

You _can_ use this. It will work as long as each of your program's data
structures is less than 4 GB large. Remember that size_t values get
larger than 4 GB only if you have a memory object (array) that is larger
than 4 GB.

> What a shame that POSIX omitted an <inttypes.h> PRIu* for size_t.

You can define it by yourself: Basically you define

  #if @BITSIZEOF_SIZE_T@ == 32
  # define PRIuSIZE PRIu32
  #endif
  #if @BITSIZEOF_SIZE_T@ == 64
  # define PRIuSIZE PRIu64
  #endif

This will work with mingw's and msvc's native printf, because gnulib's
<inttypes.h> replacement defines PRIu64 to "I64u", and the native printf
supports %I64u directives.

Note that this will not work inside gettext() arguments, though, because
PRIuSIZE is not standard. For internationalized messages, you will need
the workaround described in the second half of
<http://www.gnu.org/software/gettext/manual/html_node/Preparing-Strings.html>.

I hope one of these two alternatives works for you, so that we can avoid an
LGPLv2+ cascade.

Bruno




More information about the libvir-list mailing list