[lug] new operator in C++

Peter Hutnick peter-lists at hutnick.com
Mon Apr 29 14:12:53 MDT 2002


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Saturday 27 April 2002 01:57 pm, Stephen Queen wrote:
> In using the new operator in C++ I cannot find whether it is guaranteed
> to initialize the new memory to zero or not. In playing with it, it
> looks like it does, but that might be coincidence. Does anyone know a
> source of information on the net regarding this? Another question
> regarding sources of information on C++. man has section 3 which is a
> good source of information for regular C. Does anyone know of anything
> like that for C++? It sure would come in handy now and then.

Let me preface my comments by saying that I am inexperienced, naive, and 
un-knowledgeable programmer, so when I use question marks below I am really 
asking a question.

Isn't initializing /everything/ within the code the only responsible thing to 
do?  Preferably to a logical nonsense value that you can easily test for?

The sum total of my programing experience is three semesters of C++ and one 
semester of DOS/x86 assembly.  Based on that very limited experience I think 
that they should have taught me asm first, and that you shouldn't trust 
compilers to do what you think they "should" do.  It seems to me that 
compilers are surprisingly inconsistent, and that if the g++ test had shown 
that the memory was zeroed out it would only prove that you could rely on 
that particular version of g++ on that particular platform to zero those 
values out.

That, of course, would have no impact on whether you should code to depend on 
that behavior.

Can anyone tell me where my thinking is off track here?

- -Peter

PS: Stephen, are you by chance a Java programmer?  I'm not trying to be funny, 
but it seems like learning on Java would lead one to think that this is 
something that a compiler "should" do.

At the risk of getting on a philosophical tear, it seems like initializing 
memory is something that a compiler "shouldn't" do.  What is the advantage?  
The odds of the compiler initializing to what ends up being a "good" test 
value for unset variables for your particular use is about nil.  On the down 
side, a function that allocates a bunch of memory, then initializes 
"properly" would take basically twice as long, since the memory would be 
initialized twice, and the actual allocation is comparatively negligible.

- -P

- -- 
/"\ ASCII Ribbon campaign against HTML e-mail
\ /
 X   Get my PGP key at http://hutnick.com/pgp
/ \  6128 5651 6F23 EC17 6EBD  737D 960A 20E6 76CA 8A59
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE8zalKlgog5nbKilkRAqY7AJ4xgLiMxuMWGUA+2SxZ3w43JKkkMwCfY60t
tGVJhYOizYOMwetpfWoBMEs=
=t+jZ
-----END PGP SIGNATURE-----




More information about the LUG mailing list