Jump to content

I'm currently tasked to implement an algorithm for Merge sort in this C++ Computing Course I'm taking right now. When I run the built executable, I get "____.exe has triggered a breakpoint." and is pointing to this in malloc.c

 

__forceinline void * __cdecl _heap_alloc (size_t size)

{

    if (_crtheap == 0) {
        _FF_MSGBANNER();    /* write run-time error banner */

        _NMSG_WRITE(_RT_CRT_NOTINIT);  /* write message */
        __crtExitProcess(255);  /* normally _exit(255) */
    }

    return HeapAlloc(_crtheap, 0, size ? size : 1);
}

 

I'm pretty sure it has to do with the way I'm allocating my dynamic arrays for merging as it's one of the last items on the call stack. My implementation for creating the dynamic array is as follows. Am I doing this right? I'm using template<class T>  as this has to work for strings, integers, and floats. 

 

int R_side = mid - low;
int L_side = high - (mid+1);
int copy_count;
T* R_arr;
T* L_arr;
    
R_arr = new T[R_side];
T_arr = new T[L_side];

 

Many thanks!

Intel® Core™ i7-12700 | GIGABYTE B660 AORUS MASTER DDR4 | Gigabyte Radeon™ RX 6650 XT Gaming OC | 32GB Corsair Vengeance® RGB Pro SL DDR4 | Samsung 990 Pro 1TB | WD Green 1.5TB | Windows 11 Pro | NZXT H510 Flow White
Sony MDR-V250 | GNT-500 | Logitech G610 Orion Brown | Logitech G402 | Samsung C27JG5 | ASUS ProArt PA238QR
iPhone 12 Mini (iOS 18.3) | iPhone 15 (iOS 18.3.1) | KZ AZ09 Pro x KZ ZSN Pro X | Sennheiser HD450bt
Intel® Core™ i7-1265U | Kioxia KBG50ZNV512G | 16GB DDR4 | Windows 11 Enterprise | HP EliteBook 650 G9
Intel® Core™ i5-8520U | WD Blue M.2 250GB | 1TB Seagate FireCuda | 16GB DDR4 | Windows 11 Home | ASUS Vivobook 15 
Intel® Core™ i7-3520M | GT 630M | 16 GB Corsair Vengeance® DDR3 |
Samsung 850 EVO 250GB | macOS Catalina | Lenovo IdeaPad P580

Link to comment
https://linustechtips.com/topic/552075-triggered-a-breakpoint/
Share on other sites

Link to post
Share on other sites

2 minutes ago, fizzlesticks said:

Is that supposed to be L_arr? 

Would help if you posted the full function.

Made a typo as I was formatting it on here. Whoops.

Disclaimer: I know this is probably implemented in the terrible manner, but I'm still a beginner so bare with me.:P

Spoiler

template <class T>
int Mergesort(T arr[], int n)
{
    int counter = 0;
    if( arr != NULL )
    {
        int mid = floor((n-1)/2);
        MergesortHelper( arr, 0, mid, n, counter);
        MergesortHelper( arr, mid+1, (n-1), n, counter);
    }
    return counter;
}

 

template <class T>
void MergesortHelper(T arr[], int low, int high, int n, int& counter)
{
    if (low < high)
    {
        int mid = low + floor((high - low)/2);
        MergesortHelper( arr, low, mid, n, counter);
        MergesortHelper( arr, mid+1, (n-1), n, counter);
        Merge( arr, low, mid, high, n, counter);
    }
    else
    {
        return;
    }
}

 

template <class T>
void Merge(T arr[], int low, int mid, int high, int n, int& counter)
{
    if ( arr == NULL )
    {
        return;
    }

 //make two temp arrays for Right and Left side, then merge
    int R_side = mid - low;
    int L_side = high - (mid+1);
    int copy_count;
    T* R_arr;
    T* L_arr;
    
    R_arr = new T[R_side];
    copy_count = 0;
    for ( int i = low; i <= mid; i++ )
    {
        R_arr[copy_count] = arr;
        copy_count++;
    }

    L_arr = new T[L_side];
    copy_count = 0;
    for ( int i = mid+1; i <= high; i++ )
    {
        R_arr = arr;
        copy_count++;
    }

 

    SelectionSort( R_arr, R_side);
    SelectionSort( L_arr, L_side);

    int copy_count_L = 0;
    int copy_count_R = 0;
    for ( int i = low; i <= high; i ++ )
    {
        if (R_arr[copy_count_R] < L_arr[copy_count_L])
        {
            arr = R_arr[copy_count_R];
            copy_count_R++;
        }
        else
        {
            arr = L_arr[copy_count_L];
            copy_count_L++;
        }
    }
}

 

