C++ 指向使用apr\u shm库的结构中的指针的指针

C++ 指向使用apr\u shm库的结构中的指针的指针,c++,c,apache,pointers,struct,C++,C,Apache,Pointers,Struct,我使用apr共享库获得apache模块代码 我修改了共享数据结构,添加了一些字段,该字段有指向指针的指针,诸如此类 typedef struct { int a; int b; char *str; double **dpp; STRU1 **str; } shm_data; /* The per-server configuration */ typedef struct { char *shmcounterfile; char *sh

我使用apr共享库获得apache模块代码

我修改了共享数据结构,添加了一些字段,该字段有指向指针的指针,诸如此类

typedef struct {
    int a;
    int b;
    char *str;
    double **dpp;
    STRU1 **str;
} shm_data;

/* The per-server configuration */
typedef struct {
    char *shmcounterfile;
    char *shmcounterlockfile;
    apr_global_mutex_t *mutex; /* the cross-thread/cross-process mutex */
    apr_shm_t *data_shm;   /* the APR shared segment object */
    shm_data *data;  /* the per-process address of the segment */
} shm_data_scfg_t;

...

/* parent httpd init code => ap_hook_post_config */
scfg = (shm_data_scfg_t*)ap_get_module_config(s->module_config, &shm_module);

apr_shm_create(&scfg->data_shm, sizeof(*scfg->data),
                            scfg->shmcounterfile, pconf);
/* The pointer to the shm_data structure is only valid
 * in the current process, since in another process it may
 * not be mapped in the same address-space. This is especially
 * likely on Windows or when accessing the segment from an
 * external process. */
scfg->data = (shm_data*)apr_shm_baseaddr_get(scfg->data_shm);

/* Clear all the data in the structure. */
memset(scfg->data, 0, sizeof(*scfg->data));

scfg->data->a = 1;
scfg->data->b = 2;
scfg->data->str = "test";

scfg->data->dpp = (double**)malloc(sizeof(double*) * 10);
for (int i = 0; i < 10; i++) {   
    scfg->data->dpp[i] = (double*)malloc(sizeof(double) * 10);
    for (int l = 0; l < 10; l++) {   
        scfg->data->dpp[i][l] = l;
    }   
}
...
typedef结构{
INTA;
int b;
char*str;
双**民进党;
STRU1**str;
}shm_数据;
/*每个服务器的配置*/
类型定义结构{
char*shmcounterfile;
char*shmcounterlockfile;
apr\u global\u mutex\u t*mutex;/*跨线程/跨进程互斥*/
apr\u shm\u t*数据\u shm;/*apr共享段对象*/
shm_数据*数据;/*段的每个进程地址*/
}shm_数据scfg_t;
...
/*父httpd init code=>ap\u hook\u post\u config*/
scfg=(shm\U数据\U scfg\U t*)ap\U获取模块配置(s->模块配置和shm\U模块);
apr\u shm\u创建(&scfg->data\u shm,sizeof(*scfg->data),
scfg->shmcounterfile,pconf);
/*指向shm_数据结构的指针仅有效
*在当前流程中,因为在另一个流程中
*不能映射到同一地址空间。这尤其重要
*可能是在Windows上,或从
*外部过程*/
scfg->data=(shm\U data*)apr\U shm\U baseaddr\U get(scfg->data\U shm);
/*清除结构中的所有数据*/
memset(scfg->data,0,sizeof(*scfg->data));
scfg->data->a=1;
scfg->data->b=2;
scfg->data->str=“测试”;
scfg->data->dpp=(双**)malloc(sizeof(双*)*10);
对于(int i=0;i<10;i++){
scfg->data->dpp[i]=(双*)malloc(sizeof(双)*10);
对于(int l=0;l<10;l++){
scfg->data->dpp[i][l]=l;
}   
}
...
而且效果很好。子进程可以访问“dpp”或“str”的值,这些值与实际值相同

据我所知,malloc在一个进程(父httpd)中分配了私有内存,该进程无法从其他进程读取。(儿童httpd)


这是怎么回事?感谢您的帮助。

如果您使用的是大量使用线程的Apache MPM,那么这段代码可能会正常工作,因为所有线程都将彼此以及它们的父进程共享一个地址空间。但是,它不能在重负载下工作(因为Apache将开始对每组线程使用不同的进程),或者根本不能在mpm下工作


如果要将数据存储在共享内存中,所有数据必须存储在共享内存中(即,不能对任何数据使用
malloc()
),并且不能在该内存中使用指针(因为如果shm区域映射到其他位置,指针将无效)。

“…内存…无法相互访问。”请问你想表达什么?这是关于C还是C++?我之所以说C,是因为您似乎在使用APR,对吗?我的意思是,指针的值仅在当前进程中有效。很抱歉我的英语很差。这是用apr在g++中编译的。哪个指针?从您的问题中不明显的是,您正在处理多个进程。共享结构(shm_数据)中的双指针“dpp”以及其中的其他指针变量。我不知道。。也许它起作用的原因是apr_shm_用shm_open和mmap.而不是shmget.创建函数实现?谢谢。但我使用的是预工作mpm。。共享结构中动态分配的指针变量('dpp')对其他进程仍然可见。