Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > min_element algorithm and function object copying

Reply
Thread Tools

min_element algorithm and function object copying

 
 
Michael Doubez
Guest
Posts: n/a
 
      09-13-2010
On 11 sep, 03:24, suresh <suresh.amritap...@gmail.com> wrote:
> Hi,
> In my code, the operator() method of a function object *is designed to
> fill a set which is a private data member of the function object. This
> function object is used in min_element algorithm.
> Since the min_element algorithm uses a copy of the function object (as
> per my understanding - hoping it to be true), I am unable to access
> the set data member of the function object. *A minimal code is show
> below:
>
> class FO{
> public:
> bool operator()(int a, int b);
> private:
> set<int> s;};
>
> bool FO:perator()(int a,int b){
> s.insert(a);
> return (a<b);}
>
> vector<int> v;
> //vector populated
> FO fo;
> min_element(v.begin(),v.end(),fo);
>
> Only after some time I realised that min_element takes only a copy of
> the variable fo. [I found that the size of the set is always zero
> after the call to *min_element]. As a solution to this problem, I
> tried to define the variable s as a pointer to set (set<int> *s) and
> then modified the code accordingly but when the code is running, half
> way thru, the code aborts saying *corrupted double-linked list:
> 0x000000000102beb0 ***.
>
> What is the solution to this issue?


My crystal ball suggests that you delete 's' member in the destructor
causing it to be corrupted in copies of fo.

Try something like:

class FO{
public:
FO( set<int>& is):s(&is){}

bool operator()(int a, int b);

private:
set<int>* s;
};

bool FO:perator()(int a,int b){
s->insert(a);
return (a<b);
}

set<int> fo;
min_element(v.begin(),v.end(),FO(fo));

--
Michael
 
Reply With Quote
 
 
 
 
Michael Doubez
Guest
Posts: n/a
 
      09-13-2010
On 13 sep, 15:41, Michael Doubez <michael.dou...@free.fr> wrote:
> On 11 sep, 03:24, suresh <suresh.amritap...@gmail.com> wrote:
>
>
>
> > Hi,
> > In my code, the operator() method of a function object *is designed to
> > fill a set which is a private data member of the function object. This
> > function object is used in min_element algorithm.
> > Since the min_element algorithm uses a copy of the function object (as
> > per my understanding - hoping it to be true), I am unable to access
> > the set data member of the function object. *A minimal code is show
> > below:

>
> > class FO{
> > public:
> > bool operator()(int a, int b);
> > private:
> > set<int> s;};

>
> > bool FO:perator()(int a,int b){
> > s.insert(a);
> > return (a<b);}

>
> > vector<int> v;
> > //vector populated
> > FO fo;
> > min_element(v.begin(),v.end(),fo);

>
> > Only after some time I realised that min_element takes only a copy of
> > the variable fo. [I found that the size of the set is always zero
> > after the call to *min_element]. As a solution to this problem, I
> > tried to define the variable s as a pointer to set (set<int> *s) and
> > then modified the code accordingly but when the code is running, half
> > way thru, the code aborts saying *corrupted double-linked list:
> > 0x000000000102beb0 ***.

>
> > What is the solution to this issue?

>
> My crystal ball suggests that you delete 's' member in the destructor
> causing it to be corrupted in copies of fo.


Oups. It didn't extend to showing me you had solved the problem.
sorry for the noise.

--
Michael


 
Reply With Quote
 
 
 
 
suresh
Guest
Posts: n/a
 
      09-13-2010
On Sep 11, 11:01*am, James Kanze <james.ka...@gmail.com> wrote:
> On Sep 11, 4:05 am, suresh <suresh.amritap...@gmail.com> wrote:
>
> > On Sep 10, 7:59 pm, suresh <suresh.amritap...@gmail.com> wrote:

