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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:02:44+00:00 2026-06-12T06:02:44+00:00

So I’ve written my own implementation of an ArrayList (aka a vector in C++)

  • 0

So I’ve written my own implementation of an ArrayList (aka a vector in C++) and I included several algorithms with it. Now my merge sort methods seem to be leaking memory, but I’ve gone over the code line by line, tracing allocation to deletion and it all seems to be going good!

I should note, that I have a test script for every method in the ArrayList and I was getting crashes, then I tried removing the mergesort test, and boom, no more crashes. But the interesting thing is…it doesn’t ALWAYS crash, it works sometimes, and it crashes others.

The code for the two methods is below:

A quick variable enumeration:

array = the array that backs the arrayList

size = an int that keeps track of the size of the array.

sorted = a boolean that indicates if the list is sorted

/**
 * Runs merge sort on this ArrayList<T>. Interface function to the central,
 * recursive, merge sort function.
 *
 * Runs in O(nlogn) time. However it consumes extra memory.
 */
template<class T>
void ArrayList<T>::mergeSort() {

    T* temp = mergeSort(array, size);
    delete [] array;
    array = temp;
    sorted = true;
}

/**
 * Runs merge sort on the passed in array. Recursive.
 *
 * Runs in O(nlogn) time. However it consumes extra memory.
 *
 * @param array the array to sort.
 * @param arraySize the size of the array that is to be sorted.
 * @return the sorted array.
 */
template<class T>
T* ArrayList<T>::mergeSort(T* array, int arraySize) {

    T* returnArray;

    //If the array is more than one element.
    if (arraySize > 1) {

        int size1 = arraySize / 2;
        int size2 = arraySize - size1;

        T* array1;
        T* array2;

        //Recurse.
        array1 = mergeSort(array, size1);
        array2 = mergeSort(array + size1, size2);

        //Allocate memory for return array.
        returnArray = new T[arraySize];

        //Loop through all elements in returnArray.
        int i = 0, j = 0, k = 0;
        while (i < arraySize) {

            //Place the lesser of two elements in returnArray.
            if ((array1[j] <= array2[k] && j < size1)
                    || k == size2) {

                returnArray[i] = array1[j];
                j++;
            }
            else {

                returnArray[i] = array2[k];
                k++;
            }

            i++;
        }

        //Free the memory allocated in the recursive calls.

        delete [] array1;
        delete [] array2;
        array1 = 0;
        array2 = 0;
    }
    //If one element is in the passed array.
    else {

        //Allocate memory for new array, and assign passed value to it.
        //This is done so delete can be called in the calling function.
        returnArray = new T[1];
        returnArray[0] = array[0];
    }

    return returnArray;
}
  • 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-12T06:02:45+00:00Added an answer on June 12, 2026 at 6:02 am

    You’re accessing array1 [ j ] before checking if it’s j < size1. If j >= size1 then accessing the array at that index is illegal. It might not always crash, depending on the memory layout of things in the heap, but it will sometimes crash. Your check should be like this:

    if (((j < size1) && (array1[j] <= array2[k])) || k == size2) {
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.