Counter variable passed to `msgpack_pack_int()` macro doesn't increment
I have a very odd issue trying to run this quite simple C program which is using zmq and msgpack.
There is no problem with server.c
, however in clinet.c:39
there
is this msgpack_pack_int (&mpkg, i);
and the value of i
seems
to be picked up as 0
and doesn't change on each iteration. I
have tried a bunch of different things (e.g. making a pointer to
i
and using that, also tried to split it into a function etc)
and nothing seems to help. I can see that msgpack_pack_int()
is
a macro, but why it would introduce such a behaviour and what can
I do to overcome it? Is there a flag that could change the behaviour
of this kind of macro (as I see it expands to an inline function)...
I have tried -Werror -Wall
, with gcc
and clang
, and nothing
comes up in warning either ;(*
I tried debugging it and i
increments as expected.
I even tried this, and it would do the same thing anyway:
void pack (msgpack_packer *p, msgpack_sbuf开发者_如何学Gofer *b) {
static volatile int i = 0;
printf("\ni=%d\n", i);
msgpack_packer_init (p, b, msgpack_sbuffer_write);
msgpack_pack_array (p, 2);
msgpack_pack_int (p, i++);
msgpack_pack_str (p, "/i/am/a/clinet/");
}
I have even tried something which was supposed to be different, but no luck here either -
int count (void) {
static int i = 0;
i += 1; return i;
}
can anyone see why would this happen?
Update 1: Also I have recompiled msgpack library itself without optimization flags, and that didn't change the behaviour either.
Update 2: Installed msgpack from the git repo and I get still have the same issue.
It turns out that on each iteration I was doing this:
msgpack_packer_init (&mpkg, &sbuf, msgpack_sbuffer_write);
that needs to be done only once, and this should be there instead:
msgpack_sbuffer_init (&sbuf);
or:
msgpack_sbuffer_clear (&sbuf);
It was rather logical to put msg_pack*
functions together and indeed
that was taken from the simple example and
the problem is really with the documentation, one extra comment would help!
Update: working version & version without memcpy.
精彩评论