This is the mail archive of the mailing list for the Cygwin project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: gcc - static pointer initialization problem

Alan Cooley wrote:


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.



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.


Unsubscribe info:
Problem reports:

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]