Hi there,

Can anyone tell me why variables can have printed values of -858993460.

I've got a pointer pointing to a memory address which has nothing in it so when i come to cout this it prints -858993460, if i give it a value then its prints that value. I had presumed this is "garbage" (whatever is in that chunk of memory) but another empty variable at a different memory location can have the same value.

Is it the size in bits of memory created for an int?
Is it a microsoft-ism thats supposed to tell that that block of memory is empty?

Many Thanks

Alex

Maybe you should post your code...

>>Can anyone tell me why variables can have printed values of -858993460.
Ever heard of "Garbage In, Barbage Out"? (pun intended)

>>Is it the size in bits of memory created for an int?
No, it can be *any* data left in the memory. Data is stored as binary in memory. You read a chunk - You get that int value.

>>Is it a microsoft-ism thats supposed to tell that that block of memory is empty?
First, there is no such thing as an empty memory. Programmers generally use 0 depict that memory is unallocated. It's a matter of preference. You may use -1 if the program logic doesn't use it in any other way. You cannot differentiate between a memory with 0 in it (empty) or a memory with 1337 in it. Some compilers will however initialize the default values of int & bool to 0 if you haven't explicitly done so.

When you create a variable, it is assigned a random location in memory. It doesn't matter what's in that memory, it's now assigned that.

int i;

i is now a 32 bit variable. those 32 bits are given to i from anywhere in the memory. anything could be in those 32 bits. Let assume the bits in i are the following:
11100101 10100111 10011110 10111001

I have no idea what number that is, since I typed it in randomly, but now we do this in our code.

i = 0;

We just did this to the 32 bits:
00000000 00000000 00000000 00000000

Basically, a memory address never has nothing in it. It either has what was in it before, or what you assigned it to after. Now when you create a pointer

int *pointer;
cout "Pointer = " << *pointer;

This either causes an error, or displays what pointer is pointing at. I don't have a compiler, but either way, it's bad.

If you do it like this:

int v;
int *pointer = &v
cout "Pointer is at: " << &pointer;

This will produce the memory location in which pointer is pointing at.

I think I get it, so essentially its garbage, stuff left over from previous “ doings”. Below it the code that produces the empty variable:

int buff;                  //buff is empty, if assigned a val, that is the printed 
int * ted = &buff;		//assigning the pointer a momory location
int alex = *ted;		//telling the pointer to go off and find the memory address and return whatevers in it and assign it to alex

if (alex == -858993460){ cout<<"Memory location " <<&buff<<" holds "<<"NOTHING"<<N;}
else { cout<<"Memory location " <<&buff<<" holds "<<alex<<N; }

The problem is when I create another variable, say int buff2, and do the same thing as above it also comes back with a printed value of -858993460, so how can two memory “chunks” hold the same garbage?

Please feel free to let me know if i'm barking up the wrong tree here as i'm farly new to c++.

Thanks

Alex

Could it be that all your never-before-used memory blocks have the same value? It's default "empty" value so to speak.

Try it with different type of variables like a char and see if all the chars also have the same value.

You will get a similar effect with any numeric value that is uninitialized. What the reported value represented is depends on the data type. The thing you need to remember is that this isn't actually a valid value.

You didn't specify exactly which compiler you're using, but you alluded to using a Microsoft Visual Studio product. I get the same "values" and effect as you in VS2008. I've never been able to confirm this, but I suspect these "default" values are how Microsoft's debugger system interprets some sort of NaN representation. If you try to use them without initializing them, there will be application issues. In other compilers/IDEs the behavior will be different but similar.

In much the same way, pointers in VC++ always initialize to 0xcccccccc. If you try to use this pointer, you will crash your program.

#include <iostream>

using namespace std;

int main ()
{
  int ia(-858993460), ib, ic, id;
  double da, db, dc, dd;
  char ca, cb, cc, cd;

  cout << "ia = " << ia << endl;
  cout << "ib = " << ib << endl;

  return 0;
}

If you try to compile and run this on a MS-VS product, it will compile, but it will crash as soon as it hits Line 12 because only ia is initialized. Code::Blocks (using MinGW/GCC), on the other hand, will assume that ib is the value 2147483647, which is the maximum value a signed 32-bit int can hold.

Hmm intriguing:
unsigned longs return: 3435973836
bool:204
Floats:-1.07374e+008
Doubles: 9.25596e+061
Short ints: -13108
Unsigned short ints: 52428

Yes I’m running MVS express 2010 on a 64 bit machine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.