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 8772535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:03:31+00:00 2026-06-13T18:03:31+00:00

I’m trying to create a dynamic array in my List class that will start

  • 0

I’m trying to create a dynamic array in my List class that will start off with a size of 2 and when you insert values with the Insert method, it will check to see if there is enough space if not it will resize the array with a size + 2… The problem is it is crashing VS is complaining about corruption of the heap. Also I think my copy constructor isn’t being called because the cout’s arent displaying:

list.h File:

class List
{
public:

    //  DEFAULT Constructor
    List();
    // Deconstructor to free memory allocated 
    ~List();// Prevent memory leaks

    // COPY Constructor for pointers
    List(const List& value);// copy constructor

    //Modification methods
    void Insert(const int);

    // User ACCESS methods
    void display(void) const;

private:
    int size;// MAX size of the array          
    int count;// current number of elements in the dynamic array

protected:
    int *intptr;// Our int pointer
};

list.cpp implementation file:

#include "list.h" // Include our Class defintion
#include <iostream>

using namespace std;

// CONSTRUCTOR
List::List() {
    size = 2; // initial size of array
    count = 0;
    intptr = new int[size]; // Start off 2 integers sized array
}
// DECONSTRUCTOR
List::~List() {
    delete[] intptr; // free allocated memory
}

// Copy constructor

List::List(const List& value) {
    size = value.size;
    cout << "Copy con size : " << size << endl;
    count = value.count;

    cout << "Compy count : " << count << endl;
    if (count < size) {
        intptr = new int[size]; // Allocate new data
    } else {
        intptr = new int[size + 2]; // Allocate new data
    }

    for (int index = 0; index < count; index++) {
        intptr[index] = value.intptr[index];
    }

    size = size + 2;
    delete[] intptr;
    intptr = value.intptr;
}

void List::Insert(const int value) {
    // do we have room?
    if (count < size) {
        intptr[count] = value;
    } else { // if not we need to add more elements to array
        intptr[count] = value; // DEEP copy invoked with copy constructor
    }

    cout << "SIZE: " << size << endl;
    cout << "COUNT" << count << endl;
    count++; // Increase items added in array
}

void List::display() const {
    for (int i = 0; i < count; i++)
        cout << intptr[i] << endl;
}

main.cpp tester

#include <iostream>
#include "list.h"

