Browsing through a Pointer to Array of Structures
Hello, I'm working on some wacky shared memory using application for Unix.
Here is the thing, I use shmget and shmat to create and attach a shared memory block, but making it an array of structures.
The structure:
Code:
struct object{
char nick[16];
pid_t pid;
key_t queue;
} *mem, *p;
SHM declaration:
Code:
if( (mem = (struct object*)shmat((key_t)argv[1], (void*)0, 0)) == (void*)-1 ) {
shmget((key_t)argv[1], SHMSIZE*sizeof(struct object), IPC_CREAT | 0666);
mem = (struct object*)shmat( (key_t)argv[1], (void*)0 , 0);
}
And now the problematic part. I'm using this loop to browse through this thing.
Code:
for(p = mem; p< &mem[SHMSIZE]); p++) {
if(!p->nick)
{
key = ftok(".", randomize()); //randomize just provides a random integer from dev/random
msgid = msgget(key, IPC_CREAT);
p->nick = getenv("USERNAME");
printf("%s", p->nick);
p->pid = getpid();
p->queue = msgid;
}
printf("lalala\n");
}
But this one just doesn't work. Any suggestions please?