Two Different Values at one Memory Address.

Ok so I've decided to fiddle around and learn about the memory addresses. So I wrote a very simple "guess this number" program. And I wrote another program to scan through various memory addresses while the "guess this number" program was running, and dereference the pointer to those addresses to see if I could find the random number. However, when I look through these memory addresses, there's nothing but garbage. I don't understand how a number between 1 and 20 can be located at the same address of a number like 2395849292. (That's just a random number I came up with but you get the idea). I even went so far as to print out the address of the random number in the "guess the number" program and matched it up with the memory address shown in the other program. The values at the same address just don't match.
post your code please.

It would make sense if you use 2 different data types but please show us what you've got, or at least the essential part of the programm.
Last edited on
Maybe that memory address is being used by another program or is just purely uninitialised and so has undefined behaviour.
Here's the "Guess the number" program code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    int random = rand() % 20;
    int guess;
    cout << "Random at " << &random << endl;
    do
    {
        cout << "Guess the number: ";
        cin >> guess;
        if (guess == random)
        {
            cout << "Correct!!!" << endl;
        }

        else
            cout << "Wrong" << endl;
    }while (guess != random);
}
. Here's the memory "scanner" program (if that's what you want to call it)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 #include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int var;
    int *pointer = &var;
    for(int i = 0; i < 10; i++)
    {
        cout << "Pointer points to: " << pointer << ": ";
        cout << *pointer++ << endl;
    }
}
You are first printing the value of the address held in pointer, you are not dereferencing it, then you print the dereferenced version with a postfix increment that has no effect in this program.
What do you mean it has no effect in the program?
Sorry, that was me being an idiot and not reading your program correctly (I missed the for). But the point about dereferencing and not does still stand I believe.
Last edited on
Sorry but I'm still a bit confused about what you're saying. I thought when I print the dereferenced pointer I am actually printing the value of the address held in the pointer. Maybe I'm just confused about the terms here. If so, could you explain?
*pointer++ does 2 things
1.) it dereferences the pointer and gives you the value
2.) it increases THE ADRESS it points to

because of 2.) you get 10 different values

Is this what your confusion is about?
Last edited on
When you print a pointer, that prints the pointer itself, the address of the location in memory that the pointer points to, not the value it points to. It therefore prints the address which is usually a massive number. When you dereference a pointer, it returns the value of what it points to, not where that value is.
Gamer2015: not really. I'm not confused about that. I first print the address of the value, then the value like so
Pointer points to 0x28fefc: 39284950
. That happens 10 different times with 10 different addresses with a difference of 4 bytes from the last, which contain 10 different values all of which are garbage.

shadowmouse, is there a difference between printing the value of the address the pointer points to and dereferencing it?
Last edited on
I thought when I print the dereferenced pointer I am actually printing the value of the address held in the pointer.

You are. But you're moving the address and then dereferencing it.

To persuade operator++ to act on the value pointed to add ()s, e.g. (*pointer)++

So you get

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

using namespace std;

int main()
{
    int var = 0; // init to zero
    int *pointer = &var;
    for(int i = 0; i < 10; i++)
    {
        cout << "Pointer points to: " << pointer << ": ";
        cout << (*pointer)++ << endl; // use brackets
    }

    return 0;
}


Now the pointer stays still and the value increments.

Pointer points to: 0x76ad8d553fcc: 0
Pointer points to: 0x76ad8d553fcc: 1
Pointer points to: 0x76ad8d553fcc: 2
Pointer points to: 0x76ad8d553fcc: 3
Pointer points to: 0x76ad8d553fcc: 4
Pointer points to: 0x76ad8d553fcc: 5
Pointer points to: 0x76ad8d553fcc: 6
Pointer points to: 0x76ad8d553fcc: 7
Pointer points to: 0x76ad8d553fcc: 8
Pointer points to: 0x76ad8d553fcc: 9


To make the pointer movement clearer you can init it to point at an array.

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

using namespace std;

int main()
{
    int var[] = {3,1,4,1,5,9,2,6,5,4}; // use an array rather than an int
    int *pointer = var;
    for(int i = 0; i < 10; i++)
    {
        cout << "Pointer points to: " << pointer << ": ";
        cout << *pointer++ << endl; // use brackets
    }
}


which gives the following output:

Pointer points to: 0013FF50: 3
Pointer points to: 0013FF54: 1
Pointer points to: 0013FF58: 4
Pointer points to: 0013FF5C: 1
Pointer points to: 0013FF60: 5
Pointer points to: 0013FF64: 9
Pointer points to: 0013FF68: 2
Pointer points to: 0013FF6C: 6
Pointer points to: 0013FF70: 5
Pointer points to: 0013FF74: 4


Andy
Last edited on
Gamer2015: not really. I'm not confused about that. I first print the address of the value, then the value like so

Pointer points to 0x28fefc: 39284950

. That happens 10 different times with 10 different addresses with a difference of 4 bytes from the last, and 10 different values all of which contain garbage.

Okey, so you actually wanted it to be like this, right?


I thought when I print the dereferenced pointer I am actually printing the value of the address held in the pointer.

When you print the pointer you print the value of the pointer which is the adress it points to.
When you print the dereferenced pointer you print the value at the location where the pointer points to.

1
2
3
4
int i = 20;
int* p = &i;
std::cout << "address: " << p << std::endl;
std::cout << "value: " << *p << std::endl; // dereferenced 
Last edited on
Andy, first of all I just wanted to say, every time I have a question, you come to help. Thanks man! But yes I did mean to write it the way I did. cout << *ptr++ << endl; was meant to be a shorthand for
1
2
cout << *ptr << endl;
ptr++;
Ok so back to my original question... Why is it that when I peek into the address of the random number of my first program, I only see garbage?

was meant to be a shorthand for

1
2
cout << *ptr << endl;
ptr++;


But then you don't have the same value on the same address because you change the address every time
I did mean to write it the way I did.

Then initializing your pointer to refer to a lone int doesn't make sense.

In a best case scenario it will just end up pointing as random memory when you increment it. But it could end up pointing at inaccessible memeory and cause an access violation/segfault (C++ has no rules about this, but Windows, Linux, etc have ways of controlling access to memory, including making regions read-only.)

Andy
Last edited on
Ok so back to my original question... Why is it that when I peek into the address of the random number of my first program, I only see garbage?


Your pointer in the second program is not pointing to the same memory as the first program. The pointer values don't matter. They are not comparable.

See: http://stackoverflow.com/questions/7266762/accessing-direct-memory-addresses-and-obtaining-the-values-in-c
Gamer, that was the point of the second program. I was trying to examine the memory of the other program to take a peek into the random number. Andy, I initialized it to a lone int in order to be able to look through other memory addresses near the random integer in the first program. I put it on the stack so the address would be near the random number in the first program (I'm on Windows so each time I run the program, the memory address is always the same). Don't worry, I would never do this in a real program but I was just doing this for learning purposes. Cire, OK I see now. That makes sense. Thanks!
Last edited on
I'm on Windows so each time I run the program, the memory address is always the same

If you were using MS-DOS then it would work.

On 32-bit Windows, each process gets it's own virtual space.
You can't access data in the other program by just scanning memory with pointer arithmetic.
Topic archived. No new replies allowed.