Intel® Core™ i7-12700 | GIGABYTE B660 AORUS MASTER DDR4 | Gigabyte Radeon™ RX 6650 XT Gaming OC | 32GB Corsair Vengeance® RGB Pro SL DDR4 | Samsung 990 Pro 1TB | WD Green 1.5TB | Windows 11 Pro | NZXT H510 Flow White
Sony MDR-V250 | GNT-500 | Logitech G610 Orion Brown | Logitech G402 | Samsung C27JG5 | ASUS ProArt PA238QR
iPhone 12 Mini (iOS 18.3) | iPhone 15 (iOS 18.3.1) | KZ AZ09 Pro x KZ ZSN Pro X | Sennheiser HD450bt
Intel® Core™ i7-1265U | Kioxia KBG50ZNV512G | 16GB DDR4 | Windows 11 Enterprise | HP EliteBook 650 G9
Intel® Core™ i5-8520U | WD Blue M.2 250GB | 1TB Seagate FireCuda | 16GB DDR4 | Windows 11 Home | ASUS Vivobook 15 
Intel® Core™ i7-3520M | GT 630M | 16 GB Corsair Vengeance® DDR3 |
Samsung 850 EVO 250GB | macOS Catalina | Lenovo IdeaPad P580

Link to comment
https://linustechtips.com/topic/552075-triggered-a-breakpoint/#findComment-7283433
Share on other sites

Link to post
Share on other sites

It's also not complete, I'm just testing up to this point to see if it works. I still have to implement the condition where if one of the sub arrays (R and L) are empty then it'll just straight copy the rest of the other. 

Intel® Core™ i7-12700 | GIGABYTE B660 AORUS MASTER DDR4 | Gigabyte Radeon™ RX 6650 XT Gaming OC | 32GB Corsair Vengeance® RGB Pro SL DDR4 | Samsung 990 Pro 1TB | WD Green 1.5TB | Windows 11 Pro | NZXT H510 Flow White
Sony MDR-V250 | GNT-500 | Logitech G610 Orion Brown | Logitech G402 | Samsung C27JG5 | ASUS ProArt PA238QR
iPhone 12 Mini (iOS 18.3) | iPhone 15 (iOS 18.3.1) | KZ AZ09 Pro x KZ ZSN Pro X | Sennheiser HD450bt
Intel® Core™ i7-1265U | Kioxia KBG50ZNV512G | 16GB DDR4 | Windows 11 Enterprise | HP EliteBook 650 G9
Intel® Core™ i5-8520U | WD Blue M.2 250GB | 1TB Seagate FireCuda | 16GB DDR4 | Windows 11 Home | ASUS Vivobook 15 
Intel® Core™ i7-3520M | GT 630M | 16 GB Corsair Vengeance® DDR3 |
Samsung 850 EVO 250GB | macOS Catalina | Lenovo IdeaPad P580

Link to comment
https://linustechtips.com/topic/552075-triggered-a-breakpoint/#findComment-7283442
Share on other sites

Link to post
Share on other sites

Well I can't get it to throw that error but the code certainly has quite a few issues.

 

int R_side = mid - low;
    int L_side = high - (mid+1);
    int copy_count;
    T* R_arr;
    T* L_arr;
    
    R_arr = new T[R_side];
    L_arr = new T[L_side];

In this section, when low=0 and high=1 R_side and L_side will end up being 0 which depending on your compiler may be causing the issue when you try to create an new array of size 0. Later in the code you both read and write outside the bounds of those arrays which could also be the problem. 

 

R_arr[copy_count] = arr

Is trying to assign a T[] into an array of T. I'd expect that should be R_arr[copy_count] = arr

Your code shouldn't even compile with that line there.

 

R_arr = arr;

I think should be L_arr[copy_count] = arr;

 

And of course you're never deleting any of the arrays you create.

That's a start at least.

 

1474412270.2748842

Link to comment
https://linustechtips.com/topic/552075-triggered-a-breakpoint/#findComment-7283550
Share on other sites

Link to post
Share on other sites

This is an implementation of merge sort I did quite some long time ago when I was learning sorting algorithms.


template<class t>
void merge(t *ar, int p , int q, int r){
    t lowHalf[q-p+1];
    t highHalf[r-q];
    int i, j, k = p;
    for(i=0; k<=q; i++,k++) lowHalf[i] = ar[k];
    for(j =0; k<=r; j++,k++) highHalf[j] = ar[k];
    k = p;    i = 0;    j = 0;
    while((i < q-p+1) && (j < r-q))    {
        if(lowHalf[i] <= highHalf[j])
        {
            ar[k] = lowHalf[i++];
        }
        else
        {
            ar[k] = highHalf[j++];
        }
        k++;
    }
    while(i < q-p+1)    {
        ar[k++] = lowHalf[i++];
    }
    while(j < r-q)    {
        ar[k++] = highHalf[j++];
        }
}
template<class t>
void mergeSort(t *ar, int p, int r){
    if(p < r)    {
        int q = (p + r)/2;
        mergeSort(ar,p,q);
        mergeSort(ar,q+1,r);
        merge(ar, p, q ,r);   
        }
}

 

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
https://linustechtips.com/topic/552075-triggered-a-breakpoint/#findComment-7284170
Share on other sites

