linux中的return问题

marshalsoul 2010-04-19 11:13:54
有一段创建消息队列的代码如下:
int creat_msg_queue()
{
key_t key;
int proj_id;
int msqid;

struct msqid_ds buffer;

proj_id=2;
key=ftok("/home/program",proj_id);
if(key==-1){
perror("cannot generate the IPC key");
return -1;
}

msqid=msgget(key,IPC_CREAT | 0660);
if(msqid==-1){
perror("cannot create message queue resource");
return -1;
}

printf("msqid=%d\n",msqid);

return msqid;
}

在该函数中我打印出的“msqid”的数值是32769
但是该函数的返回值却是0,这是为什么啊?

...全文
261 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
kingteng 2010-04-19
  • 打赏
  • 举报
回复
32769的二进制码是1000000000000001正好是0的补码。不过我解释不了,标记等待高人
HalfmanHuang 2010-04-19
  • 打赏
  • 举报
回复
溢出了诶,32769换到二进制1000000000000001,正好是0
challenge99 2010-04-19
  • 打赏
  • 举报
回复
调用 creat_msg_queue 的代码一并给出
winbsd 2010-04-19
  • 打赏
  • 举报
回复
unsigned int
int
pottichu 2010-04-19
  • 打赏
  • 举报
回复

#include <stdio.h>
# include <sys/ipc.h>
#include <sys/types.h>
#include <sys/msg.h>
#include <unistd.h>

int creat_msg_queue()
{
key_t key;
int proj_id;
int msqid;

struct msqid_ds buffer;

proj_id=2;
key=ftok("/home/program",proj_id);
if(key==-1){
perror("cannot generate the IPC key");
return -1;
}

msqid=msgget(key,IPC_CREAT | 0660);
if(msqid==-1){
perror("cannot create message queue resource");
return -1;
}

printf("msqid=%d\n",msqid);

return msqid;
}

int main()
{
int ret = creat_msg_queue();
printf("%d \n", ret);
}



结果都是 0 阿。。。
你的 kernel 是什么版本?

23,114

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