Value at non-initiated pointer

Jul 12, 2012 at 11:55pm
I have a question regarding the value of a newly assigned integer for dynamic memory allocation.

I have a piece of example code that creats a new int and then displays the address location and the value the address is holding. Before running this piece of code for the first time I was curious to see if a null value came out, but this was not the case. 94201 or something came out as the value. Can someone explain why a non-initiated dynamically allocated integer has a random value?

Here is the code:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

int main()

{

    int *pointsTo= new int;

    cout << "Address: " << pointsTo << endl;
    cout << "Value: " << *pointsTo << endl;

    delete pointsTo;

    return 0;
}


THanks in advance!!!

-primez
Jul 13, 2012 at 2:01am
For the same reason that a regular int would have a garbage value. The OS just gives it to you, it's your job to do something with it.
Last edited on Jul 13, 2012 at 2:01am
Jul 13, 2012 at 3:05am
Thanks for the reply! Makes sense now.
Topic archived. No new replies allowed.