From Bugzilla Helper: User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0) Description of problem: 1)When I define structed variables within a function/main as local variable using a structed type declared outside all the function/main at the beginning of the program, I always get "segmentation fault" from the running of compiler-passed program when I put a system- declared struct definition "struct sigaction sa;" before my-declared struct definitions; sometimes gcc can find only one my-declared struct and sometimes none; 2)When I define these variables outside the function/main as global variables after the structed type declarations, I only receive "segmentation fault" from the running of the compiler-passed program; 3)my declared struct is fixed-sized with 1 int and 2 unsigned char and gcc should recognize, allocate storages for them, and initiate them by default when I define variables using these types, according Brian Richiete C book. Version-Release number of selected component (if applicable): How reproducible: Always Steps to Reproduce: 1.copy the following program types.c into your pc redhat 7.3: #include <stdio.h> #include <signal.h> //#include "types.h" struct maketype { int c0; unsigned char c1; unsigned char c2; }; //struct maketype *m_c; //struct maketype *s_c; typedef struct maketype Mt; //Mt *m_c; //Mt *s_c; main () { //struct sigaction sa; struct maketype *m_c; struct maketype *s_c; //Mt *m_c; //Mt *s_c; int i; printf("types Test!\n"); m_c->c0 = 1; printf("types Test! m c0\n"); m_c->c1 = 2; printf("types Test! m c1:%d\n",m_c->c1); m_c->c2 = 3; s_c->c0 = 10; // printf("types Test! s c0: %d\n",s_c->c0); for (i=0;i<4; i++) { printf("Mt c0:%d\n",m_c->c0); // sleep(3); m_c->c0++; } }//end main 2.make or gcc types.c -o types directly 3.run types to see the stranges 4.comment out some lines of codes and/or add some lines of codes as one of the following options: 5.goto step 2. to see the results until all the options has been selected or you don't think further options(adding your own option) are needed: 5.1 if line 16 "struct sigaction sa;" is kept there, using line 17,18 "struct maketype *m_c; struct maketype *s_c;" or line 19,20 "Mt *m_c; Mt *s_c;", the returning is: 5.2 if line 16 is commented out, the returning is ok; 5.3 if line 19,20 "Mt *m_c; Mt *s_c;" are commented out and line 12, 13 "Mt *m_c; Mt *s_c;" or line 9,10 "struct maketype *m_c,*s_c;" remains there, the returning is: Actual Results: * if line 16 "struct sigaction sa;" is kept there, using line 17,18 * "struct maketype *m_c; struct maketype *s_c;" or line 19,20 * "Mt *m_c; Mt *s_c;", the returning is: [trouble:759]$ make clean rm -f *.o types [trouble:760]$ make gcc -c -o types.o types.c gcc types.o -o types [trouble:761]$ types types Test! Segmentation fault [trouble:762]$ * if line 16 is commented out, the returning is: [trouble:768]$ make clean rm -f *.o types [trouble:769]$ make gcc -c -o types.o types.c gcc types.o -o types [trouble:770]$ types types Test! types Test! m c0 types Test! m c1:2 Mt c0:1 Mt c0:2 Mt c0:3 Mt c0:4 Aborted [trouble:771]$ * if line 19,20 "Mt *m_c; Mt *s_c;" are commented out and line 12,13 * "Mt *m_c; Mt *s_c;" or line 9,10 "struct maketype *m_c,*s_c;" * * remains there, the returning is: [trouble:771]$ make clean rm -f *.o types [trouble:772]$ make gcc -c -o types.o types.c gcc types.o -o types [trouble:773]$ types types Test! Segmentation fault [trouble:774]$ Expected Results: [trouble:770]$ types types Test! types Test! m c0 types Test! m c1:2 Mt c0:1 Mt c0:2 Mt c0:3 Mt c0:4 [trouble:771]$ Additional info:
*** Bug 76986 has been marked as a duplicate of this bug. ***
Try to use gcc -Wall -O on the test, gcc will tell you what's wrong. Dereferencing uninitialized pointers is always a bad idea.
I got it. Thank you very much. I focused on struct while ignoring pointer.