I have a templated class that I am trying to declare as a two-dimensional templated object. When I declare my 2D object, my constructor is being called something like 15 times, and for what I think are related reasons my destructor is trying to delete memory that was not allocated. I suspect it has to do with the way I am trying to pass another instance of my class as a template parameter...
A probably related issue is that while main is making those excessive constructor calls, my destructor is called and I get a malloc error. I can't seem to find where I am allocating memory without the new operator...to my understanding, every possible instance of a My_vector should assign new
memory to array.
Why is my constructor being called repeatedly during that first line within my main function?
main.cpp
#include <iostream>
#include "My_vector.h"
int main() {
My_vector<My_vector<bool>> board; //this is where I get 15 calls to the constructor
My_vector<bool> row(4, 0);
board.push_back(row);
}
My_vector.h
#include <cstring>
#include <initializer_list>
template <typename Type>
class My_vector {
private:
Type* array;
int vector_capacity;
int vector_size;
public:
//Constructors
My_vector(int n = 0) { //keeps getting called
vector_capacity = n + 10;
array = new Type[vector_capacity];
vector_size = 0;
}
My_vector(int n, Type value) {
initialize(n + 10);
for ( ; n > 0; n--) {
push_back(value);
}
}
My_vector(std::initializer_list<Type> list) {
initialize(list.size() + 10);
memcpy(array, list.begin(), sizeof(Type) * list.size());
}
//Destructor
~My_vector() {delete [] array; array = nullptr;} //get a "pointer being freed was not allocated" error in the same line that creates the excessive constructor calls
//Accessors
int size() const {return vector_size;}
int capacity() {return vector_capacity;}
Type& at(int const) const;
//Mutators
void push_back(Type const& val);
void increase_capacity();
void initialize(int n = 10);
void erase(int);
void resize(int, Type val = Type());
void assign(std::initializer_list<Type> list);
};
template <typename Type>
void My_vector<Type>::push_back(Type const& val) {
if (size() == capacity()) {
increase_capacity();
}
array[vector_size++] = val;
}
template <typename Type>
void My_vector<Type>::increase_capacity() {
Type* temp_array = new Type[1.5 * capacity()];
memcpy(temp_array, array, size() * sizeof(Type));
delete [] array;
array = temp_array;
vector_capacity = 1.5 * capacity();
}
template <typename Type>
Type& My_vector<Type>::at(int const index) const {
return *(array + index);
}
template <typename Type>
void My_vector<Type>::initialize(int n) {
vector_capacity = n;
array = new Type[vector_capacity];
vector_size = 0;
}
Your call gets you to the first constructor:
My_vector(int n = 0) { //keeps getting called
Inside this constructor, you create 10 more My_vector objects:
vector_capacity = n + 10;
array = new Type[vector_capacity];
Again - the first entry is for the board object - My_vector of My_vector object.
The rest of the entries in the same constructor are for for each of the 10 new My_vector of bool objects that you make in the above code !
I hope this helps,
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments