Hi In my program I am creating an object of a class in a loop and storing it in a vector. Then I print the address of the object and the member variable. After that I erase them . I see that every time I see the same address assigned to my object and the member variable which is a pointer. Can any one explain this behavior.
#include <iostream>
#include <vector>
using namespace std;
typedef struct csock {
unsigned int _refCount;
mutable pthread_mutex_t _pdata_mutex;
pthread_attr_t attr;
bool _bSocketClosed; /// used for testing while closing the socket
int _iSockFD; /// Holds the native socket descriptor
void* _pfnArgs;
void* _logger; /// For debug logging
bool _isServiceRunning;
pthread_t _thread_id; /// Thread id for the listener service
int _pipes[2]; /// For stop signal communication
pthread_mutex_t* _countMutex;
unsigned long _idleTimeOut; //Idle Time Out
FILE* fp;
} __attribute__((packed)) csock_t;
class csocket
{
protected:
void* _pImpl;
public :
csocket(){
csock_t* ps = new csock_t;
this->_pImpl = reinterpret_cast<void*> (ps);
std::cout<<"\n ps is "<<ps <<" _pImpl is "<<_pImpl <<endl;
}
void* getImpl()
{
return _pImpl;
}
};
int main ()
{
vector<csocket> v;
for (int i=0; i< 5; ++i) {
v.push_back(csocket());
cout<<"\n after pushback "<<v[v.size()-1].getImpl();
cout<<"\n after pushback object is"<<&(v[v.size()-1]);
delete (csock_t*) v[0].getImpl();
v.erase(v.begin()+0);
}
cout <<"\n size of vector is "<<v.size();
return 0;
}
I am adding the output in my system
ps is 0x8368008 _pImpl is 0x8368008
after pushback 0x8368008
after pushback object is0x8368078
ps is 0x8368008 _pImpl is 0x8368008
after pushback 0x8368008
after pushback object is0x8368078
ps is 0x8368008 _pImpl is 0x8368008
after pushback 0x8368008
after pushback object is0x8368078
ps is 0x8368008 _pImpl is 0x8368008
after pushback 0x8368008
after pushback object is0x8368078
ps is 0x8368008 _pImpl is 0x8368008
after pushback 0x8368008
after pushback object is0x8368078