I know this is wrong, but I want to know why it works

So, this code sample creates an automatic variable and pushes it to a vector repeatedly. As this image shows, the same memory address is being pushed to the vector over and over, AND when I read from it, I'm reading the same memory address over and over, BUT different values are stored there each time.

1) How can there be multiple different values in the same memory location?
2) Don't the automatic variables "newClass" cease to exist after the "for" loop?

Here is the image: http://screencast.com/t/0ZNxWcokc

If you don't feel comfortable clicking on the image, here's the code sample:

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>

class myClass
{
public:
	explicit myClass(int suppliedInt):myInt{suppliedInt}{}
	~myClass(){}
	int mInt() {return myInt;}
private:
	int myInt;
};


int main()
{
	std::vector<myClass> myVector;

	for(int i = 0; i < 10; i++)
	{
		myClass newClass(i);
		std::cout << "Pushing back: " << &newClass << std::endl;
		myVector.push_back(newClass);
	}

	std::cout << std::endl << std::endl;

	for(auto x : myVector)
	{

		std::cout << &x << ": " << x.mInt() << std::endl;
	}
}


Obviously, the solution is just to allocate the variables on the heap instead of the stack...
Your vector stores actual objects, not pointers. In line 23, you copy newClass into the vector. So each time through the loop, the variable newClass occupies the same location as the last iteration, but its contents are copied into myVector.

When you get to line 28, the variable x also happens to occupy the same location in memory as newClass did in the previous loop. For each member of the vector, the contents are copied into x before the the print statement.

So, actually, everything is fine. No need to allocate on the heap. (That's taken care of for you by the vector.)
Ah, I see, so it's not pushing the actual object newClass into the vector but rather copying newClass's contents into another myClass object in the vector.

I just realized my mistake was taking the address of newClass each time and NOT &myVector[i]. Of course the automatic variable have the same address each time.
Edit: slow ...


Yes, a new object 'newClass' is created and destructed on every iteration of the lines 19-24 loop. Yes, memory is allocated for it from the stack and later deallocated. Is it more or less likely that the stack pointer is at same address at the start of each iteration?

Does that even have any significance, at which point in stack an automatic variable of each iteration exists? No.


The range-syntax for loop simply repeats the same phenomenon. These are equivalent:
1
2
3
4
5
6
7
8
9
10
// A
for ( auto x : myVector ) {
  std::cout << x;
}

// B
for ( size_t i = 0; i < myVector.size(); ++i  ) {
  auto x = myVector[i];
  std::cout << x;
}

The 'x' is an automatic variable that lives only one iteration, just like the newClass in the previous loop. Each iteration has different instance, but "miraculously" the compiler has
allocated same bits of stack each time.

1) How can there be multiple different values in the same memory location?

A) Like this:
1
2
3
4
int foo;
foo = 7;
foo = 42;
foo = 0;

Value is different at different moment in time, not simultaneously.

B) Like this:
1
2
3
4
std::vector bar = { 7, 42, 0 };
assert( &bar[0] != &bar[1] &&
        &bar[0] != &bar[2] &&
        &bar[1] != &bar[2] );

The different values exists simultaneously in the vector, where they are in different addresses (in free store, not in stack).

2) Don't the automatic variables "newClass" cease to exist after the "for" loop?

Not just after the loop. At end of each iteration:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

struct Foo
{
  int myInt;
  Foo( int x ) : myInt( x ) { std::cout << "Foo() "; }
  ~Foo() { std::cout << " ~Foo()\n"; }
};


int main()
{
  for( int i = 0; i < 10; ++i ) {
	Foo foo( i );
	std::cout << foo.myInt;
  }
  return 0;
}
Last edited on
Yeah, I think I had a brain fart there, but also a misunderstanding of how vectors stored their held objects. Thank you both for your insightful posts.
Topic archived. No new replies allowed.