I'm trying to share a pointer of defined class between the parent and the forked child through shared memory.

so in parent's main i create the pointer

```
mydata *p;
Reader::GetInstance()->Read(p, i+1);
        pid = fork();
        if (pid == -1){
            cout << "error on fork"<<endl;
        }else if (pid == 0){
            cout << "i will fork now" <<endl;
            const char * path = "./mydatamanager";
            execl (path, "-", (char *)0);
            break;
        }else {
            writer(shmid, p);
        }

```

writer contains this 

```
void writer(int shmid , mydata * p)
{
    void *shmaddr;   
    shmaddr = shmat(shmid, (void *)0, 0);
    if((int)shmaddr == -1)
    {   
        perror("Error in attach in writer");
        exit(-1);
    }
    else
    {
        memcpy( shmaddr, p, sizeof(*p) );
    }   
}

```

and my data is 

```
    class mydara {
    public:
        int var1;
        int var2;
        int var3;
        int var4;
        int var5;
        int var6;
        char *var7;
mydata (int v2, int v3,char *v7, int v6){
        var2 = v2;
        var3 = v3;
        var7 =new char[128];
        strcpy(var7, v7);
        var6 = v6;
        var4 = 0;
        var5 = 0;
    }
    };

```

and in the mydatamanager i get this pointer this way

```
void reader(int shmid, mydata *& p)
{
    cout << "in reader" << endl;
    void *shmaddr;

      //sleep(3);

    shmaddr = shmat(shmid, (void *)0, SHM_RDONLY|0644);
    if((int)shmaddr == -1)
    {   
        perror("Error in reader");
        exit(-1);
    }
    else
    {
        cout << "in else "<< endl;
        p = (mydata*) shmaddr;
        cout <<"shared memory address is " <<shmaddr <<endl;
        cout <<"var5 "<< p->var5<< endl;
        cout <<"var2 "<< p->var2<< " match with "<<getpid() << "?" << endl;
        cout <<"var3 "<< p->var3<< endl;
        cout <<"var4 "<< p->var4<< endl;
        cout <<"var7 "<< p->var7<< endl; // the 
        //shmdt(shmaddr);
    }
}

```

and mydatamanager main :

```
int main()
{
    cout << "in main" <<endl;
    int shmid;
    shmid = shmget(IPC_PRIVATE, 4096, IPC_CREAT|0644);
    cout << "in advanced point" <<endl;
    sleep(1);
    mydata * p;
    reader (shmid, p);
    cout << p->var7 <<endl;
    return 0;
}

```

the results are always 0.
how can i share this pointer through the parent and the child and where is the fault in my code?