Object Instantiation Calling Constructor Too Many Times

KhanKhuu

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;
}
DanD

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

android viewpagerindicator calling getPageTitle() too many times

From Dev

Calling jQuery function too many times in Rails app

From Dev

onLayoutChange() called too many times

From Dev

redirect too many times laravel

From Dev

Nginx redirecting too many times

From Dev

Executing a file too many times?

From Dev

Process evaluated too many times

From Dev

Request take too many times

From Dev

Timer is executing too many times

From Dev

Calling Object's constructor

From Dev

Object instantiation fails when using overloaded constructor

From Dev

Pull variable name through to constructor on object instantiation

From Dev

Error( Does not have companion object and too many arguments for public constructor adapterState(listState List,stateData>)

From Dev

infinite scroll triggers the event too many times

From Dev

Knockout computed is triggered too many times

From Dev

Assembly loops loop too many times

From Dev

AWS Cloudfront redirects TOO_MANY times

From Java

MigLayout - componentResized() called too many times

From Dev

Python recursive function called too many times

From Dev

Node .on method firing too many times

From Java

JSF preRenderView called too many times

From Dev

Redirected Too Many Times Error [PHP & MySQL]

From Dev

jQuery - on click with * selector trigger too many times

From Dev

Debugging java for loop that runs too many times

From Dev

Brightness keys adjust brightness too many times

From Dev

Local storage being read too many times

From Dev

passport deserialize being called too many times

From Dev

Why is Iteration looping too many times?

From Dev

Decoding too many times encoded string in PHP

Related Related

HotTag

Archive