Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT cygwin DOT com Mail-Followup-To: cygwin AT cygwin DOT com Delivered-To: mailing list cygwin AT cygwin DOT com Message-ID: <3F32EC34.3080100@alltel.net> Date: Thu, 07 Aug 2003 19:17:56 -0500 From: Ken Dibble User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.0.2) Gecko/20030208 Netscape/7.02 X-Accept-Language: en-us, en MIME-Version: 1.0 To: cygwin AT sources DOT redhat DOT com Subject: Re: gcc - static pointer initialization problem References: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Alan Cooley wrote: >Hello, > >I am having a problem with a C program. It appears that static variables >are not being initialized when in an object file. The program compiles and >links fine but funcPtrStruct.ptr is null so program crashes. > >This program works using gcc on Linux and (as I understand the C language) >should work anywhere. > >Thanks in advance for any explanation/corrections you can provide. > >Alan. > > > FWIW: The cygwin list is probably not the best resource for resolving this. The program should crash. There are multiple problems here. My opinion is that variables should not be declared in header files. This leads to ambiguity when the header files declare variables which conflict with source files. My opinion is that all warnings should be turned on all the time. Had CFLAGS been declared to be -g -O -Wall the following would have resulted: $ make gcc -g -O -Wall -c -o static_function_ptr_def.o static_function_ptr_def.c static_function_ptr_def.c: In function `func': static_function_ptr_def.c:4: warning: implicit declaration of function `printf' static_function_ptr_def.c: At top level: static_function_ptr_def.c:8: conflicting declarations of `funcPtrStruct' static_function_ptr_def.h:10: `funcPtrStruct' previously declared here static_function_ptr_def.h:10: warning: `funcPtrStruct' defined but not used make: *** [static_function_ptr_def.o] Error 1 Which would have given a clue that something wasn't right. That this program ever worked anywhere in any context is probably an accident (and maybe a linker bug?). When static_function_ptr.c compiled it rightly picked up funcPtrStruct from the header file whis is not initialized. When static_function_ptr_def.c compiled it picked up funcPtrStruct(which is not initialized) from the header file which conflicts with funcPtrStruct(which is initialized) that it declares. In any event the funcPtrStruct in static_function_ptr_def.c is never used (and shouldn't be). Bottom line, bad programming, correct result. Regards, Ken -- Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple Problem reports: http://cygwin.com/problems.html Documentation: http://cygwin.com/docs.html FAQ: http://cygwin.com/faq/