8000 [BUG] kernel/msgqueue.h -- Not Bug. · Issue #353 · sogou/workflow · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[BUG] kernel/msgqueue.h -- Not Bug. #353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
daidai21 opened this issue May 2, 2021 · 4 comments
Closed

[BUG] kernel/msgqueue.h -- Not Bug. #353

daidai21 opened this issue May 2, 2021 · 4 comments

Comments

@daidai21
Copy link
daidai21 commented May 2, 2021
#include <stdio.h>
#include "./msgqueue.h"

int main(int argc, char* argv[]) {
  msgqueue_t* tmp_mq = msgqueue_create(10, 0);
  char str[] = "hello";

  msgqueue_put(str, tmp_mq);
  char* p = msgqueue_get(tmp_mq);
  printf("%s\n", p);
  printf("%c\n", *p++);

  return 0;
}
➜  workflow gcc example.c msgqueue.c;./a.out
[1]    64749 segmentation fault  ./a.out
@Barenboim
Copy link
Contributor
Barenboim commented May 2, 2021

Not a bug.
The second argument of msgqueue_create() is the offset from the head each message, where users have to reserve bytes of a pointer size for internal linking. This will reduce the allocating and freeing of memory.
In you case, maybe:

int main(void)
{
    msgqueue_t *mq = msgqueue_create(10, -sizeof (void *));
    char str[sizeof (void *) + 6];
    char *p = str + sizeof (void *);
    strcpy(p, "hello");
    msgqueue_put(p, mq);
    p = msgqueue_get(mq);
    printf("%s\n", p);
    return 0;
}

@Barenboim Barenboim changed the title [BUG] kernel/msgqueue.h [BUG] kernel/msgqueue.h -- Not Bug. May 2, 2021
@daidai21 daidai21 closed this as completed May 3, 2021
@sourcema
Copy link

Not a bug. The second argument of msgqueue_create() is the offset from the head each message, where users have to reserve bytes of a pointer size for internal linking. This will reduce the allocating and freeing of memory. In you case, maybe:

int main(void)
{
    msgqueue_t *mq = msgqueue_create(10, -sizeof (void *));
    char str[sizeof (void *) + 6];
    char *p = str + sizeof (void *);
    strcpy(p, "hello");
    msgqueue_put(p, mq);
    p = msgqueue_get(mq);
    printf("%s\n", p);
    return 0;
}

请问下我改成这样为什么输出为空了:
msgqueue_t *mq = msgqueue_create(10, -1);
char str[1 + 6];
char p = str + 1;
strcpy(p, "hello");
msgqueue_put(p, mq);
p = (char
)msgqueue_get(mq);
printf("%s\n", p);
return 0;

@Barenboim
Copy link
Contributor

Linkoff的位置,需要用户预留一个指针大小的空间。内部要拉链的。

@sourcema
Copy link

Linkoff的位置,需要用户预留一个指针大小的空间。内部要拉链的。

好的,谢谢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
0