Uninitialized variables

Pages: 12
Apr 3, 2012 at 12:57pm
Can anyone tell me why uninitialized SHORT variables in C++ console applications always default -13108 and INT variables to -858993460.

Just curious.
Apr 3, 2012 at 1:07pm
They don't. They just get whatever left-over memory was there. If you run your tests enough they'll be different each time, or the same, it is not guaranteed.
Apr 3, 2012 at 1:20pm
I've been teaching programming for 30 years now (C++ for the last 12) and these values are consistent for me and every one of the students in the lab. I understand the concept of leftover garbage, it just seems like it gets consistently translated to these values.
Apr 3, 2012 at 1:29pm
Well then, you've consistently got the exact same leftover stuff in memory.
Apr 3, 2012 at 1:32pm
It's not guaranteed. My closest guess is that whatever initialization happened before main was called left those values in the memory every time for the implementation you use. For me in VC++2008 its completely random every time I run it, Debug or Release alike, but other compilers may be different.

http://ideone.com/Oi5vS

I have a feeling it's implementation defined.
Apr 3, 2012 at 1:39pm
It's hex 0xCCCC... Some compiler fill the memory in the debug version with values like that. So that the programmer can recognize not initialzed memory easier
Apr 3, 2012 at 1:42pm
http://en.wikipedia.org/wiki/Magic_number_%28programming%29#Magic_debug_values

It's the VS debug uninitialised local variable value.
Apr 3, 2012 at 3:47pm
Perhaps Moschops last answer is a little more logical than his previous ones
Apr 3, 2012 at 3:53pm
Perhaps you should provide information about your compiler and operating system without being asked next time.
Apr 3, 2012 at 3:57pm
Perhaps Moschops last answer is a little more logical than his previous ones


It's the exact same answer. If you consistently run in MS debug mode, it will consistently dump the same values into memory to mark it as uninitialised.

Perhaps you should know your tools better than you do.
Apr 3, 2012 at 3:57pm
It's not more logical. It's more specific.

Your question was "in a C++ console application", in which case the result is undefined (random). On MVC++, two specific values are used to signify uninitialized stack and heap variables.

I'm shocked that you've been teaching for 30 years without knowing this.
Last edited on Apr 3, 2012 at 3:57pm
Apr 3, 2012 at 5:16pm
@ Gaminic. I'm equally shocked thatyou define a repeating result as being RANDOM?

@Moschops. I'm quite satisfied with my knowledge tools, thankyou, but Consistent and same are hardly adjectives that can be used to describe leftover garbage.

@Athar I wasn't asked this time. Perhaps you were reading a different thread.

Anyway gentlemen. Thanks for your opinions. Sorry if I put some noses out of joint
Last edited on Apr 3, 2012 at 5:17pm
Apr 3, 2012 at 5:35pm
I'm quite satisfied with my knowledge tools, thankyou,

I don't think you are. If you were, you wouldn't have asked this question which is, ultimately, a question about your tools.
Apr 3, 2012 at 5:37pm
Thank you again for your opinion Mos and, again, sorry about your nose
Apr 3, 2012 at 5:39pm
Please also thank me for my statements of fact. Then suck up to me some more, and apologise harder.
Last edited on Apr 3, 2012 at 5:39pm
Apr 3, 2012 at 5:41pm
sorry, I got a lot of contradictory "facts". Made it difficult to find the logical answer I was looking for. Have a nice day.
Last edited on Apr 3, 2012 at 5:45pm
Apr 4, 2012 at 10:50am
I don't care much for your attitude, I must say.
Apr 4, 2012 at 11:17am
So are you the one that reported him? Gam?
Apr 4, 2012 at 11:50am
Sorry if I "Jump in"... But it looks like there was a little flame war here.
Moschops already told the answer. I don't see why you are still arguing about the answer. It's not a random number. It's a specific value depending on your Compiler/OS. Let me say, I already knew this from last year (You clearly can see the hex value in the MSVS's debugger... And it's not a random value). Only a few compilers don't clean up the "Leftover garbage" (I'm not even sure about this). The last 8 posts are almost completely useless, from my Point of View.
Apr 4, 2012 at 12:03pm
I guess simple little me got confused when the answer from mos was both "leftover stuff" (referring to the previous comment--leftover garbage) which would be different every time, and consistently setting uninitialized variables to the same value which, due to its bizzare value, is a good indication to the programmer as to its unitinitialized state.

This debate is, at best, symantical at this point.

So I have the answer to my question for which I will again thank you all and again appologize if I offended you
Pages: 12