[RFC] Proper initialization of class members and variables

Stefan Teleman steleman at nyc.rr.com
Sat Nov 19 17:51:33 GMT 2005


On Saturday 19 November 2005 09:14, Luciano Montanaro wrote:
> Yes, but for object instances, the constructor is called.
> If the m_* variables in the patch are member variables, these should be
> initialized in the constructor. If the object does not have an explicit
> constructor, the default one clears the object to zero, if I remember
> correctly. Otherwise the constructor has to initialize the member variables
> itself.

i think you mean "if the object does not have a declared default constructor", 
the compiler will automatically generate one and call it for these object 
members, strictly in the order in which these members have been declared in 
the class declaration. if a member initialization list has not been 
explicitly implemented, the default constructor for these objects will be 
called, and no other constructor.

for non-static members, no intialization to 0 or any other value is ever 
performed, for any type. the memory manager is not required to allocate space 
from any known memory page or block. it is not even required to allocate from 
heap (which is implementation-specific), since C++ refers to dynamically 
allocated storage as "Free Store". it is however very likely that the memory 
block returned by the allocator comes from a page which has been marked as 
"free" on the allocator's free list, but has never been cleared to 0, or any 
other value (marking a page as "free" consists only of flipping a bit). this 
memory block returned by the allocator will very likely be on a page which is 
also on its MRU list, thereby avoiding a page fault, and is therefore 
guaranteed to contain garbage.

--Stefan

-- 
Stefan Teleman          'Nobody Expects the Spanish Inquisition'
steleman at nyc.rr.com                          -Monty Python




More information about the kde-core-devel mailing list