Pregunta

In the following code, based on reading cplusplus.com, I'm trying to test my basic understanding on pointers.

#include <iostream>
using namespace std;

int main() {
    int foo, *bar, fubar;
    foo = 81;
    bar = &foo;
    fubar = *bar;
    cout << "the value of foo is " << foo << endl;
    cout << "the value of &foo is " << &foo << endl;
    cout << "the value of bar is " << bar << endl;
    cout << "the value of *bar is " << *bar << endl;
    cout << "the value of fubar is " << fubar << endl;
    cin.get();
}

That leads to the output:

the value of foo is 81
the value of &foo is xx
the value of bar is xx
the value of *bar is 81
the value of fubar is 81

Where xx is some long number that changes at each runtime. When I add the following:

    cout << "the address of foo is " << &foo << endl;
    cout << "the address of fubar is " << &fubar << endl;

It leads to:

the address of foo is xx
the address of fubar is xy

Where xy is different to xx on runtime.

  • Question 1: At the declarations, does the declaration *bar make it a 'pointer' at that moment in time, until it is used i.e. fubar = *bar at which point is it a dereferenced variable? Or is it that a pointer is always a variable and that's just me getting all nooby and tied down in (probably incorrect) semantics?

  • Question 2: xx is a long number that changes each runtime so am I right that it's the address space?

  • Question 3: Am I right in thinking that whilst fubar and foo have the same value, they are completely independent and have no common address space?

¿Fue útil?

Solución

  • Answer 1: Correct. The variable bar is a pointer, and using *bar dereferences the pointer and evaluates to what the pointer is pointing to. Realise though that it doesn't evaluate to a [temporary] copy, but a reference to its target.

  • Answer 2: "Address space" is different from "address"; the address of a variable is simply its location in memory, whereas a program's "address space" is all the memory it has available to use. Variables do not have address spaces, programs do. What you are seeing in the output of your program are addresses (the addresses of foo and fubar).

  • Answer 3: Yes, you are correct. They have the same value, but what happens to one will not affect the other (they are at different addresses and are therefore different objects).

Otros consejos

  1. It is declared as the type int* which is a small amount of memory itself to store the address. A pointer variable is always a pointer. When you use the dereferencing operator, you are getting a reference to the variable who's address you store.
  2. It is the actually address into virtual memory where your variable is stored. They are displayed in hex (most likely). It is not an "address space" (See @Seth's answer)
  3. Yes. When you do the assignment, you are copying the value of foo into fubar. If fubar was a pointer, however, and you assigned bar to it, their dereferenced locations would be identical.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top