Why are all of my “this” pointers the same value?

As noted in comments, you’re seeing the this pointer for a temporary and then copying the temporary into the vector. Since the temporary is temporary your system is reusing the same memory location on each loop iteration.

However, if you print the this pointer for the vector elements, you will notice they are placed in sequential memory locations as you would expect for a vector.

#include <iostream>                                                                                                                                                                                            
#include <vector>

struct myclass {
    myclass() {
        std::cout << this << std::endl;
    }

    void print() const { 
        std::cout << this << std::endl; 
    }
};

int main() {
    std::vector<myclass> v;
    for (uint32_t i = 0; i < 10; i++)
        v.push_back(myclass());

    std::cout << "\n";

    for (auto &x : v) 
        x.print();
        
    return 0;
}

Output:

0x7ff7bfe6f5a8
0x7ff7bfe6f5a8
0x7ff7bfe6f5a8
0x7ff7bfe6f5a8
0x7ff7bfe6f5a8
0x7ff7bfe6f5a8
0x7ff7bfe6f5a8
0x7ff7bfe6f5a8
0x7ff7bfe6f5a8
0x7ff7bfe6f5a8

0x600001f4c030
0x600001f4c031
0x600001f4c032
0x600001f4c033
0x600001f4c034
0x600001f4c035
0x600001f4c036
0x600001f4c037
0x600001f4c038
0x600001f4c039

The emplace_back member function of std::vector may be used to construct objects in-place in a vector.

In the following we construct 5 temporaries which we can see reuse the same memory, and then we use push_back to place them in the vector.

Then we use emplace_back to construct five objects directly in the vector.

We can see that the memory address printed when the objects are cerated are the same as those printed later.

Note: vectors may reallocate and move their contents if size exceeds capacity. To avoid this in this case I’ve reserved a capacity of 10 for the vector v.

#include <iostream>                                                                                                                                                                                            
#include <vector>

struct myclass {
    myclass() {
        std::cout << this << std::endl;
    }

    void print() const { 
        std::cout << this << std::endl; 
    }
};

int main() {
    std::vector<myclass> v;

    v.reserve(10);

    for (uint32_t i = 0; i < 5; i++)
        v.push_back(myclass());

    for (uint32_t i = 0; i < 5; i++) 
        v.emplace_back();

    std::cout << "\n";

    for (auto &x : v) 
        x.print();
        
    return 0;
}

Output:

0x7ff7bf57b598
0x7ff7bf57b598
0x7ff7bf57b598
0x7ff7bf57b598
0x7ff7bf57b598
0x600002610035
0x600002610036
0x600002610037
0x600002610038
0x600002610039

0x600002610030
0x600002610031
0x600002610032
0x600002610033
0x600002610034
0x600002610035
0x600002610036
0x600002610037
0x600002610038
0x600002610039

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)