Prev: Stylistic questions on UNIX C coding.
Next: For thine is the capitalization, the spacing, and the brace question. For ever and ever. On Usenet.
From: Vandana on 24 Feb 2010 17:27 Hello All, When I compile the program below: I get the error, main.c: In function main: main.c:33: error: dereferencing pointer to incomplete type Looks like the struct is not getting memory allocated. But I thought this is happening throught the shmget/shmat calls. Am I wrong? I am unable to understand this, could someone please explain this. Thank you. ################ /*Sim.h*/ #ifndef SIM_H #define SIM_H #include <stdio.h> struct sync_proc { int parent_count; int child_count; int count; int done; }; extern struct sync_proc *sync_procs; int sim_main(int core); #endif ~ ######################### main.c /* main.c */ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/shm.h> #include <sys/ipc.h> #include <sys/wait.h> #include <pthread.h> #include "sim.h" int main() { struct sync_procs *sync_core; pid_t pid; int status; key_t key; key = 1045; int shmidi; if ((shmid = shmget(key, sizeof(struct sync_proc), IPC_CREAT | 0666)) < 0) { perror("shmget"); exit(1); } printf("SHMID_opti::: %d\n", shmid); sync_core = (struct sync_procs*) shmat(shmid, NULL, 0); if (sync_core == (struct sync_procs*) -1) { perror("shmat"); exit(1); } sync_core->count = 0; /* line producing the error */ pid = fork(); if (pid == 0 ) { /* child process */ sim_main(0); exit(0); } else { /* parent process */ sim_main(1); wait(&status); } return 0; } ################## /* sim.c - does nothing for now */ #include <stdio.h> int sim_main(int core) { printf("From sim_main.c\n"); return 0; } ~
From: Moi on 24 Feb 2010 17:39 On Wed, 24 Feb 2010 14:27:37 -0800, Vandana wrote: > Hello All, > > When I compile the program below: I get the error, main.c: In > function 'main': > main.c:33: error: dereferencing pointer to incomplete type > > Looks like the struct is not getting memory allocated. But I thought > this is happening throught the shmget/shmat calls. Am I wrong? I am > unable to understand this, could someone please explain this. Thank you. > > ################ > /*Sim.h*/ > #ifndef SIM_H > #define SIM_H > #include <stdio.h> > > struct sync_proc { > int parent_count; > int child_count; > int count; > int done; > }; [snip] > printf("SHMID_opti::: %d\n", shmid); > sync_core = (struct sync_procs*) shmat(shmid, NULL, 0); > sync_core = (struct sync_proc*) shmat(shmid, NULL, 0); HTH, AvK
From: Nicolas George on 24 Feb 2010 18:53 Vandana wrote in message <60eea08c-0418-47d2-be51-76949a18bf88(a)s25g2000prd.googlegroups.com>: > struct sync_proc { > struct sync_procs *sync_core; Trivial typo: sync_proc and sync_procs are not the same thing.
From: Nicolas George on 24 Feb 2010 18:57 Moi wrote in message <7ffbd$4b85aa91$5350c024$29001(a)cache120.multikabel.net>: > sync_core = (struct sync_proc*) shmat(shmid, NULL, 0); sync_core = shmat(...); Casting from void * is useless in C and may hide legitimate warnings, for example if the header declaring shmat was forgotten and thus shmat got the implicit int return type.
From: Vandana on 24 Feb 2010 19:14
Thanks a lot all, so stupid of me. Sorry for wasting your time. On Feb 24, 3:57 pm, Nicolas George <nicolas$geo...(a)salle-s.org> wrote: > Moi wrote in message > > <7ffbd$4b85aa91$5350c024$29...(a)cache120.multikabel.net>: > > > sync_core = (struct sync_proc*) shmat(shmid, NULL, 0); > > sync_core = shmat(...); > > Casting from void * is useless in C and may hide legitimate warnings, for > example if the header declaring shmat was forgotten and thus shmat got the > implicit int return type. |