我使用shmget和shmat创建一个读写缓冲区。但是,当我写写段时,我的程序崩溃了。这是代码-
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/ipc.h>
#include <sys/shm.h>
void Cds_shminit(unsigned int key, unsigned int size, void* shmp)
{
int shmid;
shmid = shmget(key, size, 0644|IPC_CREAT);
if(shmid == -1){
printf("AICM_DEBUG(TCL Process):Error allocating shared memory...\n");
exit(-1);
}
/*if the shared memory assignment is successful, attach to the segment
and obtain a pointer*/
shmp = shmat(shmid, NULL, 0);
if(shmp == (void *) -1){
printf("AICM_DEBUG(TCL Process):Error attaching to shared memory....\n");
}
}
int main(void)
{
int shmid1,shmid2;
void *p;
void *q;
Cds_shminit(0x1234, 1024, p);
if(p==NULL){
printf("Null Ptr p\n");
}
int a = 0x51515151;
int *u;
u = (int *)p;
*u = a;
Cds_shminit(0x1234, 1024, q);
if(q==NULL){
printf("Null Ptr q\n");
}
printf("%x \n",*(int *)q);
printf ("%x %x\n",p,q);
while(1);
}
最后,我添加了检查以查看p是否为NULL,并且确实如此。有人可以帮我找到此代码的问题吗?如果我全局声明了void指针(在main()外部声明void * p和void * q)。我无法投下他们。强制转换全局空指针是否有限制?