由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - static initialization dependency c++
相关主题
C++: Static initialization dependency error LNK2001:的错误如何改正?
c++ 不自动initialize变量么?抠字眼:assignment and initialize in C++
static vector 怎么 initialize ?a question about CAST
关于C/C++里的Static variable的memory allocation/initializac++标准函数传递一问
码工试题 (转载)[合集] 为什么 const member 不能是 static.
c++ initialize structC++里面如何最方便的表示这个数组的数组?
Test your C++ knowledge...再问C++初始化问题。
一道 memset in C++的题Learn C++ 11 in 20 Minutes (视频)
相关话题的讨论汇总
话题: static话题: c++话题: int话题: here
进入Programming版参与讨论
1 (共1页)
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

1 (共1页)
进入Programming版参与讨论
相关主题
Learn C++ 11 in 20 Minutes (视频)码工试题 (转载)
pthread_create inside a constructorc++ initialize struct
请教大家一道C的面试题Test your C++ knowledge...
问个copy constructor的问题一道 memset in C++的题
C++: Static initialization dependency error LNK2001:的错误如何改正?
c++ 不自动initialize变量么?抠字眼:assignment and initialize in C++
static vector 怎么 initialize ?a question about CAST
关于C/C++里的Static variable的memory allocation/initializac++标准函数传递一问
相关话题的讨论汇总
话题: static话题: c++话题: int话题: here