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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:43:13+00:00 2026-06-15T22:43:13+00:00

The purpose of the program that I’m working on is creating a class to

  • 0

The purpose of the program that I’m working on is creating a class to “improve” the default integer array data type by simulating a dynamic array of pointers. I keep running into errors when trying to delete pointers and arrays of pointers where it says “Windows has triggered a breakpoint in project4.exe.

This may be due to a corruption of the heap, which indicates a bug in project4.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while project4.exe has focus.

The output window may have more diagnostic information.”

class Array
{
private:
    int length;
int* data;
public:
    Array();
    Array(const Array &cpy);
    ~Array();
    bool addint(int toadd);
    bool deletelast();
    int getlength();
    friend ostream& operator<<(ostream &out, const Array &n);
};

ostream& operator<<(ostream &out, const Array &n);

Array::Array()
{
    length = -1;    
    data = NULL;
}

Array::Array(const Array &cpy)
{  
    length = cpy.length;                //value of length is copied

    if (length < 0)
        data = NULL;
    else
    {
        data = new int [length];

        for (int i=0; i<=length; i++)
            data[i] = cpy.data[i];
    }

}    

Array::~Array()
{
    if (length != 0)
        delete [] data;
    else
        delete data;

    data = NULL;
}  

bool Array::addint(int toadd)
{   
    length ++;
    int* point = new int[length];

    for (int i=0; i < length; i++)
        point[i] = data[i];             

    point[length] = toadd;

    if (length != 0)    
        delete [] data; 

    data = point;

    point = NULL;

    return true;
}    

bool Array::deletelast()
{
    int* temppoint;
    if (length > 0)
        temppoint = new int [length-1]; 
    else
        temppoint = new int[0];

    for (int i=0; i<length; i++)        
        temppoint[i] = data[i];

    if (length == 0)
        temppoint[0] = 0;

    length --;
    delete [] data; 
    data = temppoint;
    temppoint = NULL;

    return true;
}  

void menu(Array var)
{
    int selection=0,
        input;
    bool success;
    Array* arrcpy;
    while (selection != 3)
    {
        if (var.getlength() == -1)
        {
            cout << "What would you like to demonstrate?" << endl << "1) Add an integer " << endl
            << "2) Exit" << endl << "Enter your selection: ";
            cin >> selection;
            if (selection == 2)
                selection = 4;
        }
        else
        {
            cout << endl << "Now what would you like to demonstrate?" << endl << "1) Add an integer " << endl
            << "2) Delete the last entered integer" << endl << "3) Copy constructor" << endl << "4) Exit" << endl << "Enter your selection: ";
            cin >> selection;
        }

        if (selection==1)
        {
            cout << endl << "The length of the array before adding a new value is: " << var.getlength() + 1 << endl;
            cout << "Please enter the integer that you wish to add: ";
            cin >> input; 
            success = var.addint(input);
            if (success)
                cout << endl << "The data input was a success!" << endl << "The length of the array is now: " 
                << var.getlength() + 1 << endl << "The new value of the array is: " << var << endl;
            else
                cout << endl << "The input failed" << endl;
        }       
        if (selection == 2)
        {
            cout << endl << "The lenght of the array before the deletion is: " << var.getlength() + 1 << endl 
                << "and the value held in the array is: " << var << endl;
            success = var.deletelast();
            if (success)
                cout << endl << "The data deletion was a success!" << endl << "The length of the array is now: "
                << var.getlength() + 1 << endl << "The new value of the array is: " << var << endl;
            else
                cout << endl << "The deletion failed" << endl;
        }       
        if (selection == 3)
        {
                cout << endl << "The lenght of the array being copied is: " << var.getlength() + 1 << endl 
                << "and the value held in the array is: " << var << endl;
                arrcpy=new Array(var);
                cout << endl << "The length of the copied array is: " << arrcpy->getlength() +1 << endl
                << "and the value contained in the array is: " << *arrcpy;
                delete arrcpy;
        }
    }
}

This is all of the relevant source code that I have in terms of the issue that I’m having. It’s coming up that every instance of the delete operator and the delete [] operator has resulted in this breakpoint error and I’m not sure what I’m doing wrong.

Edit: Rewrote the code to have the value of length default to 0 instead of -1 and everything works now!

  • 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-15T22:43:14+00:00Added an answer on June 15, 2026 at 10:43 pm
    int* point = new int[length];
    

    Since your length starts at -1, the first call of this line would be to do new int[0]. Could be the problem.. If you insist on not fixing the semantics of length, you want length+1 here

    Unrelated point, you should look at how std::vector does it, instead of reallocating every addition, you should try overallocating and only reallocating if the space is filled.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Purpose: Create a program that takes two separate files, opens and reads them, assigns
How can i write a program using STANDARD UNIX UTILITIES that will read data
I have a program that must be compiled only in DEBUG mode. (testing purpose)
I'm coding a little program that has to sort a large array (up to
I'm working on a program that modifies a file, and I'm wondering if the
My program that I am writing's purpose arose with this issue: There are two
I'd like to make a program that can enter login credentials for authentication purpose
Lets say there are multiple functions throughout my program that need to append data
Background: for my computer science class, we were asked to create a program that
Suppose we have a program that returns a character array definition - so we

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.