r**u 发帖数: 1567 | 1 Here's a common scenario: source file first.cpp define a static data member
of a class and provided an initializer for it. Source file second.cpp #
includes first.h and attempts to examine the value of the static data member
. When you execute that statement, the program crashes.The reason is the
compiler doesn't guarantee that C::i is initialized before it is referenced
in the second.cpp. Here is example:
//----first.h
class C
{
public:
static const int i;
};
//----first.cpp
#include "first.h"
const int C::i=5;
//----second.cpp
#include "first.h"
int MIN=C::i; // may crash here
Questions:
1. Do we have this problem in C too or only in C++? I have never noticed
this problem in C.
2. This seems a pretty serous problem. How to avoid it? E.g., if we use
singleton, how to make sure that the instance is initialized before use it? |
s**********g 发帖数: 139 | 2 define a static function geti and use that. make i private.
member
member
referenced
【在 r**u 的大作中提到】 : Here's a common scenario: source file first.cpp define a static data member : of a class and provided an initializer for it. Source file second.cpp # : includes first.h and attempts to examine the value of the static data member : . When you execute that statement, the program crashes.The reason is the : compiler doesn't guarantee that C::i is initialized before it is referenced : in the second.cpp. Here is example: : //----first.h : class C : { : public:
|
r**u 发帖数: 1567 | 3 Thanks. Do we have this problem in C too or only in C++?
member
member
referenced
【在 r**u 的大作中提到】 : Here's a common scenario: source file first.cpp define a static data member : of a class and provided an initializer for it. Source file second.cpp # : includes first.h and attempts to examine the value of the static data member : . When you execute that statement, the program crashes.The reason is the : compiler doesn't guarantee that C::i is initialized before it is referenced : in the second.cpp. Here is example: : //----first.h : class C : { : public:
|
d****i 发帖数: 4809 | 4 Since C does not have class, static variable in C has different meanings
than in C++. So static variable in C means either 1) internal linkage (file
scope) 2) local static variable retains the value between function calls.
【在 r**u 的大作中提到】 : Thanks. Do we have this problem in C too or only in C++? : : member : member : referenced
|
r**u 发帖数: 1567 | 5 Thanks.
I tried the following code.
File1.c
int x = 1;
File2.c
#include
extern int x;
int y = x + 1;
int main() {
printf("x: %d y: %d\n", x, y);
}
If compile with gcc, it gives an error "initializer element is not constant"
. But it compile with g++. I guess in C it's not allow to initialize a
global variable from another variable. So no dependency problem in C.
file
【在 d****i 的大作中提到】 : Since C does not have class, static variable in C has different meanings : than in C++. So static variable in C means either 1) internal linkage (file : scope) 2) local static variable retains the value between function calls.
|
s**********g 发帖数: 139 | 6 C requires static initialization to be constant, i.e. known at compile time.
When the C compiler compiles File2.c, it doesn't know what x is, thus can
not initialize y.
Different from C, C++ allows static to be lazy initialized the first time it
's used at runtime. So if you put the static in a function, it gets
initialized the first time the function is called. By doing this, it
guarantees the static is always initialized before use. |
r**u 发帖数: 1567 | 7 Thanks. It is much clear now.
time.
it
【在 s**********g 的大作中提到】 : C requires static initialization to be constant, i.e. known at compile time. : When the C compiler compiles File2.c, it doesn't know what x is, thus can : not initialize y. : Different from C, C++ allows static to be lazy initialized the first time it : 's used at runtime. So if you put the static in a function, it gets : initialized the first time the function is called. By doing this, it : guarantees the static is always initialized before use.
|
d****n 发帖数: 1637 | 8 //put this into main()
int main(){
int C::i=1; // will be okay
}
【在 r**u 的大作中提到】 : Thanks. It is much clear now. : : time. : it
|