[BUG] kernel/msgqueue.h -- Not Bug.
daidai21 opened this issue · 4 comments
#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
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;
}
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;
Linkoff的位置,需要用户预留一个指针大小的空间。内部要拉链的。
Linkoff的位置,需要用户预留一个指针大小的空间。内部要拉链的。
好的,谢谢