共享内存Segv

发布于 2025-01-07 09:55:03 字数 851 浏览 2 评论 0原文

我尝试使用Boost,并在分配了3000个大小为24的对象后有一个segv,现在我开始使用 sys/ipc.h 和 sys/shm.h ,我分配了25 mio字节(如果我正确理解这一点)

似乎也可以在我的linux机器上正常工作 ipcs -m 将显示分配的段

0x000081bc 917516     testUser 644        25000000   0

sysctl -p 将打印

kernel.shmmax = 25500000

由于某种原因它可以工作直到程序达到“43406 x 24字节”那就是它将segv的地方。我很高兴得到一些提示我的问题所在。另请注意,这是否是分配和使用对象共享内存的错误方法。

#define MAXMYMEM 25000000
int sharedMemId;
x *p_sharedMemory;
x *p_other;
sharedMemId = shmget(2232, MAXMYMEM, IPC_CREAT | 0644);

if(sharedMemId >= 0){

    p_sharedMemory = (x*) shmat( sharedMemId, 0 , 0);

    if(p_sharedMemory != ( x *)-1) {

        cout << sizeof(x) << endl;

        for(unsigned int i = 0 ; i < 1000000;i++ ){



            (p_sharedMemory + (sizeof(x) * i))->setTest(i);

        }

I have tried to use Boost and had a segv after 3000 allocations of objects of size 24, now i started to use sys/ipc.h and sys/shm.h , i allocate 25 mio bytes (if i understand this properly)

it seems also to work properly on my linux box ipcs -m will show the allocated segment

0x000081bc 917516     testUser 644        25000000   0

sysctl -p will print

kernel.shmmax = 25500000

For some reason it works until the program reaches "43406 x 24 bytes" that's the place it will segv. I would be glad to get some hints where my problem lies. Please also note if this is the wrong way to allocate and use the shared memory with objects.

#define MAXMYMEM 25000000
int sharedMemId;
x *p_sharedMemory;
x *p_other;
sharedMemId = shmget(2232, MAXMYMEM, IPC_CREAT | 0644);

if(sharedMemId >= 0){

    p_sharedMemory = (x*) shmat( sharedMemId, 0 , 0);

    if(p_sharedMemory != ( x *)-1) {

        cout << sizeof(x) << endl;

        for(unsigned int i = 0 ; i < 1000000;i++ ){



            (p_sharedMemory + (sizeof(x) * i))->setTest(i);

        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

懒猫 2025-01-14 09:55:03

(p_sharedMemory + (sizeof(x) * i))->setTest(i);

为什么在这里使用 sizeof(x) ?向指向 x 的指针加 1 将指向下一个 x,而不是下一个字节。我怀疑这是你的问题。

(p_sharedMemory + (sizeof(x) * i))->setTest(i); 更改为

++p_sharedMemory;
p_sharedMemory->setTest(i);

(p_sharedMemory + (sizeof(x) * i))->setTest(i);

Why are you using sizeof(x) here? Adding one to a pointer that points to an x will point at the next x, not the next byte. I suspect this is your problem.

change (p_sharedMemory + (sizeof(x) * i))->setTest(i); to

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