Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 989489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:48:04+00:00 2026-05-16T05:48:04+00:00

In my program I am creating an object of a class in a loop

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T05:48:05+00:00Added an answer on May 16, 2026 at 5:48 am

    I think what you are looking for is storage based on a weak pointer. Your
    vector stores weak pointers to the connection objects. Your main application
    gets the shared pointers. The destructor for your connection object frees the
    resource. Periodically you can pack the vector by erasing all weak pointers
    with a use_count of zero.

    class Connect;
    
    typedef boost::shared_ptr<Connect> ConnectPtr;
    typedef boost::weak_ptr<Connect> ConnectWeakPtr;
    typedef std::vector<ConnectWeakPtr> ConnectVector;
    
    ConnectVector vector;
    
    ConnectPtr ptr( new Connect ( ... ));
    
    vector.push_back(ptr);
    
    void pack() {
        ConnectVector tmp;
        BOOST_FOREACH(ConnectWeakPtr const & p, mMap){
            if (p.use_count() > 0){
                tmp.insert(p);
            }
            mMap.swap(tmp);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im building a relatively large object-oriented program. I have a class called AerodynamicCalculator that
Bassicly im creating a program that reads information from an xml file into a
I'm creating a program that authenticates from a before it runs. I also want
I'm creating a program using C# (in ASP.NET environment) that makes an XML file.
I'm creating an object that will represent some system info that can be changed
I use the following program for creating JTable using java class. If I get
I am creating a program that encrypts/hashs messages I have a list that contains
I am creating a meta data class for a POCO object. I am adding
In my program there is a BackgroundWorker class that preloads the images to BitmapImage
I'm creating a program that requires some options values to be set along with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.