int main()
{
    List mylist;

    mylist.Insert(5);
    mylist.Insert(6);
    mylist.Insert(2);
    mylist.Insert(8);
    mylist.Insert(4);
    mylist.Insert(5);
    mylist.Insert(9);
    mylist.Insert(8);
    mylist.Insert(5);
    mylist.Insert(9);
    mylist.Insert(8);
    mylist.Insert(5);
    mylist.Insert(9);
    mylist.Insert(8);
    mylist.Insert(5);
    mylist.Insert(9);

    mylist.display();

    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-13T18:03:32+00:00Added an answer on June 13, 2026 at 6:03 pm

    You are not managing the array correctly, especially in your Insert() method. Try this instead:

    #include "list.h" // Include our Class defintion 
    #include <iostream> 
    
    // CONSTRUCTOR 
    List::List()  
    { 
        intptr = new int[2];
        size = 2;
        count = 0; 
    
        std::cout << "Initial size : " << size << " count : " << count << std::endl; 
    } 
    
    // DECONSTRUCTOR 
    List::~List() 
    { 
        delete [] intptr; // free allocated memory 
    } 
    
    // Copy constructor 
    List::List(const List& value) 
    { 
        intptr = new int[value.size]; // Allocate new data 
        size = value.size; 
        count = value.count; 
    
        for(int index = 0; index < count; ++index) 
            intptr[index] = value.intptr[index]; 
    
        std::cout << "Copy size : " << size << " count : " << count << std::endl; 
    } 
    
    void List::Insert(const int value) 
    { 
        if (count == size)
        { 
            int *newintptr = new int[size+2];
    
            for(int index = 0; index < size; ++index) 
                newintptr[index] = intptr[index]; 
    
            delete[] intptr;
            intptr = newintptr;
            size += 2;
        }
    
        intptr[count] = value; 
        ++count;
    
        std::cout << "New size : " << size << " count : " << count << std::endl; 
    } 
    
    void List::display() const 
    { 
        for(int i = 0; i < count; i++) 
            std::cout << intptr[i] << std::endl; 
    } 
    

    .

    #include <iostream> 
    #include "list.h" 
    
    int main() 
    { 
        List mylist; 
    
        mylist.Insert(5); 
        mylist.Insert(6); 
        mylist.Insert(2); 
        mylist.Insert(8); 
        mylist.Insert(4); 
        mylist.Insert(5); 
        mylist.Insert(9); 
        mylist.Insert(8); 
        mylist.Insert(5); 
        mylist.Insert(9); 
        mylist.Insert(8); 
        mylist.Insert(5); 
        mylist.Insert(9); 
        mylist.Insert(8); 
        mylist.Insert(5); 
        mylist.Insert(9); 
    
        mylist.display(); 
        system("PAUSE"); 
    
        List mylist2(myList); // copy construct a new list
    
        mylist2.display(); 
        system("PAUSE"); 
    
        return 0; 
    } 
    

    With that said, you really should use std::vector instead, eg:

    #include <iostream>            
    #include <vector>
    #include <algorithm>
    
    void displayValue(int value)
    {
        std::cout << value << std::endl; 
    }
    
    int main()            
    {            
        std::vector<int> mylist;            
    
        mylist.push_back(5);            
        mylist.push_back(6);            
        mylist.push_back(2);            
        mylist.push_back(8);            
        mylist.push_back(4);            
        mylist.push_back(5);            
        mylist.push_back(9);            
        mylist.push_back(8);            
        mylist.push_back(5);            
        mylist.push_back(9);            
        mylist.push_back(8);            
        mylist.push_back(5);            
        mylist.push_back(9);            
        mylist.push_back(8);            
        mylist.push_back(5);            
        mylist.push_back(9);            
    
        std::for_each(mylist.begin(), myList.end(), displayValue);
        system("PAUSE");            
    
        std::vector<int> myList2(myList);
    
        std::for_each(mylist2.begin(), myList2.end(), displayValue);
        system("PAUSE");            
    
        return 0;            
     }       
    

    To take it a step further, if you want to keep using your custom List class, at least use std::vector inside of it:

    #include <vector>
    
    class List  
    {  
    public:  
        //  DEFAULT Constructor  
        List();  
    
        //Modification methods  
        void Insert(const int);  
    
        // User ACCESS methods  
        void display(void) const;  
    
    protected:  
        std::vector<int> intvec;
    };
    

    .

    #include "list.h" // Include our Class defintion 
    #include <iostream> 
    
    // CONSTRUCTOR 
    List::List()  
    { 
        intvec.reserve(2);
        std::cout << "Initial size : " << intvec.capacity() << " count : " << intvec.size() << std::endl; 
    } 
    
    // Copy constructor 
    List::List(const List& value) 
    { 
        intvec = value.intvec;
        std::cout << "Copy size : " << invec.capacity() << " count : " << intvec.size() << std::endl; 
    } 
    
    void List::Insert(const int value) 
    { 
        intvec.push_back(value); 
        std::cout << "New size : " << intvec.capacity() << " count : " << intvec.size() << std::endl; 
    } 
    
    void List::display() const 
    { 
        for(std::vector<int>::const_iterator iter = intvec.begin(), end = intvec.end(); iter != end; ++iter) 
            std::cout << *iter << std::endl; 
    } 
    

    .

    #include <iostream> 
    #include "list.h" 
    
    int main() 
    { 
        List mylist; 
    
        mylist.Insert(5); 
        mylist.Insert(6); 
        mylist.Insert(2); 
        mylist.Insert(8); 
        mylist.Insert(4); 
        mylist.Insert(5); 
        mylist.Insert(9); 
        mylist.Insert(8); 
        mylist.Insert(5); 
        mylist.Insert(9); 
        mylist.Insert(8); 
        mylist.Insert(5); 
        mylist.Insert(9); 
        mylist.Insert(8); 
        mylist.Insert(5); 
        mylist.Insert(9); 
    
        mylist.display(); 
        system("PAUSE"); 
    
        List mylist2(myList); // copy construct a new list
    
        mylist2.display(); 
        system("PAUSE"); 
    
        return 0; 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

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.