Thread: Using shared_ptr instead of raw pointers

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    2

    Using shared_ptr instead of raw pointers

    Hi,

    I want to used shared_ptr here instead of raw pointers. How can the following code be changed to use shared_ptr or shared_array?

    Code:
    #define  MAX_COLS 1000
    
    MyClass
    {
         private:
                    int	**m_value;
                    ...
    		...
         public:
            Myclass()
    	{
    		m_value = new int *[MAX_COLS];	
    		
    		for(int count = 0; count < MAX_COLS; count++ )
    		{
    			m_value[count] = NULL;
    		}	
    	}
    
            ~MyClass();
    	{
    		if(m_value)
    		{
    			for(int i = 0; i < MAX_COLS ; i++)
    			{
    				delete [](m_value[i]);
    			}
    			delete []m_value;
    			m_value = NULL;
    		}
    		
    	}
    
    	
    	void insert(index, int* data);
    	{
    		//do all the preliminary checks like bounds, null check
    		m_value[index] = data;
    		
    	}
    	
    
    	void processData()
    	{
    		for(int count = 0; count < MAX_COLS; count++  )
    		{
    			int *data  = m_value[count];
    			//Write the data to a file or do something with it
    		}
    		
    	}
    	
            //other members
    };

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    std::tr1::shared_ptr is probably inappropriate here since you appear to be dealing with some kind of container of objects rather than a pointer to a single object.

    boost::shared_array might be appropriate since it looks like the insert member function does not make a copy. On the other hand, is this lack of a copy intentional, or is it an oversight? If it is intentional, perhaps a std::vector<boost::shared_array<int> > would be appropriate (or you could use a std::tr1::array<boost::shared_array<int>, MAX_COLS> instead). If not, perhaps a std::vector<std::vector<int> > would be better. Of course, you should change the insert member function's signature accordingly.

    If the indices can have many gaps in the sequence, perhaps a std::map would be better than a std::vector to store the shared_array or vector.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You seem to be using the old "C" way of 2D arrays. Are you use a std::vector< std::vector<int> > won't do?
    Consider also using const T instead of #define.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    2
    Thanks for ideas. The insert without copy is intentional.

    When do you use shared_ptr<> and when shared_array<>.

    what does these signify?

    boost::shared_ptr<int> smrtptr (new int[256]);
    and
    boost::shared_array<int> smrtarray (new int[256]);

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    shared_ptr deletes with delete and shared_array deletes with delete[].
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    shared_array also implements operator [] for element access, and I would assume has less overhead than an array of shared_ptr's.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM

Tags for this Thread