Correctly print uint64_t / size_t in C

Have you even stumbled upon a strange warning when trying to print an uint64_t or a size_t ?
Do no take that warning lightly, as your code can break anytime if you’re not fixing it.

Warning example:

warning: format '%llu' expects argument of type 'long long unsigned int', but argument 4 has type 'uint64_t' [-Wformat]

To avoid the warning and crashes, you have to use a one of the PRIxxx macro which will choose the right way for your system to print the value, without crashes.
Example:

#include <inttypes.h>
uint64_t i;
printf("%"PRIu64"\n", i);

Macros for printf family can be found here.

Why gettimeofday() is bad at timing things

I was reading a changelog from a library I love (unrelated but here is the link: https://liballeg.org/ ) and I stumble upon that change:


“Use clock_gettime with CLOCK_MONOTONIC instead of gettimeofday (check-switch-26)”

Then I did a bit of research on the topic, and found a good explanation here:

https://blog.habets.se/2010/09/gettimeofday-should-never-be-used-to-measure-time.html

TLDR: if you use gettimeofday to time things then your program may be affected by time shift, because gettimeofday is not monotonic. if you do not care about the date and only about elapsed time, use clock_gettime.

Cross platform correct socket error code

A friend using my networking code made the good point that it was not correctly reporting errors on some windows compilers.

I hope we got this sorted out.

Grab / update your code from the git repo: https://framagit.org/GullRaDriel/nilorea-library

Added:
-netw_unload() =>macro who calls WSACleanup . Call it before exiting (on OK or error). Does nothing on *nix/*nux


-netw_set_blocking(…) => set blocking mode on a network

Internals:
-neterr => WSAGetLastError or errno
-netstrerr => FormatMessage or strerror (strerror_r wrapper is quoted because not working on redhat)

This has caused a full revamping of all error management, but now it’s ok. Valgrind checked.

#nilorea-library #networking #winsock #socket