Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Weird behavior ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 6 Topic 9802 of 9938
Post > Topic >>

Weird behavior with uninitialized data

by "Hakusa@[EMAIL PROTECTED] " <Hakusa@[EMAIL PROTECTED] > Jul 17, 2008 at 10:38 PM

I know working with uninitialized data is a sin, but I was doing this
experiment where I spawned an int onto the heap and checked it's
address, then deleted it, then redefined it as a new int on the heap.
This behaved as hoped: The address never changed--I believe this is
proof of good decision-making in where to allocate objects, but also a
warning not to use a deleted pointer.

What got weird is when I started toying around with how C++ (or at
least gcc's interpretation) handled uninitialized data:

Integer based types seemed to auto-initialize to some random number
(I'm guessing what was there before they were allocated to that spot).
Then, when I do x++ in the cout, it reads as zero, then the next time
I put it in a cout, another seemingly random number. Interestingly
enough, when I do a += to the x in the cout, it skips printing the
zero and goes straight to the random number--also, if I do the ++
outside the cout. I guess that has more to do with what int++ returns.

Chars were an exception: They initialized at zero (' ') and x++ gave a
predictable 1 ('☺'). What I found odd was that x-- remained at zero.
I'd always been told when a variable reaches it's maximum or minimum,
it "overflows" to the minimum or maximum. (I'll test this another day,
maybe.)

Floats are the ones that scare me. Here's some output of initializing
a float x, then printing it a bunch of times:
5.95081e-39
5.95095e-39
3.30589e-39
5.95081e-39
7.92254e+33
NaN x++
7.92252e+33
7.92252e+33
7.92252e+33

"NaN x++" is where I do x++ inside the cout.

Scary. Not only does it initialize randomly, but it keeps changing
until modified! Doing it with a double was similar, but x++ returned a
valid number and all values beyond that were one.

When I created an object (a struct called A with members a and b), I
found ints and floats alike were initialized to zero and only char*
and strings acted weirdly.

Can anyone supply me with rationality for any of this nonsense?
Particularly, I'm curious about why the float values are ever
changing. Could I base a random function on this?


-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 6 Posts in Topic:
Weird behavior with uninitialized data
"Hakusa@[EMAIL PROTE  2008-07-17 22:38:33 
Re: Weird behavior with uninitialized data
Alberto Ganesh Barbati &l  2008-07-18 04:28:30 
Re: Weird behavior with uninitialized data
"Hakusa@[EMAIL PROTE  2008-07-18 15:06:45 
Re: Weird behavior with uninitialized data
Alberto Ganesh Barbati &l  2008-07-18 22:43:55 
Re: Weird behavior with uninitialized data
brangdon@[EMAIL PROTECTED  2008-07-19 14:37:35 
Re: Weird behavior with uninitialized data
=?iso-8859-2?B?SmFuIFJpbm  2008-07-19 14:38:22 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Aug 27 21:13:12 CDT 2008.