944,080 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3276
  • C++ RSS
Sep 17th, 2004
0

Problem with pointers

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Narayan is offline Offline
2 posts
since Sep 2004
Sep 17th, 2004
0

Re: Problem with pointers

Maybe something like this?
C++ Syntax (Toggle Plain Text)
  1. dptr = iter->second;
  2. if(dptr != NULL)
  3. {
  4. delete dptr;
  5. iter->second = NULL;
  6. }
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 22nd, 2004
0

Re: Problem with pointers

I tried this also, but still i am facing the same problem.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Narayan is offline Offline
2 posts
since Sep 2004
Sep 22nd, 2004
0

Re: Problem with pointers

Quote originally posted by Narayan ...
I tried this also, but still i am facing the same problem.
Quote originally posted by Narayan ...
C++ Syntax (Toggle Plain Text)
  1. MyMap[1] = p;
  2. MyMap[2] = p;
  3. MyMap[3] = p;
Why start at 1 when element 0 is the first element?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 22nd, 2004
0

Re: Problem with pointers

Take a look at this.Maybe It can give you some ideas.
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 14
Solved Threads: 0
Newbie Poster
big146 is offline Offline
18 posts
since Jul 2004
Oct 5th, 2004
0

Re: Problem with pointers

could you help me in some questions
Reputation Points: 10
Solved Threads: 0
Newbie Poster
samar is offline Offline
3 posts
since Oct 2004
Oct 5th, 2004
0

Re: Problem with pointers

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: State abbreviations
Next Thread in C++ Forum Timeline: Help Using For Loop To Display Ordered Numbers





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC