Problem with pointers

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2004
Posts: 2
Reputation: Narayan is an unknown quantity at this point 
Solved Threads: 0
Narayan Narayan is offline Offline
Newbie Poster

Problem with pointers

 
0
  #1
Sep 17th, 2004
I am storing the address of an Object in more than one entry in a vector;
At the time of Deallocating the memory, I am retriving the Pointers one by one and Deleting it; The problem is that say &Obj is stored at indexes 1, 3;
delete works first time; But it fails at 3rd index since it has been deleted in the first iteration itself; Could any body help me out to solve this ;
Here is the Sample Code;

class A{//...};
typedef map<int, A*> Type_t;
Type_t MyMap;
A *p = new A();
MyMap[1] = p;
MyMap[2] = p;
MyMap[3] = p;
//...
A* dPtr;
for( Type_t::iterator iter = MyMap.begin(); iter != MyMap.end(); iter++)
{
dPtr = (*iter).second;
delete dptr; //Works fine for the first time, But fails from 2nd Iteration
}
-Narayan
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 255
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Problem with pointers

 
0
  #2
Sep 17th, 2004
Maybe something like this?
  1. dptr = iter->second;
  2. if(dptr != NULL)
  3. {
  4. delete dptr;
  5. iter->second = NULL;
  6. }
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 2
Reputation: Narayan is an unknown quantity at this point 
Solved Threads: 0
Narayan Narayan is offline Offline
Newbie Poster

Re: Problem with pointers

 
0
  #3
Sep 22nd, 2004
I tried this also, but still i am facing the same problem.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 255
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Problem with pointers

 
0
  #4
Sep 22nd, 2004
Originally Posted by Narayan
I tried this also, but still i am facing the same problem.
Originally Posted by Narayan
  1. MyMap[1] = p;
  2. MyMap[2] = p;
  3. MyMap[3] = p;
Why start at 1 when element 0 is the first element?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 18
Reputation: big146 is an unknown quantity at this point 
Solved Threads: 0
big146's Avatar
big146 big146 is offline Offline
Newbie Poster

Re: Problem with pointers

 
0
  #5
Sep 22nd, 2004
Take a look at this.Maybe It can give you some ideas.
  1. #include <vector>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <boost/shared_ptr.hpp>
  5.  
  6. struct Foo
  7. {
  8. Foo( int _x ) : x(_x) {}
  9. ~Foo() { std::cout << "Destructing a Foo with x=" << x << "\n"; }
  10. int x;
  11. /* ... */
  12. };
  13.  
  14. typedef boost::shared_ptr<Foo> FooPtr;
  15.  
  16. struct FooPtrOps
  17. {
  18. bool operator()( const FooPtr & a, const FooPtr & b )
  19. { return a->x < b->x; }
  20. void operator()( const FooPtr & a )
  21. { std::cout << " " << a->x; }
  22. };
  23.  
  24. int main()
  25. {
  26. std::vector<FooPtr> foo_vector;
  27.  
  28. foo_vector.push_back( FooPtr(new Foo(3)) );
  29. foo_vector.push_back( FooPtr(new Foo(2)) );
  30. foo_vector.push_back( FooPtr(new Foo(1)) );
  31.  
  32. std::cout << "Original foo_vector:";
  33. std::for_each( foo_vector.begin(), foo_vector.end(), FooPtrOps() );
  34. std::cout << "\n";
  35.  
  36. std::sort( foo_vector.begin(), foo_vector.end(), FooPtrOps() );
  37.  
  38. std::cout << "Sorted foo_vector:";
  39. std::for_each( foo_vector.begin(), foo_vector.end(), FooPtrOps() );
  40. std::cout << "\n";
  41. return 0;
  42. }
big146
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3
Reputation: samar is an unknown quantity at this point 
Solved Threads: 0
samar samar is offline Offline
Newbie Poster

Re: Problem with pointers

 
0
  #6
Oct 5th, 2004
could you help me in some questions
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,868
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Problem with pointers

 
0
  #7
Oct 5th, 2004
>could you help me in some questions
If you bother to ask them, and in a separate thread unless they are directly related to the original question asked in this thread or one of the replies.
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2856 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC