Therefore, for any. Therefore the first. You're certainly right about this. I got myself confused again. But after reading the link by Pete Becker, I think I've understood how this is done. Actually the "default" definition of the cout, cin, Thus, for every.
Note that there is no guarantee that cout be initialized before the object A a in main. In other words, if by any chance, the A a object is constructed before the initializations made in A.
How come the code always executes? The obvious answer: VS doesn't use the scheme above to guarantee these initializations. My question then is : what is the method used by compilers in general to achieve this result, even in the special cases shown by the example above? What do you mean by "this"? The standard makes no bounds on what undefined behavior may result in.
It could be anything from insidiously working up to fire and brimstone coming down from the skies, rivers and seas boiling. Forty years of darkness, earthquakes, volcanoes, human sacrifice, dogs and cats living together, mass hysteria. As to when cout is initialized, well that's not defined. Cout's initialization falls under the broad category of dynamic initialization and between translation units roughly a cpp file and all it includes you don't know except that it happens before main is entered.
Now I'm away from my visual studio installation so I can't tell you the Microsoft behavior, but it would be dangerous to rely on it because Microsoft would be free to change it at anytime perhaps without you having to rebuild your code, they might make a security patch to the runtimes and bust it there.
The initializaiton of the statically allocated ostream cout follows the general rules for dynamic initialization. Your Init class is in a dynamic initialization in your tranlation unit let's just call it Init. The dynamic initializaiton in any given translation unit is done in the order of definition.
So you have no guarantee that cout is intialized before Init runs because there is nothign that says iostream. The implementation is free to do it in either order.
It's not even required to be consistant from execution to execution. In the case where Init. While your program doesn't necessarily ALWAYS exhibit undefined behavior, it isn't guaranteed to always be defined behavior. You're playing Russian Roulette with the order of initializaiton.
This was really a very good answer. I can see now that the code I presented above exhibits UB. However, in response to one of my previous questions you said:. But I'm really intrigued on how the code above executes successfully on VS not on GCC , no matter the order in which the.
Could you elaborate a little bit more on what you said above regarding "the pointers to initializer functions being put in different sections, Which part of my answer above is unclear? Somehow, MSVC ensures that global variables defined by the STL implementation are initialized before any global variables defined by the program. How exactly it ensures that, I don't know. With this trick, you can effectively build an array of pointers at link time. ATL uses the same trick for similar purposes.
So my guess is, perhaps there's some possibly undocumented compiler option that instructs the compiler to place pointers to initializer functions into a section named, say,. I just made up the named for exposition, but the point is, the section with CRT global initializers is alphabetically ahead of the section with "normal" initializers in your program, and so the linker arranges them in a way that CRT initializers get called first.
Once again, this is just a guess. I don't understand this machinery very well nor do I particularly care, as long as it works as advertized. I may be completely off. Learn More. Ask a question. Quick access. Search related threads. Remove From My Forums. Answered by:. Archived Forums. C Standards, Extensions, and Interop. This forum covers all standardized languages, extensions, and interop technologies supported by Visual C.
Sign in to vote. First extern istream cin; extern ostream out; are just declarations. Friday, February 15, PM. Igor Tandetnik. If the last object is destroyed, cout and friends are flushed calling. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams?
Collectives on Stack Overflow. Learn more. Asked 10 years, 11 months ago. Active 10 years, 11 months ago. Viewed 1k times. So std::cout is an object. Improve this question. Vikas Vikas 8, 3 3 gold badges 37 37 silver badges 47 47 bronze badges. In the library somewhere that you don't have easy access to. Remember that you're not guaranteed anything about how it's constructed, just that it is and what it'll do — Robert.
Add a comment. Active Oldest Votes. Improve this answer. Johannes Schaub - litb Johannes Schaub - litb k gold badges silver badges bronze badges. Sign up or log in Sign up using Google.
0コメント