指针由shmat指向返回的地址空间的末尾,它给出seg故障 [英] pointer returned by shmat points at the end of address space which gives seg fault

查看:502
本文介绍了指针由shmat指向返回的地址空间的末尾,它给出seg故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  // TeamSize是一个整数
int Seg_id = shmget(SHM_KEY,sizeof(Word)* TeamSize,IPC_CREAT);

void * Seg_ptr = shmat(Seg_id,0,0);

new(Seg_ptr)Word [TeamSize];

我在此段代码中遇到问题。 Word类是用8个字节的char数组和一些解析函数定义的类。我想我使用shmget和shmat就像别人使用它们。但我不断得到seg故障。当我打印出Seg_id,它看起来正常只是一些数字。但Seg_ptr指向0xffffffffffffffff。然后下一行代码显然给我seg故障。我想知道为什么Seg_ptr指向内存空间的结尾。提前感谢!

解决方案

测试后,似乎非root用户不能使用shmat或者它将返回Permission Denied。 / p>

晚更新:
设置权限有帮助,如shmget(SHM_KEY,sizeof(...),(IPC_CREAT | 0666)这个论点;它结果是共享内存段与相同的键已经存在。然后我可以使用ipcs检查我已经有共享内存段与相同的密钥,并使用ipcrm释放它。


 //TeamSize is an integer   
int Seg_id = shmget(SHM_KEY,sizeof(Word)*TeamSize,IPC_CREAT);

void* Seg_ptr = shmat(Seg_id,0,0);

new(Seg_ptr) Word[TeamSize];

I am having trouble with this segment of code. The Word class is a class that I defined with 8 bytes char array and some parse functions. I think I am using shmget and shmat just like how others use them. But I keep getting seg fault. When I print out Seg_id, it looks normal just some number. But Seg_ptr points at 0xffffffffffffffff. Then the next line of code obviously gives me seg fault. I want to know why Seg_ptr points at the end of memory space. Thanks in advance!

解决方案

After testing, it seems that non-root user cannot use shmat or it will return Permission Denied.

late update: setting permission helps, such as shmget(SHM_KEY, sizeof(...),(IPC_CREAT | 0666)).But I kept getting invalid argument with this ; it turned out shared memory segment with the same key already existed. I could then use ipcs to check if I already have the shared memory segment with the same key and use ipcrm to free it.

这篇关于指针由shmat指向返回的地址空间的末尾,它给出seg故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