Error when running +

EDIT: The lowlife pos OP deleted his posts from this thread.

The second parameter to operator<< should be const.
Last edited on
dutch thanks it works but now this is coming up and it only comes up when i include the + operator in my main
cout<<"900+8000 = "<<(one+two)<<endl;


dbiguint operator +(const dbiguint &lhs, const dbiguint &rhs)
{
dbiguint one(lhs);
one += rhs;
return one;
}
a.out(39742,0x10d0c9dc0) malloc: *** error for object 0x7ff022c01700: pointer being freed was not allocated
a.out(39742,0x10d0c9dc0) malloc: *** set a breakpoint in malloc_error_break to debug
what does this error mean?
Im not sure what this means is their something wrong with my main? @dutch
@dutch
Last edited on
You're missing a copy constructor. Without it, when you do dbiguint one(lhs); you get the default copy ctor which just copies the data_ pointer, without creating a separate array. In that case, the data_ array gets deleted twice.

1
2
3
4
5
6
7
8
// copy constructor
dbiguint::dbiguint(const dbiguint& b)
{
  capacity_ = b.capacity_;
  data_ = new unsigned short[capacity_];
  for (size_t i = 0; i < capacity_; ++i)
    data_[i] = b[i];
}

Also:

Your operator+= (and operator-=) should have return values of dbignuint& and need to return *this at the end.

Then operator+ can be written like this (note that lhs is passed by value, i.e., as a copy):

1
2
3
4
dbiguint operator+(dbiguint lhs, const dbiguint &rhs)
{
    return lhs += rhs;
}

In operator[], you don't need the pos>=0 test since pos (of type size_t) is unsigned and so can't be negative.

In reserve, the initial test should be <=, not just <

1
2
  if (newcapacity_ <= capacity_)
    return;

Last edited on
operators have fixed designs, eg you can't make operator + take 5 arguments.
+= needs to return something, because its an assignment.
x+=3; //this makes sense, its effectively x = x+3;
+=5; //what should this do? its like saying =5; you need something on both sides of assignments.

you can abuse operators a lot in c++ (its one of the things weak languages that prevent overloads claim is a negative) but there are some things you can do with normal functions that you can't force an operator to do.

you can probably force the code to do nothing with the returned value, though.
operator += (type thing) {cout << thing; return this;}
class.operator+=(athing); //dunno if you can make something like this compile and run or not. But really, with that kind of syntax (its close if not right? ) you may as well just make a non operator method to do whatever it was.
Last edited on
but its a void += would I still return *this?

Obviously you can't return something if it's void.
But the += operator is not usually void.
Are you forced (by a moronic teacher) to declare it that way?
If not, then do it properly and return *this (as a reference, i.e., dbignuint&).
needs to return something, because its an assignment.

Really? Assignments do not need to be chainable, but it is convenient if they are.

The builtin operators do not return void.
https://en.cppreference.com/w/cpp/language/operator_assignment
https://en.cppreference.com/w/cpp/language/operator_arithmetic
True, I suppose its not enforced! But that is going to yield weird code to me.
Last edited on
Topic archived. No new replies allowed.