1

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?

ja_him
  • 417
  • 1
  • 4
  • 20
  • 2
    At least even that site [uses `int main()`](http://www2.research.att.com/~bs/bs_faq2.html#void-main). – chris Jul 18 '12 at 16:13
  • I'm assuming from your link you're offering a suggested correction? I used that site as a reference after reading SO questions. Do you have better references you would like to suggest? – ja_him Jul 18 '12 at 16:16
  • @ja_him he means that your program is incorrect for using `void main()` instead of `int main()` – Seth Carnegie Jul 18 '12 at 16:17
  • 2
    Seth is right. As for cplusplus.com itself, it's known to contain errors; most people prefer http://en.cppreference.com/w/cpp as a reference. – chris Jul 18 '12 at 16:18
  • OK, well thanks for the heads up @chris – ja_him Jul 18 '12 at 16:19

2 Answers2

6
  • 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).

Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
1
  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.
Linuxios
  • 34,849
  • 13
  • 91
  • 116