类型铸造共享存储器指向整数指针

发布于 2025-02-04 22:54:31 字数 478 浏览 1 评论 0原文

int main()
{
    key_t key = ftok("yu", 65);
    int shmid = shmget(key, 100 * sizeof(int), 0666 | IPC_CREAT);
    int** Matr = (int**)shmat(shmid, (void*)0, 0);

    for (int i = 0; i<3; i++)
    {
        for (int j = 0; j<3; j++)
        {
            Matr[i][j] = i + j; // writing to shared memory
        }
    }

    shmdt(Matr);
    return 0;
}

我正在尝试键入将共享内存指针铸造为整数双指针,但是每次编译代码时,都会说分割故障(核心倾倒)。有人可以告诉我如何做吗?提前致谢。

PS:我在C ++上这样做。

int main()
{
    key_t key = ftok("yu", 65);
    int shmid = shmget(key, 100 * sizeof(int), 0666 | IPC_CREAT);
    int** Matr = (int**)shmat(shmid, (void*)0, 0);

    for (int i = 0; i<3; i++)
    {
        for (int j = 0; j<3; j++)
        {
            Matr[i][j] = i + j; // writing to shared memory
        }
    }

    shmdt(Matr);
    return 0;
}

I am trying to type cast the shared memory pointer to integer double pointer however every time I compile the code, it says that segmentation fault(core dumped). Can someone tell me how to do this? Thanks in advance.

P.S : I am doing this on C++.

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

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

发布评论

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

评论(1

云仙小弟 2025-02-11 22:54:32

您可以尝试以下代码。

主要区别在于,矩阵是用 1D 连续的内存块(数组)实现的,因此matr是类型int*int*

矩阵的2D索引的管理是“手动”通过从2个矩阵索引(i,j)中计算数组中的索引(idx)来完成的。

注意:我建议您为所有系统调用添加错误处理。

#include <sys/ipc.h>
#include <sys/shm.h>

int main()
{
    key_t key = ftok("yu", 65);
    // check if key is -1 and if so handle the error ...
    int shmid = shmget(key, 100 * sizeof(int), 0666 | IPC_CREAT);
    // check if shmid is -1 and if so handle the error ...
    int* Matr = (int*)shmat(shmid, (void*)0, 0);
    // check if Matr is (void*)-1 and if so handle the error ...

    int width = 3;
    int height = 3;
    // NOTE: width * height must be <= 100 (according to the declared size of the shared memory).

    for (int j = 0; j<height; ++j)
    {
        for (int i = 0; i<width; ++i)
        {
            int idx = j * width + i;
            Matr[idx] = i + j; // writing to shared memory
        }
    }

    int st = shmdt(Matr);
    // check if st is -1 and if so handle the error ...
    return 0;
}

You can try the following code.

The main difference is that the matrix is implemented with a 1D continous block of memory (an array), and therefore Matr is of type int*.

The management of the 2D indices of the matrix is done "manually" by calculating an index in the array (idx) from the 2 matrix indices (i,j).

Note: I advise you to add error handling for all the system calls.

#include <sys/ipc.h>
#include <sys/shm.h>

int main()
{
    key_t key = ftok("yu", 65);
    // check if key is -1 and if so handle the error ...
    int shmid = shmget(key, 100 * sizeof(int), 0666 | IPC_CREAT);
    // check if shmid is -1 and if so handle the error ...
    int* Matr = (int*)shmat(shmid, (void*)0, 0);
    // check if Matr is (void*)-1 and if so handle the error ...

    int width = 3;
    int height = 3;
    // NOTE: width * height must be <= 100 (according to the declared size of the shared memory).

    for (int j = 0; j<height; ++j)
    {
        for (int i = 0; i<width; ++i)
        {
            int idx = j * width + i;
            Matr[idx] = i + j; // writing to shared memory
        }
    }

    int st = shmdt(Matr);
    // check if st is -1 and if so handle the error ...
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文