>
> * *[...]
>
> > I can give one more input so that the experts here may be able to give
> > me some suggestions.
> > My defintion of the pointer is like this:
> > set<double> * distances; //inside the class as private element.

>
> How and when does it get set? *How is it copied?


It is set in the constructor as shown below.
>
> > //in the constructor
> > distances = new set<double>;

>
> In which constructor: all of them (including the copy
> constructor), or?


I have not written a copy constructor. So the compilers default copy
constructor is used.
>
> > //in the destructor
> > delete distances;

>
> Under what conditions?

What I meant is, when the code was aborted during execution, I was
trying to see what triggers abort and I found that removing delete
distances removes abort. But then it would result in non freeing of
memory and I didnt want to use that "crude" solution to my problem.
However I realised that it happens because the pointer is deleted
twice. Once when the copied object is destructed at the end of
execution of min_element algorithm and once when my own destructor is
called.

>
> > what i found is, if I dont delete the distances variable in
> > the destructor, I am fine....
> > any suggestions?

>
> A quick fix (and possibly the best solution in this case,
> although we don't have enough information to be sure) would be
> to use boost::shared_ptr<std::set<double> >, rather than a raw
> pointer. *Still, I would suggest you start by learning about the
> rule of four: try reading Scott Meyers' _Effective_ _C++_, for
> starters.


I think boost::shared_ptr is the solution to my problem. Will look at
Scott Meyers' _Effective_ _C++_ too.
thanks
suresh


 
Reply With Quote
 
suresh
Guest
Posts: n/a
 
      09-13-2010
On Sep 13, 6:41*am, Michael Doubez <michael.dou...@free.fr> wrote:
> On 11 sep, 03:24, suresh <suresh.amritap...@gmail.com> wrote:
>
>
>
>
>
> > Hi,
> > In my code, the operator() method of a function object *is designed to
> > fill a set which is a private data member of the function object. This
> > function object is used in min_element algorithm.
> > Since the min_element algorithm uses a copy of the function object (as
> > per my understanding - hoping it to be true), I am unable to access
> > the set data member of the function object. *A minimal code is show
> > below:

>
> > class FO{
> > public:
> > bool operator()(int a, int b);
> > private:
> > set<int> s;};

>
> > bool FO:perator()(int a,int b){
> > s.insert(a);
> > return (a<b);}

>
> > vector<int> v;
> > //vector populated
> > FO fo;
> > min_element(v.begin(),v.end(),fo);

>
> > Only after some time I realised that min_element takes only a copy of
> > the variable fo. [I found that the size of the set is always zero
> > after the call to *min_element]. As a solution to this problem, I
> > tried to define the variable s as a pointer to set (set<int> *s) and
> > then modified the code accordingly but when the code is running, half
> > way thru, the code aborts saying *corrupted double-linked list:
> > 0x000000000102beb0 ***.

>
> > What is the solution to this issue?

>
> My crystal ball suggests that you delete 's' member in the destructor
> causing it to be corrupted in copies of fo.
>
> Try something like:
>
> class FO{
> public:
> FO( set<int>& is):s(&is){}
>
> bool operator()(int a, int b);
>
> private:
> set<int>* s;
>
> };
>
> bool FO:perator()(int a,int b){
> s->insert(a);
> return (a<b);
>
> }
>
> set<int> fo;
> min_element(v.begin(),v.end(),FO(fo));

thanks Micheal, I had tried this solution and it worked but I was
looking for a different solution.
thanks again
suresh
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
slicing - copying or assigning derived object to base object subramanian100in@yahoo.com, India C++ 1 04-22-2008 02:55 PM
Filtered Back Projection Algorithm (FBP Algorithm) Bapaiah Katepalli VHDL 1 06-23-2006 04:50 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
Copying the style object from one object to another Sparhawk Javascript 5 08-29-2004 09:50 AM
Key generation algorithm and Cipher algorithm Ahmed Moustafa Java 0 11-15-2003 06:35 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57