Board index » cppbuilder » Initialization of a struct, false variable...
mauro
![]() CBuilder Developer |
mauro
![]() CBuilder Developer |
Initialization of a struct, false variable...2005-12-08 07:48:03 PM cppbuilder89 Hi all, following global structure: struct Nachricht { int integer; bool gelesen; }; Then in one of my functions: Nachricht news; When I had a look on the 2 variables I have seen that the integer variable has allways undefined values. But the bool variable seems to have allways the value FALSE. Can I confidence that the bool variables have allways the value FALSE or not?? Thanks Marius |
mauro
![]() CBuilder Developer |
2005-12-08 07:57:57 PM
Re:Initialization of a struct, false variable...
It is also done randomly as I have noticed...
|
Hans Galema
![]() CBuilder Developer |
2005-12-08 09:09:29 PM
Re:Initialization of a struct, false variable...
mauro wrote:
QuoteNachricht news; Hans. {smallsort} |
Chris Uzdavinis
![]() CBuilder Developer |
2005-12-08 09:38:47 PM
Re:Initialization of a struct, false variable...
"mauro" < XXXX@XXXXX.COM >writes:
QuoteHi all, I've seen one program that depended on an uninitialized boolean value that worked correctly for over a year before a failure occurred. Some maintenance coding in totally unrelated section somehow changed the value that aligned with the variable, and it flipped. Very surprising, and not easy to track down. -- Chris (TeamB); |
Andrue Cope [TeamB]
![]() CBuilder Developer |
2005-12-08 09:49:45 PM
Re:Initialization of a struct, false variable...
mauro wrote:
QuoteThen in one of my functions: eg; struct Nachricht { int integer; bool gelesen; Nachricht() { integer=0; gelesen=false; } }; ..or slightly better especially for nonbuilt-in types: Nachricht() :integer( 0 ), gelesen( false ) {;} This is better because without it more complicated types (such as class members) will first be constructed and then assigned. The above code makes the construction and assignment one operation. For built-in types this isn't an issue since construction is a 'no-operation' but for classes the saving can be significant. -- Andrue Cope [TeamB] [Bicester, Uk] info.borland.com/newsgroups/guide.html |
Duane Hebert
![]() CBuilder Developer |
2005-12-08 10:38:20 PM
Re:Initialization of a struct, false variable...
"Hans Galema" < XXXX@XXXXX.COM >wrote in message
Quotemauro wrote: Does this do doh = {0} ? |
Alan Bellingham
![]() CBuilder Developer |
2005-12-08 10:41:41 PM
Re:Initialization of a struct, false variable...
"Duane Hebert" < XXXX@XXXXX.COM >wrote:
QuoteWhat would happen if one of the members is -- ACCU Conference 2006 - 19-22 April, Randolph Hotel, Oxford, UK |
Chris Uzdavinis
![]() CBuilder Developer |
2005-12-08 11:19:01 PM
Re:Initialization of a struct, false variable...
"Andrue Cope [TeamB]" < XXXX@XXXXX.COM >writes:
Quotemauro wrote: Quotebut it's better IMO to give them their own constructors adding a constructor to a struct means that you can no longer use "offsetof" on that struct. But I frequently find structs migrate into classes anyway, and so we might as well start off with them that way if there is any suspicion that it may happen. -- Chris (TeamB); |
Andrue Cope [TeamB]
![]() CBuilder Developer |
2005-12-09 12:14:23 AM
Re:Initialization of a struct, false variable...
Chris Uzdavinis (TeamB) wrote:
QuoteWith a constructor, they're not longer POD-structs. QuoteFor one example, share them with external libraries. If they aren't POD in theory then BCB is using some clever magic and they are POD in practice. QuoteBut I frequently find structs migrate into classes anyway, and so we intrinsic intelligence uses 'class'. -- Andrue Cope [TeamB] [Bicester, Uk] info.borland.com/newsgroups/guide.html |
Chris Uzdavinis
![]() CBuilder Developer |
2005-12-09 12:55:30 AM
Re:Initialization of a struct, false variable...
"Andrue Cope [TeamB]" < XXXX@XXXXX.COM >writes:
QuoteChris Uzdavinis (TeamB) wrote: be initialized with memset, and if there's a constructor, memset would bypass the beginning of the object's life. Further, if there are externally-visible side-effects, they won't happen either. A POD-struct is defined in terms of an aggregate with additional limitations. Class-type aggregates cannot have constructors, among other things. Quote>For one example, this being a real problem, if all you have is a constructor, is probably pretty small. It's still worth noting that this is a potential problem, in case it ever breaks you can't complain. :) QuoteWe even share them with external libraries. If they aren't POD in Quote>But I frequently find structs migrate into classes anyway, and so we remind myself about how often this happens and wonder why I keep falling into the same trap! -- Chris (TeamB); |
mauro
![]() CBuilder Developer |
2005-12-09 03:06:05 AM
Re:Initialization of a struct, false variable...
Hmm, the discussion sounds very strange :o)
|
Alisdair Meredith[TeamB]
![]() CBuilder Developer |
2005-12-09 05:31:53 AM
Re:Initialization of a struct, false variable...
Chris Uzdavinis (TeamB) wrote:
QuoteFor POD-structs you can use aggregate initialization syntax, but not struct Test { std::string data; }; int main() { Test t = { "Hello World" }; } Note: due to a bug in BCB6 this will indeed fail to compile. Try it in BCB2006(preview) or a more modern compiler, and you might want to rephrase your statement ;?) -- AlisdairM(TeamB) |
maeder
![]() CBuilder Developer |
2005-12-09 06:13:24 AM
Re:Initialization of a struct, false variable...
"Duane Hebert" < XXXX@XXXXX.COM >writes:
Quote"Hans Galema" < XXXX@XXXXX.COM >wrote in message As assignment? No. doh = {0}; // error |
Old Wolf
![]() CBuilder Developer |
2005-12-09 06:39:35 AM
Re:Initialization of a struct, false variable...
"Alisdair Meredith[TeamB]" wrote:
QuoteChris Uzdavinis (TeamB) wrote: All PODs are aggregates, but not vice-versa. You can use aggregate initialization syntax on any aggregate, whether or not it is a POD. From ISO C++98 8.5.1: An aggregate is an array or a class (clause 9) with no userdeclared constructors (12.1), no private or protected nonstatic data members (clause 11), no base classes (clause 10), and no virtual functions (10.3). From 9#4: A POD-struct is an aggregate class that has no nonstatic data members of type pointer to member, non-POD-struct, nonPODunion (or array of such types) or reference, and has no userdefined copy assignment operator and no userdefined destructor. |
Duane Hebert
![]() CBuilder Developer |
2005-12-09 09:15:24 AM
Re:Initialization of a struct, false variable...
"Thomas Maeder [TeamB]" < XXXX@XXXXX.COM >wrote in message
Quote"Duane Hebert" < XXXX@XXXXX.COM >writes: int x; double y; double doh[42]; }; Then spoo foo = {0}; Is it guaranteed that doh will be initialized to all zeroes? With MSVC7.1 this is the case. I wanted to know if this was guaranteed behavior. From Alan's response, I take it that it is guaranteed. I know that saying double doh[42] = {0}; works. I wasn't sure about when the array was a member of a structure as I show above. Thanks. |