Thread: Looking for help with pointer idea.

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    673

    Question Looking for help with pointer idea.

    Well, I don't really know how to explain this other than show you so that is what I'll do.

    Code:
    int* a = new int(1000);
    int* b = a;
    
    delete a; a = 0;
    I know that doesn't make any sense, but what I am wanting to do is that when a is freed b is automatically set to NULL also.

    I am also wanting to maintain the pointer syntax for the most part so I don't end up with another class that is unreadable. I also need to have it work on multiple levels. So basically
    Code:
    int* a = new int(1000);
    int* b = a;
    int* c = b;
    //etc etc
    I want c and b to be set to null when a is freed.

    I have been thinking about this for several hours, but I have yet to come up with anything that lets me maintain the regular syntax.

    - I don't care about allowing directly newing or deleteing the data as a function for this is fine
    - I want to maintain the * and -> to access the data for getting/setting

    I am hoping someone here can give me some insight/ideas on how to achieve this. I am very grateful for any assistance. Thank you.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Raigne
    I know that doesn't make any sense, but what I am wanting to do is that when a is freed b is automatically set to NULL also.
    The solution is to use a std::tr1::shared_ptr instead of a raw pointer. (Or in the absence of a TR1 implementation, boost::shared_ptr.)

    EDIT:
    Actually, if only a has ownership while b (and c) does not, then a could be a std::tr1::shared_ptr while b (and c) would be a std::tr1::weak_ptr (or boost::weak_ptr, as the case may be).
    Last edited by laserlight; 01-07-2009 at 01:19 AM.
    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
    Beginner leiming's Avatar
    Join Date
    Jan 2008
    Location
    Fujian, China
    Posts
    25
    About the second question, using:

    int* &b = a;
    int* &c = b;

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by leiming
    About the second question, using:

    int* &b = a;
    int* &c = b;
    That will only work if b (and c) will never be bound to another pointer.
    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

  5. #5
    Beginner leiming's Avatar
    Join Date
    Jan 2008
    Location
    Fujian, China
    Posts
    25
    Quote Originally Posted by laserlight View Post
    That will only work if b (and c) will never be bound to another pointer.
    I just try writting some codes to reach it:
    Code:
    #include <iostream>
    using namespace std;
    
    class myintpointer{
    	public:
    		myintpointer(){
    			p2 = 0;
    			p = 0;
    		}
    		myintpointer(int* p){
    			this->p = p;
    			p2 = this;
    		}
    		operator int* (){
    			if (p2==this)
    				return p;
    			else
    				return *p2;
    		}
    		int* operator = (int *p){
    			this->p = p;
    			p2 = this;
    		}
    		myintpointer* operator = (myintpointer *p){
    			p2 = p;
    		}
    	private:
    		int *p;
    		myintpointer *p2;
    };
    
    int main(){
    	myintpointer a = new int;
    	myintpointer b, c;
    	*a = 1000;
    	c = b;
    	b = a;
    	cout << *a << " " << *b << endl;
    	delete a;
    	c = new int(2000);
    	b = c;
    	cout << *c << " " << *b << endl;
    	return 0;
    }
    I hope it helps.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by leiming
    I just try writting some codes to reach it:
    What do you mean? Your original suggestion seems pretty clear, just that it has a limitation that might not be relevant. (And it shares with Raigne's original code the disadvantage of having to do manual memory management.)
    Last edited by laserlight; 01-07-2009 at 03:01 AM.
    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

  7. #7
    Beginner leiming's Avatar
    Join Date
    Jan 2008
    Location
    Fujian, China
    Posts
    25
    Quote Originally Posted by laserlight View Post
    What do you mean? Your original suggestion seems pretty clear, just that it has a limitation that might not be relevant. (And it shares with Raigne's original code the disadvantage of having to do manual memory management.)
    I mean if that shall work when b and c are bound to another pointer, this is the only way I can find.

    It has no other meanings. And because English is not my native language; if there are something I misunderstand, I say sorry for that.

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. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  3. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 01:09 PM
  4. Pointer validity check
    By Carlos in forum Windows Programming
    Replies: 6
    Last Post: 12-11-2003, 03:40 AM
  5. Replies: 2
    Last Post: 11-28-2003, 11:50 AM