>> but why? i thaught that with this line the queue queue<LeftistHeap *> q; would be made responsible for desposing the elements....
the queue owns pointers to LeftistHeaps, not the LeftistHeaps themselves. so it would release what it owns, ie. the pointer, not the pointed object. the behaviour would have been different for a queue<LeftistHeap>.
>> why the size of leftistheap is just sizeof(pointer) + sizeof(int)??I also have three pointers in the leftistheap class...
the size of an object would include the size of its non-static member variables ( + a vptr if there are virtual functions + one vbaseptr for each virtual base class + any padding; however for yourLeftistHeap none of these apply). the only non-static member variables of a LeftistHeap are
myNode *root; int emptyFlag;
>> here is what i did... if the queue isn't responsible then i am, so i did something like this:
>> .....
>> doesn't work afterwards...as i would expect...
>> how can i deallocate something that i a not in control, because when i push the pointer in the queue i lose all control on that memory....
the queue will safely keep (a copy of) anything that you push into it. and give it back to you on demand.
do something like this instead.
void LeftistHeap::initializeHeap(std::vector<int> & vec)
{
//prey to work!!!
queue<LeftistHeap *> q;
for(int i=0; i<vec.size(); ++i)
{
q.push(new LeftistHeap(vec[i]) );
}
LeftistHeap *temp;
while ( q.size()!=1)
{
temp = q.front();
q.pop();
//dld temp = merge(temp, q.front())
temp->merge( *(q.front()) );
// delete the LeftistHeap which we have just merged in to temp
delete q.front() ; // *** this was added to the original code ***
q.pop();
q.push(temp);
}
//kanoume assign to teliko heap.....
root=merge(q.front()->root, NULL);
// delete the one temporary LeftistHeap remaning
delete q.front() ; // *** this was added to the original code ***
}
note 1: i do not think that prayer is an effective antidote to problems in software.
note 2: no one is going to accuse you of writing fiendishly efficient code!