// Create a shared memory template <class T> int CreateShm(T **ppst, int iKey, int iSize) { int shmid; void *p; struct shmid_ds stShmIDDS; if ((shmid = shmget(iKey, iSize, IPC_CREAT | 0777)) < 0) { // failed to create shm return -1; } if ((p = shmat(shmid, 0 ,0)) == (void *) -1) { // failed to get address return -1; } shmctl(shmid, IPC_STAT, &stShmIDDS); if (stShmIDDS.shm_cpid == getpid()) { // init this memory here } *ppst=(T *)p; return 0; };