Question concerning vector of pointers

Dec 29, 2015 at 2:59am
Why does the following print out different memory addresses for the bullet object
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  	vector<Bullet*> Gun;
	Gun.push_back(&bullet);
	Gun.push_back(&bullet);
	Gun.push_back(&bullet);
	Gun.push_back(&bullet);

	const Bullet * ptr = &bullet;
	int count = 0;

	cout << "Original bullet object: " << ptr << endl;
	cout << "bullet object address: " << &bullet << endl;
	cout << "  " << endl;
	cout << "  " << endl;

	for(vector<Bullet>::const_iterator itr = Gun.begin(); itr != Gun.end(); itr++)
	{
		count++;
		cout << "Address " << count << " :" << &(*itr) << endl;
	}


shouldn't the pointers in the vector be printing out the same address for the bullet object. Why are a different set of memory addresses allocated?
Dec 29, 2015 at 3:38am
I think, You are printing out an iterator in your loop, not the bullet object address.
Also I cannot get your code to run. What are you trying to accomplish that is not working?
Last edited on Dec 29, 2015 at 3:45am
Dec 29, 2015 at 3:45am
Your code doesn't compile. Line 15 declares itr as an iterator for vector<Bullet> but Gun is a vector<Bullet*>. This code works:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <vector>

using std::cout;
using std::vector;
using std::endl;

typedef int Bullet;

int main()
{
    int bullet;
    vector<Bullet*> Gun;
    Gun.push_back(&bullet);
    Gun.push_back(&bullet);
    Gun.push_back(&bullet);
    Gun.push_back(&bullet);

    const Bullet * ptr = &bullet;
    int count = 0;

    cout << "Original bullet object: " << ptr << endl;
    cout << "bullet object address: " << &bullet << endl;
    cout << "  " << endl;
    cout << "  " << endl;

    for(vector<Bullet*>::const_iterator itr = Gun.begin(); itr != Gun.end(); it\
r++)
        {
            count++;
            cout << "Address " << count << " :" << *itr << endl;
        }
}

$ ./foo
Original bullet object: 0x22caac
bullet object address: 0x22caac


Address 1 :0x22caac
Address 2 :0x22caac
Address 3 :0x22caac
Address 4 :0x22caac

Dec 29, 2015 at 3:47am
@Bdanielz
Doesn't an iterator act like a pointer? Shouldn't the iterator be printing out the exact address as the bullet object?

EDIT:
Did not know you responded dhayden. Your code let me to the right outout but I'm left with a question

what is the difference between

 
cout << "Address " << count << " :" << &(*itr) << endl;


and

 
cout << "Address " << count << " :" << *itr << endl;


The first gives different memory addresses for the pointers while the second one prints the same memory address, what was expected.
Last edited on Dec 29, 2015 at 5:12am
Dec 29, 2015 at 4:38am
Doesn't an iterator act like a pointer?

I think a pointers and iterators are closely related but not the same.

Shouldn't the iterator be printing out the exact address of the bullet object?

It appears you were correct and I was wrong. I could not test due to not being able to compile your code. In other words I guessed.
Last edited on Dec 29, 2015 at 4:38am
Dec 29, 2015 at 2:08pm
what is the difference between

cout << "Address " << count << " :" << &(*itr) << endl;

This prints out the address of whatever is referenced by iter.

and cout << "Address " << count << " :" << *itr << endl;

Prints out the value of whatever is referenced by itr. For a pointer, the value is an address (typically of another object.)

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

int main()
{
    int val = 6;
    int* val_ptr = &val;

    // We don't expect the value contained by val_ptr to be 6
    std::cout << "val_ptr: " <<  val_ptr << '\n';

    // similarly we expect that the object val_ptr will have an address
    // that is different than val.
    std::cout << "&val_ptr: " << &val_ptr << '\n';

    // The same goes for pointers.
    int * ptr;
    int ** ptr_ptr = &ptr;

    std::cout << "ptr: " << ptr << '\n' ;               // some unspecified value
    std::cout << "ptr_ptr: " << ptr_ptr << '\n' ;       // the address of ptr
    std::cout << "&ptr_ptr: " << &ptr_ptr << '\n';      // the address of ptr_ptr
}
val_ptr: 000000D6C4EFFB84
&val_ptr: 000000D6C4EFFBA8
ptr: CCCCCCCCCCCCCCCC
ptr_ptr: 000000D6C4EFFBC8
&ptr_ptr: 000000D6C4EFFBE8
Last edited on Dec 29, 2015 at 2:09pm
Dec 29, 2015 at 9:00pm
So to recap

pointers, in addition to the pointed to value and address, also have a memory address of their own. Since they are objects that are allocated on the stack.

Dec 29, 2015 at 9:01pm
Since they are objects that are allocated on the stack.

Pointers do not have to be on the stack. They can also be on the heap.
Dec 29, 2015 at 10:08pm

Pointers do not have to be on the stack. They can also be on the heap.


Right in the following sense:
 
Object * ptr = new Object(); // Though I understand this is no longer recommended  


I meant in the regular raw pointer sense:
 
int * ptr;
Last edited on Dec 29, 2015 at 10:09pm
Dec 29, 2015 at 10:41pm
Both this pointer:
Object * ptr = new Object(); // Though I understand this is no longer recommended
and this pointer:
int * ptr;
are on the stack.
Last edited on Dec 29, 2015 at 10:42pm
Dec 29, 2015 at 10:52pm
 
Object * ptr


is on the stack, but
 
new Object()


is on the heap. The Object * ptr is an object allocated on the stack but points to heap memory correct?
Dec 29, 2015 at 11:01pm
That's correct.

Consider the following:
 
  int *ptr = new int * [100];


That just created 100 int pointers on the heap. In this example, ptr is on the stack, but it does not have to be.
Dec 30, 2015 at 7:59pm
That's clear Anon.
Thanks for the help everyone.
Topic archived. No new replies allowed.