Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #70276

Re: I'm not able to find the error

From alexo <alelvb@inwind.it>
Newsgroups comp.lang.c++
Subject Re: I'm not able to find the error
Date 2019-12-05 05:45 +0100
Organization Aioe.org NNTP Server
Message-ID <qsa20n$4ab$1@gioia.aioe.org> (permalink)
References (1 earlier) <qs90n8$1fld$1@gioia.aioe.org> <qs9jpt$7ah$1@gioia.aioe.org> <qs9r0h$162q$1@gioia.aioe.org> <qs9rph$195l$1@gioia.aioe.org> <xfWdnQ1TqbatHnXAnZ2dnUU78fXNnZ2d@brightview.co.uk>

Show all headers | View raw


Il 05/12/19 05:10, Mike Terry ha scritto:
> On 05/12/2019 02:58, alexo wrote:
>> Il 05/12/19 03:45, alexo ha scritto:
>>> Il 05/12/19 01:42, Manfred ha scritto:
>>>> On 12/4/19 8:16 PM, Manfred wrote:
>>>>>>
>>>>
>>>> Sorry, wrong line. Itis the /move/ assignment operator that should
>>>> get a non-const reference.
>>>>
>>
>>
>>> So To summarize, if I have well understood, this should be the four
>>> operators:
>>>
>>>        Vector(const Vector& r) :              // the copy constructor
>>>            sz(r.size()),
>>>            data( new int[sz] )
>>>        {
>>>            for(size_t i=0; i < sz; i++)
>>>                data[i] = r.data[i];
>>>        }
>>>
>>>        Vector(Vector&& r) :                   // the move constructor
>>>                sz{r.size()},
>>>                data{r.data}
>>>        {
>>>            r.sz = 0;
>>>            r.data = nullptr;
>>>        }
>>>
>>>        Vector& operator= (const Vector& r)    // the copy assignment
>>>        {
>>>            if(this != &r)
>>>            {
>>>                delete [] data;
>>>
>>>                sz = r.size();
>>>                data = new int[sz];
>>>
>>>                for(size_t i=0; i < sz; i++)
>>>                    data[i] =  r.data[i];
>>>                }
>>>
>>>            return *this;
>>>        }
>>>
>>>        Vector& operator= (const Vector&& r)    // the move assignment
>>>        {
>>>            if(data)
>>>              delete [] data;
>>>
>>>            data = r.data;
>>>
>>>            return *this;
>>>        }
>>>
>>> Thank you.
>>
>> No, Manfred
>> That gives the runtime error again. Both the move compy constructor and
>> the move assignment operator should take non const reference.
>>
>> If I write them this way everything works:
>>
>>       Vector(Vector&& r) :              // the move constructor
>>               sz{r.size()},
>>               data{r.data}
>>       {
>>           r.sz = 0;
>>           r.data = nullptr;
>>       }
>>
>>       Vector& operator= (Vector&& r)    // the move assignment
>>       {
>>           if(data)
>>             delete [] data;
>>
>>           data = r.data;
>>           r.data = nullptr;
>>           r.sz = 0;
>>
>>           return *this;
>>       }
> 
> Yes, both the Vector&& r  should be non-const.  That's because having 
> "stolen" the resources from r, you need to update r to reflect this, 
> otherwise the memory for the data member will be referenced in two 
> distinct objects, and so when the objects are destroyed both of them 
> will attempt to free the memory, giving the errors you see.
> 
> Also there's another error above:  in the move-assignment function you 
> steal the data resource from r, but you also need to set sz to correctly 
> reflect the size of the array you've stolen.
> 
> I.e. you need both these lines:
> 
>         data = r.data;
>         sz = r.sz;
> 
> (not just the data line).  This didn't show up as a problem in your 
> output, because it just so happens sz has the default constructor 
> setting of 5, and r.sz also happened to be 5 (but in other programs 
> could be different).
> 
> Regards,
> Mike.
> 
> 

Hi Mike,
thank you for your explanation of the error. I didn't understand why 
there were a double free call.

thank you

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

I'm not able to find the error alexo <alelvb@inwind.it> - 2019-12-04 17:54 +0100
  Re: I'm not able to find the error Volker Wippel <Volker.Wippel@v2c2RemoveNoSpam.at> - 2019-12-04 18:40 +0100
  Re: I'm not able to find the error Daniel <danielaparker@gmail.com> - 2019-12-04 10:01 -0800
    Re: I'm not able to find the error Daniel <danielaparker@gmail.com> - 2019-12-04 10:06 -0800
  Re: I'm not able to find the error Manfred <noname@add.invalid> - 2019-12-04 20:16 +0100
    Re: I'm not able to find the error Manfred <invalid@add.invalid> - 2019-12-05 01:42 +0100
      Re: I'm not able to find the error alexo <alelvb@inwind.it> - 2019-12-05 03:45 +0100
        Re: I'm not able to find the error alexo <alelvb@inwind.it> - 2019-12-05 03:58 +0100
          Re: I'm not able to find the error Mike Terry <news.dead.person.stones@darjeeling.plus.com> - 2019-12-05 04:10 +0000
            Re: I'm not able to find the error alexo <alelvb@inwind.it> - 2019-12-05 05:45 +0100
          Re: I'm not able to find the error Manfred <noname@add.invalid> - 2019-12-05 17:29 +0100
  Re: I'm not able to find the error alexo <alelvb@inwind.it> - 2019-12-04 22:52 +0100
    Re: I'm not able to find the error Daniel <danielaparker@gmail.com> - 2019-12-04 16:47 -0800
      Re: I'm not able to find the error alexo <alelvb@inwind.it> - 2019-12-05 03:37 +0100

csiph-web