Link to post
Share on other sites

6 hours ago, Nineshadow said:

-snip-

Visual Studio is so finicky. I remember my professor said the compiler does not like variables when initialing the size of an array.

Untitled.png

Intel® Core™ i7-12700 | GIGABYTE B660 AORUS MASTER DDR4 | Gigabyte Radeon™ RX 6650 XT Gaming OC | 32GB Corsair Vengeance® RGB Pro SL DDR4 | Samsung 990 Pro 1TB | WD Green 1.5TB | Windows 11 Pro | NZXT H510 Flow White
Sony MDR-V250 | GNT-500 | Logitech G610 Orion Brown | Logitech G402 | Samsung C27JG5 | ASUS ProArt PA238QR
iPhone 12 Mini (iOS 18.3) | iPhone 15 (iOS 18.3.1) | KZ AZ09 Pro x KZ ZSN Pro X | Sennheiser HD450bt
Intel® Core™ i7-1265U | Kioxia KBG50ZNV512G | 16GB DDR4 | Windows 11 Enterprise | HP EliteBook 650 G9
Intel® Core™ i5-8520U | WD Blue M.2 250GB | 1TB Seagate FireCuda | 16GB DDR4 | Windows 11 Home | ASUS Vivobook 15 
Intel® Core™ i7-3520M | GT 630M | 16 GB Corsair Vengeance® DDR3 |
Samsung 850 EVO 250GB | macOS Catalina | Lenovo IdeaPad P580

Link to comment
https://linustechtips.com/topic/552075-triggered-a-breakpoint/#findComment-7285996
Share on other sites

Link to post
Share on other sites

1 hour ago, BlueChinchillaEatingDorito said:

Visual Studio is so finicky. I remember my professor said the compiler does not like variables when initialing the size of an array.

Variable length arrays aren't in the C++ standard so most compilers don't support it. It is however in the C standard so some compilers like G++ allow it anyway.

1474412270.2748842

Link to comment
https://linustechtips.com/topic/552075-triggered-a-breakpoint/#findComment-7286837
Share on other sites

Link to post
Share on other sites

7 minutes ago, fizzlesticks said:

Variable length arrays aren't in the C++ standard so most compilers don't support it. It is however in the C standard so some compilers like G++ allow it anyway.

Yeah.

If your compiler doesn't let you do it then just use dynamically allocated arrays.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
https://linustechtips.com/topic/552075-triggered-a-breakpoint/#findComment-7286888
Share on other sites

Link to post
Share on other sites

On ‎2016‎-‎02‎-‎21 at 0:40 PM, Nineshadow said:

Yeah.

If your compiler doesn't let you do it then just use dynamically allocated arrays.

 int L_side = mid - low +1;
 int R_side = high - mid +1;
 
 T* L_arr;
 T* R_arr;
 
 L_arr = new T[L_side];
 R_arr = new T[R_side];
 
It's not allowing me to do this. I've checked with my friend who also took this course and said there shouldn't be an issue. It compiles and runs will encounter a bad::alloc during execution. Trying to access the array will result in an access violation, encounter a breakpoint, then corrupt the heap. Really at a loss right now.

Intel® Core™ i7-12700 | GIGABYTE B660 AORUS MASTER DDR4 | Gigabyte Radeon™ RX 6650 XT Gaming OC | 32GB Corsair Vengeance® RGB Pro SL DDR4 | Samsung 990 Pro 1TB | WD Green 1.5TB | Windows 11 Pro | NZXT H510 Flow White
Sony MDR-V250 | GNT-500 | Logitech G610 Orion Brown | Logitech G402 | Samsung C27JG5 | ASUS ProArt PA238QR
iPhone 12 Mini (iOS 18.3) | iPhone 15 (iOS 18.3.1) | KZ AZ09 Pro x KZ ZSN Pro X | Sennheiser HD450bt
Intel® Core™ i7-1265U | Kioxia KBG50ZNV512G | 16GB DDR4 | Windows 11 Enterprise | HP EliteBook 650 G9
Intel® Core™ i5-8520U | WD Blue M.2 250GB | 1TB Seagate FireCuda | 16GB DDR4 | Windows 11 Home | ASUS Vivobook 15 
Intel® Core™ i7-3520M | GT 630M | 16 GB Corsair Vengeance® DDR3 |
Samsung 850 EVO 250GB | macOS Catalina | Lenovo IdeaPad P580

Link to comment
https://linustechtips.com/topic/552075-triggered-a-breakpoint/#findComment-7296467
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×