0

My code compiles successfully, but when I try to run it, I keep getting this error: * Error in `./a.out': corrupted double-linked list: 0x00000000021c1280 * Aborted

This is my VectorDouble.cpp file

#include<bits/stdc++.h> 
#include <cstring>
#include "VectorDouble.h"
#include <iostream>

using namespace std;

VectorDouble::VectorDouble() {
    cout<<"constructor called"<<endl;
    max_count = 50;
    arr = new double[this->max_count];
    count = 0;
}

VectorDouble::VectorDouble(int max_count_arg) {
    max_count = max_count_arg;
    arr = new double[max_count_arg];
    count = 0;
}

VectorDouble::VectorDouble(const VectorDouble& copy) {
    max_count = copy.max_count;
    arr = new double[this->max_count];
    count = copy.count;
}

VectorDouble::~VectorDouble() {
    delete []arr;
}

VectorDouble VectorDouble::operator =(VectorDouble& copy) {
    VectorDouble temp(copy.max_count);

    for(int i =0; i<=this->count;i++){
        temp.arr[i]=copy.arr[i];
    }
    return temp;
}

bool VectorDouble::operator ==(VectorDouble b) const {
    bool isEqual = true;
    if(this->count == b.count){
        for(int i = 0; i<=this->count; i++){
            if(this->arr[i] == b.arr[i]){
                isEqual= true;
            }
            else{
                return false;
            }
        }
    }
    return isEqual;
}

void VectorDouble::push_back(double num) {

    if(this->count+1>this->max_count){
        this->max_count *= 2;
        VectorDouble temp(2*(this->max_count));
        for(int i = 0; i<this->max_count; i++){
            temp.arr[i]=this->arr[i+1];
        }
        temp.arr[count+1] = num;
    }
    else{
        this->arr[count+1]=num;
        this->count++;
    }

}

int VectorDouble::capacity() {
    return this->max_count;
}

int VectorDouble::size() {
    return this->count;

}

void VectorDouble::resize(unsigned int size, double defaultVal) {
    if(size>(this->count)){
        for(int i = this->count; i<size; i++){
            this->arr[i] = defaultVal;
        }
        this->count=size;
    }
    else{
        for(int i = size; i < this->count; i++){
            this->arr[i] ='\0';
        }
        this->count=size;
    }
}

void VectorDouble::reserve(unsigned int size) {
    if(size>(this->max_count)){
        this->max_count = size;
    }
}

double VectorDouble::value_at(unsigned int i) {
    if(i>(this->count)){
        throw std::logic_error("out of bounds");
    }
    return this->arr[i];
}

void VectorDouble::change_value_at(double newValue, unsigned int i) {
    if(i>(this->count)){
        throw std::logic_error("out of bounds");
    }
    this->arr[i]=newValue;
}

ostream& operator<<(ostream& os, const VectorDouble &vd)
{
  for(int i = 0; i < vd.count; i++){
    os << vd.arr[i] << " ";
  }
  return os;
}

This is my VectorDouble.h file

#ifndef DYNAMICARRAY_H
#define DYNAMICARRAY_H

#include <iostream>

using namespace std;

class VectorDouble {
    public:
      int max_count;
      int count;
      double* arr;
    public:
      VectorDouble();
      VectorDouble(int max_count_arg);
      VectorDouble(const VectorDouble& copy);
      ~VectorDouble();
      VectorDouble operator =(VectorDouble& copy);
      bool operator ==(VectorDouble b) const;
      void push_back(double num);
      int capacity();
      int size();
      void reserve(unsigned int size);
      void resize(unsigned size, double defaultVal = 0.0);
      double value_at(unsigned int i);
      void change_value_at(double newValue, unsigned int i);
      friend ostream& operator<<(ostream& os, const VectorDouble &vd);
      // DO NOT CHANGE THE FOLLOWING LINE OF CODE. It is for the testing framework
      // DO NOT IMPLEMENT THE FOLLOWING FUNCTION. It is implemented by the testing framework
      friend int reserved_driver_main();
};

#endif

This is my main.cpp file

#include <iostream>
#include "VectorDouble.h"
using namespace std;


int user_main() {
     // test 1, verify that default constructor initializes max_count
  VectorDouble v;
  if (v.max_count == 50)
  {
    std::cout << "1.1. default constructor: max_count = 50; test passed" << std::endl;
  }
  else
  {
    std::cout << "1.1. default constructor: max_count != 50; test failed" << std::endl;
  }
    return 0;
}
  • Possible duplicate of [What does 'corrupted double-linked list' mean](https://stackoverflow.com/questions/14897157/what-does-corrupted-double-linked-list-mean) – tbejos Jul 27 '19 at 05:43
  • I can see lots and lots of bugs in your code, but nothing that would cause `user_main` to have a problem. – john Jul 27 '19 at 05:53
  • I've taken you code and tried it [here](https://www.onlinegdb.com/Hy2YDPYfB). All I've done is change `user_main` to `main`. As you can see there is no error with the code you have posted. – john Jul 27 '19 at 05:58
  • However there are a lot of bugs in your code, the copy constructor, the assignment operator, the resize method are all bugged. – john Jul 27 '19 at 05:58
  • @john what bugs do you see in my code? – dwightkschrute Jul 27 '19 at 05:58
  • You can add the reserve method as well. – john Jul 27 '19 at 05:59
  • And push_back too. – john Jul 27 '19 at 05:59
  • The resize bug looks like the most serious, that could definitely crash your program. The resize method should allocate new memory if the new size is bigger than the capacity, your version doesn't do this. – john Jul 27 '19 at 06:01
  • Possible duplicate of [What is a glibc free/malloc/realloc invalid next size/invalid pointer error and how to fix it?](https://stackoverflow.com/questions/23680334/what-is-a-glibc-free-malloc-realloc-invalid-next-size-invalid-pointer-error-and) – Masoud Keshavarz Jul 27 '19 at 06:03
  • What you should do is start testing your code, piece by piece, to work through the bugs. I suggest you start with the copy constructor. You'll quickly see that it doesn't copy any values. – john Jul 27 '19 at 06:04
  • Then you could try the assignment operator which has a similar problem. – john Jul 27 '19 at 06:08
  • Then, slightly more advanced, you could try the push_back method. If you create a new vector and then push_back 51 values, you'll find that the first 50 work, but the 51st does not get added to the vector. – john Jul 27 '19 at 06:09
  • Welcome to Stack Overflow! You should never `#include `. It is not proper C++. It ruins portability and fosters terrible habits. See [Why should I not `#include `](https://stackoverflow.com/q/31816095). – L. F. Jul 27 '19 at 09:36
  • Also, please avoid `using namespace std;`. It is considered bad practice. See [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721) – L. F. Jul 27 '19 at 09:36

0 Answers0