传递给 `msgpack_pack_int()` 宏的计数器变量不会递增

发布于 2024-12-05 01:13:32 字数 1273 浏览 0 评论 0原文

我在尝试运行这个使用 zmq 的非常简单的 C 程序 时遇到了一个非常奇怪的问题和msgpack

server.c 没有问题,但是在 clinet.c:39 中有 这是 msgpack_pack_int (&mpkg, i); 并且 i 的值似乎 被选取为 0 并且在每次迭代中都不会改变。我 尝试了很多不同的事情(例如指向 i 并使用它,还尝试将其拆分为函数等) 似乎没有什么帮助。我可以看到 msgpack_pack_int() 是 一个宏,但是为什么它会引入这样的行为以及什么可以 我该怎么克服呢?是否有一个标志可以改变行为 这种宏(正如我所看到的,它扩展为内联函数)... 我已经尝试使用 -Werror -Wallgccclang,但什么也没有 出现警告;(*

我尝试调试它,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 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_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

開玄 2024-12-12 01:13:32

事实证明,在每次迭代中我都这样做:

 msgpack_packer_init (&mpkg, &sbuf, msgpack_sbuffer_write);

只需要完成一次,而这应该是:

 msgpack_sbuffer_init (&sbuf);

或者:

 msgpack_sbuffer_clear (&sbuf);

msg_pack* 函数放在一起是相当合乎逻辑的,而且确实如此
这是取自 简单示例
问题确实出在文档上,一条额外的评论会有所帮助!

更新:工作版本 & 没有 memcpy 的版本

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文