Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8640263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:14:15+00:00 2026-06-12T11:14:15+00:00

This class is for a midterm assignment. It’s a dynamic array that works like

  • 0

This class is for a midterm assignment. It’s a dynamic array that works like a vector. The problem is that when a new index is added to the array then the destructor is called(when the program ends) the program throws an exception and takes to a file called dgbheap.c. it happens when I delete origin only in the destructor.

DynamicArray.h

    #ifndef DYNAMICARRAY_H
#define DYNAMICARRAY_H
    #include <new>
    #include <cstdlib>
    using namespace std;

    template <class T>
    class DynamicArray
    {
        private:
            T *origin;
            T *allocator;
            int size;
            int current;


        public:
            DynamicArray();
            DynamicArray(int);
            DynamicArray(const DynamicArray&);
            ~DynamicArray();
            void add(int);
            void empty();
            void remove(int);
            int getSize();
            T &operator[](const int&);
            const T operator=(const DynamicArray&);
            void put(T&);
            void put(T);
    };
    template <class T>
    DynamicArray<T>::DynamicArray()
    {
        origin = new T[5];
        allocator = NULL;
        size = 5;
        current = 0;
    }

    template <class T>
    DynamicArray<T>::DynamicArray(int s)
    {
        size = s;

        try
        {
            origin = new T[s];
            allocator = NULL;
            current = 0;
        }
        catch(bad_alloc)
        {
            cout << "Error: Bad memery allocation\n";
            exit(EXIT_FAILURE);
        }
    }

    template <class T>
    DynamicArray<T>::DynamicArray(const DynamicArray& obj)
    {
        empty();
        for(int counter = 0; counter < obj.size - 1; counter++)
        {
            origin[counter] = obj.origin[counter];
        }
    }

    template <class T>
    DynamicArray<T>::~DynamicArray()
    {
        delete [] origin;
        delete [] allocator;
    }

    template <class T>
    void DynamicArray<T>::add(int size)
    {
        allocator = new T[size];
        for(int counter = 0; counter < this-> size - 1; counter++)
        {
            allocator[counter] = origin[counter];
        }
        delete [] origin;
        origin = new T[size];
        this->size = this->size + size;

        for(int counter = 0; counter < this->size - 1; counter++)
        {
            origin[counter] = allocator[counter];
        }
        allocator = NULL;
    }


    template <class T>
    void DynamicArray<T>::empty()
    {
        if(size >= 0)
            delete [] origin;
    }




    template <class T>
    int DynamicArray<T>::getSize()
    {
        return size;
    }

    template <class T>
    T &DynamicArray<T>::operator[](const int &index)
    {
        return origin[index];
    }


    template <class T>
    const T DynamicArray<T>::operator=(const DynamicArray &obj)
    {
        size = obj.size;
        delete [] origin;

        origin = new T[size];

        for(int counter = 0; counter < size; counter++)
            *(origin + counter) = *(obj.origin + counter);

        return *origin;
    }

    template <class T>
    void DynamicArray<T>::put(T &obj)
    {
        if(current == size - 1)
        {

            add(1);
            origin[size - 1] = obj;
            ++current; 
        }
        else
        {
            origin[current] = obj;
            ++current;
        }
    }

    template <class T>
    void DynamicArray<T>::put(T obj)
    {
        if(current > size - 1)
        {

            add(1);
            origin[size - 1] = obj;
            ++current; 
        }
        else
        {
            origin[current] = obj;
            ++current;
        }
    }


    template <class T>
    void DynamicArray<T>::remove(int location)
    {
        allocator = new T[size - 1];
        int skipper = 0;

        for(int counter = 0; counter < size - 1; counter++, skipper++)
        {
            if(counter != location)
                allocator[counter] = origin[skipper]
            else if(counter == location)
            {
                ++skipper;
                allocator[counter] = origin[skipper]
                continue;
            }
        }
    }


    #endif // !DYNAMICARRAY_H

main.cpp

    #include <iostream>
    #include <string>
    #include "DynamicArray.h"

    using namespace std;

    int main()
    {
        DynamicArray<int> a(5);
        a.put(21);
        a.put(22);
        a.put(30);
        a.put(58);
        a.put(87);
        a.put(87);

        cout << a[0] << endl;
        cout << a[1] << endl;
        cout << a[2] << endl;
        cout << a.getSize() << endl;

        system("pause");
        return 0;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:14:16+00:00Added an answer on June 12, 2026 at 11:14 am

    The problem is in:

    template void DynamicArray::add(int size);

    You’re confusing your size variables that have same name this->size and argument size

    Here’s the corrected member function:

    template <class T>
    void DynamicArray<T>::add(int size)
    {
      allocator = new T[this->size]; // was just 'size', needs to be old size
      for(int counter = 0; counter < this->size - 1; counter++)
      {
        allocator[counter] = origin[counter];
      }
      delete [] origin;
      this->size = this->size + size; // was down one line
      origin = new T[this->size]; // was 'size'. ie old size, not new size set in line above
    
      for(int counter = 0; counter < this->size - 1; counter++)
      {
        origin[counter] = allocator[counter];
      }
      allocator = NULL;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Serializing this class works fine. However, sometimes I'd like to exclude the field. Is
My class looks like this: class Post(db.Model): link = db.LinkProperty() I am getting the
Having a class like this: class Spam(object): def __init__(self, name=''): self.name = name eggs
//this class (or interface if you like) is set up as generic... public abstract
consider this class: class baseController { /* Action handler array*/ std::unordered_map<unsigned int, baseController*> actionControllers;
Is this class thread-safe? class Counter { private ConcurrentMap<String, AtomicLong> map = new ConcurrentHashMap<String,
My class is like this, public class Person : DynamicObject { public string Name
Consider this class: class foo(object): pass The default string representation looks something like this:
This works fine class Window.AppViewModel message : ko.observable() chatMessages : ko.observableArray() canSendMessage : ko.observable(false)
This class has information that I want to send with an intent to another

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.