[RFC] Proper initialization of class members and variables

Luciano Montanaro mikelima at cirulla.net
Sat Nov 19 14:14:02 GMT 2005


On Saturday 19 November 2005 12:59, Thiago Macieira wrote:
> Christian Ehrlicher wrote:
> >I get a lot of trouble because of unitialised variables when using msvc.
> >This is because gcc seems to auto-initialize all vars with '0' but msvc
> >does not (in debug it fills all with 0xcd).
> >My question is if autoinitialization is a c++-standard (and msvc ignores
> >it) or if this is a gcc feature.
> >If this is a gcc feature, you should avoid it and initialize all
> >variables before use because you never know how other compilers
> >initialize the memory (and imho it's not a good coding style to not
> >initialize variables before use).
>
> Stack and heap POD variables are not auto-initialised to 0 or anything
> else.
>

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.


> To rely on them being so is an error and they must be flushed out.
>
> The difference here is the memory allocator. Apparently, operator new on
> Linux returns newly-allocated pages from the OS more frequently (those
> are initialised to 0 by the OS).
>
malloc (or new) does not clear pages, but the OS clears them before allowing 
the program to see them, for security reasons. And there are objects that are 
allocated on the stack...

> I find it difficult to believe Windows would give one program's page to
> another without zeroing it out first.

-- 
Luciano Montanaro //
                \X/ mikelima at cirulla.net




More information about the kde-core-devel mailing list