| | |
Problem with pointers
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2004
Posts: 2
Reputation:
Solved Threads: 0
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
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
Maybe something like this?
C++ Syntax (Toggle Plain Text)
dptr = iter->second; if(dptr != NULL) { delete dptr; iter->second = NULL; }
"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
•
•
•
•
Originally Posted by Narayan
I tried this also, but still i am facing the same problem.
•
•
•
•
Originally Posted by Narayan
C++ Syntax (Toggle Plain Text)
MyMap[1] = p; MyMap[2] = p; MyMap[3] = p;
"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
Take a look at this.Maybe It can give you some ideas.
C++ Syntax (Toggle Plain Text)
#include <vector> #include <iostream> #include <algorithm> #include <boost/shared_ptr.hpp> struct Foo { Foo( int _x ) : x(_x) {} ~Foo() { std::cout << "Destructing a Foo with x=" << x << "\n"; } int x; /* ... */ }; typedef boost::shared_ptr<Foo> FooPtr; struct FooPtrOps { bool operator()( const FooPtr & a, const FooPtr & b ) { return a->x < b->x; } void operator()( const FooPtr & a ) { std::cout << " " << a->x; } }; int main() { std::vector<FooPtr> foo_vector; foo_vector.push_back( FooPtr(new Foo(3)) ); foo_vector.push_back( FooPtr(new Foo(2)) ); foo_vector.push_back( FooPtr(new Foo(1)) ); std::cout << "Original foo_vector:"; std::for_each( foo_vector.begin(), foo_vector.end(), FooPtrOps() ); std::cout << "\n"; std::sort( foo_vector.begin(), foo_vector.end(), FooPtrOps() ); std::cout << "Sorted foo_vector:"; std::for_each( foo_vector.begin(), foo_vector.end(), FooPtrOps() ); std::cout << "\n"; return 0; }
big146
![]() |
Similar Threads
- Problem with pointers (C++)
- The Problem with Pointers (C++)
- swapping with pointers ... (C)
- Converting Struct to class lost with pointers (C++)
- Problem About Pointers (C++)
Other Threads in the C++ Forum
- Previous Thread: State abbreviations
- Next Thread: Help Using For Loop To Display Ordered Numbers
Views: 2856 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






