|
[A5] al_ustr_empty_string() questions |
axilmar
Member #1,204
April 2001
|
1) Should the result of al_ustr_empty_string() be freed? 2) Is this valid code? ALLEGRO_USTR *str = al_ustr_empty_string(); ... al_ustr_free(str);
|
X-G
Member #856
December 2000
|
1) Quote: Return a pointer to a static empty string. The string is read only. 2) Valid C, but wrong. -- |
tobing
Member #5,213
November 2004
|
Shouldn't the return of al_ustr_empty_string(); better be a const ALLEGRO_USTR* then? |
X-G
Member #856
December 2000
|
You'd think so, wouldn't you? -- |
tobing
Member #5,213
November 2004
|
Yes, that's what I would have thought. Or said. Or whatever. |
axilmar
Member #1,204
April 2001
|
Quote: Return a pointer to a static empty string. The string is read only. The above is so vague. It can be interpreted in many ways:
I think there is a problem with this function; the returned string should be available to free, since it cannot be shared. |
Elias
Member #358
May 2000
|
The return should be const, yes. axilmar said: The above is so vague. It can be interpreted in many ways: Only if you overlook the "static" in there. But yes, always better to be explicit, maybe someone can make a patch which adds something like "The string is read only and must not be freed.". -- |
axilmar
Member #1,204
April 2001
|
What does 'static' refer to in this context? is the string buffer static or the ALLEGRO_USTR instance static? or both? |
Elias
Member #358
May 2000
|
Yeah, as I said, someone should make it more clear. It means both apparently. -- |
Matthew Leverton
Supreme Loser
January 1999
|
When in doubt, look at the source: 1/* Function: al_ustr_empty_string
2 */
3ALLEGRO_USTR *al_ustr_empty_string(void)
4{
5 static struct _al_tagbstring empty = _al_bsStatic("");
6 return ∅
7}
|
GullRaDriel
Member #3,861
September 2003
|
So it is not thread safe. "Code is like shit - it only smells if it is not yours" |
X-G
Member #856
December 2000
|
Why wouldn't it be? -- |
tobing
Member #5,213
November 2004
|
Because empty will be initialized by the first caller of al_ustr_empty_string(), and it might happen that two threads try to do this at the same time. |
Elias
Member #358
May 2000
|
tobing said: Because empty will be initialized by the first caller It's static, it will be initialized before main(). So completely thread-safe. -- |
tobing
Member #5,213
November 2004
|
It's static, but inside a function. So it's only initialized when the first call to the function occurs. |
bamccaig
Member #7,536
July 2006
|
Are you sure that isn't just C++? In C, I can't even seem to initialize my static local variable with a function... It needs to be a constant expression (which sounds like compile-time to me). In C++, it's no problem. -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
Elias
Member #358
May 2000
|
In C static variables (no matter if inside a function or not) are initialized before main(). [edit:] Found it in the C99 standard, 6.2.4 §3: Quote:
An object whose identifier is declared with external or internal linkage, or with the
-- |
tobing
Member #5,213
November 2004
|
OK, I'm so much used to C++ that I tend to forget that there might so differences like this. Sorry. |
Evert
Member #794
November 2000
|
tobing said: OK, I'm so much used to C++ that I tend to forget that there might so differences like this.
So does that mean that in C++ code, the compiler has to insert code to check whether a static variable has been used before and is therefore initialised already, or needs to be initialised now? Or does it do code-flow analysis to figure out where to insert the initialisation? |
tobing
Member #5,213
November 2004
|
The first. It's checked at runtime if the static is already initialized or not, and the first one coming by does the initialization. When it comes to overhead and what the compiler generates, I don't know... The good thing about this is, you can control the initialization, i.e. when the static is initialized. It's quite simple to implement singletons that way, or control the order of initialization of "global" variables between different compilation units. |
|