0

I'm constructed a simple container called buffer. When overloading the = operator i'm getting the following error

Exception thrown at 0x774AEBA5 (ntdll.dll) in init_list_test.exe: 0xC000000D: An invalid parameter was passed to a service or function. Unhandled exception at 0x774AEBA5 (ntdll.dll) in init_list_test.exe: 0xC000000D: An invalid parameter was passed to a service or function.

Not really sure what this means.

Here is the code to reproduce:

#include "buffer.h"
int main()
{
    buffer buf(10);
    buffer test(10);
    buf = test;


    return 0;
}

in buffer.cpp

#include "buffer.h"
#include <iostream>
size_t buffer::get_size() const
{
    return length;
}
buffer::buffer(size_t length) : start(new int[length]), length(length)
{
    std::cout << length << +" size" << std::endl;
}

buffer::buffer(const buffer& rhs) : start(new int[length]), length(rhs.get_size())
{
    std::copy(rhs.begin(), rhs.end(), start);
}

buffer& buffer::operator=(const buffer& rhs)
{
    buffer temp_buff(rhs);
    return temp_buff;

}

int* buffer::begin()
{
    return start;
}

int* buffer::end()
{
    return start + length;
}

const int* buffer::begin() const
{
    return start;
}

const int* buffer::end() const
{
    return start + length;
}

in buffer.h

#pragma once
class buffer {
    int* start;
    size_t length;
public:
    size_t get_size() const;
    explicit buffer(size_t size);
    buffer(const buffer& rhs);
    buffer& operator=(const buffer& rhs); 
    int* begin(); 
    int* end();
    const int* begin() const;
    const int* end() const;
};

I saw on cppref that the offical way was to do something like this code below:

// assume the object holds reusable storage, such as a heap-allocated buffer mArray
T& operator=(const T& other) // copy assignment
{
    if (this != &other) { // self-assignment check expected
        if (other.size != size) {         // storage cannot be reused
            delete[] mArray;              // destroy storage in this
            size = 0;
            mArray = nullptr;             // preserve invariants in case next line throws
            mArray = new int[other.size]; // create storage in this
            size = other.size;
        } 
        std::copy(other.mArray, other.mArray + other.size, mArray);
    }
    return *this;
}

However i wanted to use the copy ctor that i already designed since the code is very similar.

kino92
  • 39
  • 7
  • Your assignment operator doesn't modify `this`. It doesn't actually do any assignment. That should have been a red flag. – StoryTeller - Unslander Monica Nov 13 '19 at 18:58
  • Ok maybe i'm a bit slow here. But why does it need to modify this? why can't i return temp_buff instead of this? – kino92 Nov 13 '19 at 19:01
  • @0x5453 oh so the problem is that i can use rhs.size() as a length to my buffer? I see. Do you have any link where i can read about this? – kino92 Nov 13 '19 at 19:02
  • Well, if I was to assign 5 to an int, I'd expect that int to hold the value of 5. If its value didn't change by the assignment, how can it hold a new value? – StoryTeller - Unslander Monica Nov 13 '19 at 19:02
  • @kino92 Sorry, ignore that comment. I didn't see that you are allocating those arrays on the heap, which is fine. (Though you should still consider switching to [`std::vector`](https://en.cppreference.com/w/cpp/container/vector) for simplicity.) – 0x5453 Nov 13 '19 at 19:03
  • @StoryTeller-UnslanderMonica So does = overloading only work on this? I assumed that when i say buf = test i'm actually saying take test as a parameter to the overloading function and give buf whatever the return value is. – kino92 Nov 13 '19 at 19:06
  • No. When you say `buf = test` then `this` is bound to `buf`, while `test` is bound to `other`. The assignment operator must then modify `this` for any changes to occur. – StoryTeller - Unslander Monica Nov 13 '19 at 19:07
  • @StoryTeller-UnslanderMonica well that explains a few things :) is this "bound to buf" called something? Just wanted to read about it so i can learn. cppreference have this in there examples but i can't find anything that states this is the case. – kino92 Nov 13 '19 at 19:13
  • @kino92 - The official term is "reference binding". References are said to bind to the object they alias. Even though `this` is not a reference officially, the term also holds for it. – StoryTeller - Unslander Monica Nov 13 '19 at 19:15
  • The compiler should have told you that `return temp_buff;` is returning a reference to a local variable. – Raymond Chen Nov 13 '19 at 19:19
  • 2
    *However i wanted to use the copy ctor that i already designed since the code is very similar.* This sounds like you're looking for the [Copy and Swap Idiom](https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom). – user4581301 Nov 13 '19 at 19:34
  • maybe this helps, an alternative way to write `a=b` is `a.operator=(b);` then it isnt much different from other member functions, there is no magic behind the scences that does assign the return value to `a`. In most cases the return value of `operator=` is simply ignored – 463035818_is_not_an_ai Nov 13 '19 at 19:41
  • Maybe [this](https://stackoverflow.com/q/4421706/10957435) will answer your question. –  Nov 13 '19 at 19:58

0 Answers0