In a C program , i create a shared memory segment of large size and then put a variable number of structs into it (2 in this example).
strcut message * x;
x = (struct message *)shmat(shmid,NULL,0);
x[0].a = 1;
x[0].b = 2;
x[1].a = 3;
x[1].b = 4;
There is a reader program which has to read all the structs written in the shared memory segment but does not know how many structs are there. I have tried the following way:
struct message * x;
x = (struct message *)shmat(shmid,NULL,0);
while(x!=NULL)
{
printf("x.a = %d\n",x->a);
printf("x.b = %d\n",x->b);
printf("\n\n");
x=x++;
}
It gives me the 2 structs correctly but after that it gives me 0 (or random garbage values ) for many more times ( for both a and b ) until it runs out of the shared memory segment and then gives a segmentation fault. How do i go about it?
I am using UBUNTU.