Topic: Q: Placement and initialization


Author: harald@bion.kth.se (Harald Winroth)
Date: 13 Mar 1994 01:44:07 GMT
Raw View
If I use placement syntax to construct an object at a certain address, e.g.

 new (addr) X(17);

is it guaranteed that nothing is written into that address before the
X(int) constructor gets called? Or is a compiler allowed to generate
code that, say, sets the memory location to zero *before* the control
is given to the constructor? (This has consequences if, for example,
the address represents a special register in memory-mapped hardware
which detects every read/write operation.)  I have not been able to
find anything about this in ARM. Will the standard say anything about
this?

-----------------------------------------------------------------------------
Harald Winroth     | Computational Vision and Active Perception Laboratory,
harald@bion.kth.se | Royal Institute of Technology, S-10044 Stockholm, Sweden
-----------------------------------------------------------------------------




Author: kanze@us-es.sel.de (James Kanze)
Date: 22 Mar 1994 17:25:42 GMT
Raw View
In article <HARALD.94Mar13024407@marilyn.bion.kth.se>
harald@bion.kth.se (Harald Winroth) writes:

|> If I use placement syntax to construct an object at a certain address, e.g.

|>  new (addr) X(17);

|> is it guaranteed that nothing is written into that address before the
|> X(int) constructor gets called? Or is a compiler allowed to generate
|> code that, say, sets the memory location to zero *before* the control
|> is given to the constructor? (This has consequences if, for example,
|> the address represents a special register in memory-mapped hardware
|> which detects every read/write operation.)

I believe that the compiler would be allowed to clear the memory to
zero before calling the constructor, independantly of whether
placement new is used or not.  From the compiler point of view, before
the constructor is called, there is just raw memory.  For that matter,
note that the memory layout isn't really guaranteed, particularly if
there is a constructor.

|> I have not been able to
|> find anything about this in ARM. Will the standard say anything about
|> this?

I doubt it.  Note that even in C, the compiler was free to insert
arbitrary padding in a struct, anywhere after the first element.

If you are really trying to access memory mapped hardware, my
suggestion would be to use a wrapper class, pass the physical
addresses as parameters to the constructor, and deference pointers to
basic data types for the actual accesses.  (You should also consider
some astute use of static class members to ensure that there are not
multiple instances driving a single device.)

Alternatively, such a program will never be portable in the strictest
sense of the word (to all conceivable hardware), so you might just
want to verify what your compiler does, and live with that.  (The
probability of a compiler actually doing something to the memory
before calling the constructor, supposing no virtual functions, no
virtual bases, etc. is extremely remote.)
--
James Kanze                       email: kanze@lts.sel.alcatel.de
GABI Software, Sarl., 8 rue du Faisan, F-67000 Strasbourg, France
Conseils en informatique industrielle --
                   -- Beratung in industrieller Datenverarbeitung