传递给 `msgpack_pack_int()` 宏的计数器变量不会递增
我在尝试运行这个使用 zmq 的非常简单的 C 程序 时遇到了一个非常奇怪的问题和msgpack。
server.c
没有问题,但是在 clinet.c:39
中有 这是 msgpack_pack_int (&mpkg, i);
并且 i
的值似乎 被选取为 0
并且在每次迭代中都不会改变。我 尝试了很多不同的事情(例如指向 i
并使用它,还尝试将其拆分为函数等) 似乎没有什么帮助。我可以看到 msgpack_pack_int()
是 一个宏,但是为什么它会引入这样的行为以及什么可以 我该怎么克服呢?是否有一个标志可以改变行为 这种宏(正如我所看到的,它扩展为内联函数)... 我已经尝试使用 -Werror -Wall
、gcc
和 clang
,但什么也没有 出现警告;(*
我尝试调试它,i
按预期递增。
我什至尝试过这个,无论如何它都会做同样的事情:
void pack (msgpack_packer *p, msgpack_sbuffer *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/");
}
我什至尝试过本来应该有所不同,但这里也没有运气 -
int count (void) {
static int i = 0;
i += 1; return i;
}
谁能明白为什么会发生这种情况?
更新 1: 另外,我重新编译了 msgpack 库本身,没有优化标志, 这也没有改变行为。
更新 2: 从 git 存储库安装了 msgpack,但我仍然遇到同样的问题。
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 toi
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_sbuffer *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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,在每次迭代中我都这样做:
只需要完成一次,而这应该是:
或者:
将
msg_pack*
函数放在一起是相当合乎逻辑的,而且确实如此这是取自 简单示例 和
问题确实出在文档上,一条额外的评论会有所帮助!
更新:工作版本 & 没有 memcpy 的版本。
It turns out that on each iteration I was doing this:
that needs to be done only once, and this should be there instead:
or:
It was rather logical to put
msg_pack*
functions together and indeedthat 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.