需求:
1:有一块共享内存,分为四个缓存区,2:每一个缓存区又是一个数组,3:每一个数组里面存放着结构体指针
如图
遇到的问题:不能成功申请内存
希望高手指点一下迷津
代码如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
typedef struct {
int32_t a;
int64_t b;
int64_t c;
}Small;
typedef struct {
int index[4];
int small_count[4];
Small *small[4][20];
}Big;
int main(){
Big *big = NULL;
int shm_id = shmget((key_t)12345,sizeof(Big),0666 | IPC_CREAT);
if(shm_id < 0){
printf("get shm_id error\n");
return -1;
}
void *shm_pointer = shmat(shm_id,(void*)0,0);
if(shm_pointer == NULL){
printf("shmmat error\n");
return -1;
}
big = (Big*)shm_pointer;
printf("big=%p\n",big);
printf("big->small[0]=%p\n",big->small[0]);
printf("big->small[0][0]=%p\n",big->small[0][0]);
//next step: I want to memcpy data to the shared_memory
Small small;
small.a = 1;
small.b = 1;
small.c = 1;
memcpy(big->small[2][10],&small,sizeof(Small));
return 0;
}