I'm building my program (tests actually) using some static library.
This library contains one file inside which I have functions like that:
string& GetString() {
static string strFilename;
return strFilename;
}
void PrintToScreen() {
printf("String: %s\n", GetString().c_str())
}
Then in my main.cpp (outside the library) I'm doing:
GetString() = "abc";
printf("String: %s\n", GetString().c_str());
PrintToScreen();
And I get this output:
String: abc
String:
So looks like second call to the function
(but done from different file, which is inside the library)
somehow clear previous value, reinitialize it, or uses own copy of it.
I changed GetString function to use 'new' but result is exactly the same (btw. program never crash).
But I don't understand hot it's possible?
Any ideas what I'm doing wrong?
------------------------------- UPDATE ------------------------------
- Test is done is single threaded environment.
- It works on some platforms and on some it doesn't (works on windows, MacOS and AIX, doesn't work on linux, HP_UX, Solaris, FreeBSD...)
- I verified address of the strFilename during the execution (printf inside GetString) and looks like it's one variable without duplicates (address is always the same)
- BUT, with nm on the final lib I get something like that:
0000000000000030 T _Z16GetLogprintfFilev
0000000000000008 b _ZGVZ16GetLogprintfFilevE16strLogprintfFile
0000000000000018 b _ZZ16GetLogprintfFilevE16strLogprintfFile
U _Z16GetLogprintfFilev
and with nm on my base lib (used by final lib) I get:
0000000000000030 T _Z16GetLogprintfFilev
0000000000000008 b _ZGVZ16GetLogprintfFilevE16strLogprintfFile
0000000000000018 b _ZZ16GetLogprintfFilevE16strLogprintfFile