我可以将共享内存的地址直接分配给指针吗?
Can I assign the address of shared memory to a pointer directly?
我正在尝试为 IPC 创建共享内存。我想将一个带有动态二维数组的结构放入共享内存中。这是结构。
/* struct with 2 2D arrays. */
struct _test {
uint16_t **A;
uint16_t **B;
} test;
我知道双指针实际上不是二维数组,我应该像 int (*ptr)[3]
一样使用指向数组 的 指针,但问题是我只能在 运行 时间内获取二维数组的大小。所以我必须以这种方式声明二维数组(至少我所知道的)。
然后我在运行时间内计算出这两个数组的大小,假设它们都是2x2数组,占用16个字节(uint16_t是2个字节)。所以我这样做了:
#include <sys/ipc.h>
#include <sys/shm.h>
#include <memory.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
size_t size = 16; //size of two uint16_t 2D arrays.
key_t key;
key = ftok("dev/null", 1);
int s_shm_id = shmget(key, size, IPC_CREAT|0600); //get 16 bytes of shared memory.
test *shm = (test*)shmat(s_shm_id,(const void*)0,0); //attach it.
//I want pointers in this struct to point at shared memory.
test *ptr = malloc(sizeof(test));
//Array A starts at the beginning of shared memory.
ptr -> A = (uint16_t **)shm; //My main confusion is here. Is this right?
int i;
for(i=0; i<2; i++)
ptr->A[i] =(uint16_t *)((uint16_t *)ptr->A + i*2);
//Array B starts right after A.
ptr -> B = ptr -> A[i-1] + 2;
for(i=0; i<2; i++)
ptr -> B[i] = (uint16_t *)((uint16_t *)ptr->B + i*2);
}
我明白这基本上是错误的,我遇到了段错误,但是怎么办?指针需要的是一个指向的地址,既然我已经创建了一个space(使用shmget),为什么我不能做一个指向它的指针呢?感谢您提前反馈!
您要的是 "jagged" 或 "scattered" 数组,而不是 "linear" 数组。散乱的二维数组其实不是一个数组,而是1+N个数组,N是你要找的二维矩阵的维数。
您显示的代码未在“1+N”内分配这 1 个数组。
假设您成功分配了足够的内存来容纳两个维度 N
为 uint16_t
的二维数组,即 2 * N*N * sizeof (uint16_t)
字节,那么准备访问此内存的代码可能看起来像这样(为了便于阅读而省略了错误检查):
void * p = ... /* Allocate memory here; does not necessarily needs to be SHM. */
uint16_t ** A = malloc(N * sizeof *A);
for (size_t i = 0; i < N; ++i)
{
A[i] = ((uint16_t*) p) + i*N;
}
uint16_t ** B = malloc(N * sizeof *B);
for (size_t i = N; i < 2*N; ++i)
{
B[i - N] = ((uint16_t*) p) + i*N;
}
/* Access A[0 .. N-1][0 .. N-1] and B[0 .. N-1][0 .. N-1] here ... */
将 A
和 B
放入 struct
中,而后者又是动态分配的,留给您作为练习。
直接访问数组的元素:A[0][0]
访问第一个数组的第一行的第一个元素。
为清楚起见,NxM
数组的相同代码
uint16_t ** A = malloc(N * sizeof *A);
for (size_t i = 0; i < N; ++i)
{
A[i] = ((uint16_t*) p) + i*M;
}
uint16_t ** B = malloc(N * sizeof *B);
for (size_t i = N; i < 2*N; ++i)
{
B[i - N] = ((uint16_t*) p) + i*M;
}
我正在尝试为 IPC 创建共享内存。我想将一个带有动态二维数组的结构放入共享内存中。这是结构。
/* struct with 2 2D arrays. */
struct _test {
uint16_t **A;
uint16_t **B;
} test;
我知道双指针实际上不是二维数组,我应该像 int (*ptr)[3]
一样使用指向数组 的 指针,但问题是我只能在 运行 时间内获取二维数组的大小。所以我必须以这种方式声明二维数组(至少我所知道的)。
然后我在运行时间内计算出这两个数组的大小,假设它们都是2x2数组,占用16个字节(uint16_t是2个字节)。所以我这样做了:
#include <sys/ipc.h>
#include <sys/shm.h>
#include <memory.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
size_t size = 16; //size of two uint16_t 2D arrays.
key_t key;
key = ftok("dev/null", 1);
int s_shm_id = shmget(key, size, IPC_CREAT|0600); //get 16 bytes of shared memory.
test *shm = (test*)shmat(s_shm_id,(const void*)0,0); //attach it.
//I want pointers in this struct to point at shared memory.
test *ptr = malloc(sizeof(test));
//Array A starts at the beginning of shared memory.
ptr -> A = (uint16_t **)shm; //My main confusion is here. Is this right?
int i;
for(i=0; i<2; i++)
ptr->A[i] =(uint16_t *)((uint16_t *)ptr->A + i*2);
//Array B starts right after A.
ptr -> B = ptr -> A[i-1] + 2;
for(i=0; i<2; i++)
ptr -> B[i] = (uint16_t *)((uint16_t *)ptr->B + i*2);
}
我明白这基本上是错误的,我遇到了段错误,但是怎么办?指针需要的是一个指向的地址,既然我已经创建了一个space(使用shmget),为什么我不能做一个指向它的指针呢?感谢您提前反馈!
您要的是 "jagged" 或 "scattered" 数组,而不是 "linear" 数组。散乱的二维数组其实不是一个数组,而是1+N个数组,N是你要找的二维矩阵的维数。
您显示的代码未在“1+N”内分配这 1 个数组。
假设您成功分配了足够的内存来容纳两个维度 N
为 uint16_t
的二维数组,即 2 * N*N * sizeof (uint16_t)
字节,那么准备访问此内存的代码可能看起来像这样(为了便于阅读而省略了错误检查):
void * p = ... /* Allocate memory here; does not necessarily needs to be SHM. */
uint16_t ** A = malloc(N * sizeof *A);
for (size_t i = 0; i < N; ++i)
{
A[i] = ((uint16_t*) p) + i*N;
}
uint16_t ** B = malloc(N * sizeof *B);
for (size_t i = N; i < 2*N; ++i)
{
B[i - N] = ((uint16_t*) p) + i*N;
}
/* Access A[0 .. N-1][0 .. N-1] and B[0 .. N-1][0 .. N-1] here ... */
将 A
和 B
放入 struct
中,而后者又是动态分配的,留给您作为练习。
直接访问数组的元素:A[0][0]
访问第一个数组的第一行的第一个元素。
为清楚起见,NxM
数组的相同代码
uint16_t ** A = malloc(N * sizeof *A);
for (size_t i = 0; i < N; ++i)
{
A[i] = ((uint16_t*) p) + i*M;
}
uint16_t ** B = malloc(N * sizeof *B);
for (size_t i = N; i < 2*N; ++i)
{
B[i - N] = ((uint16_t*) p) + i*M;
}