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

The Archive Base Latest Questions

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

I am writing a function that is supposed to sort an array using merge

  • 0

I am writing a function that is supposed to sort an array using merge sorting. So far I have the two functions:

template <typename Item, typename SizeType>
void merge_sort(Item data[], SizeType size) {
SizeType size1, size2;
if(size > 1) {
    size1 = size/2;
    size2 = size - size1;
    merge_sort(data,size1);
    merge_sort((data+size1),size2);

    merge(data,size1,size2);
}
}

and:

template <typename Item, typename SizeType>
void merge(Item data[], SizeType size1, SizeType size2) {
Item* temp;
SizeType copied = 0;
SizeType copied1 = 0;
SizeType copied2 = 0;

temp = new Item[size1 + size2];

while(copied1 < size1 && copied2 < size2) {
    if(data[copied1] < (data + size1)[copied2])
        temp[++copied] = data[++copied1];
    else
        temp[++copied] = (data + size1)[++copied2];
}

while (copied1 < size1)
    temp[++copied] = data[++copied1];
while (copied2 < size2)
    temp[++copied] = (data + size1)[++copied2];

for(SizeType i = 0; i < size1 + size2; ++i)
    data[i] = temp[i];
delete [] temp;
}

When I try to run the program however, I get a debug error saying that the heap is corrupted after a normal block and that CRT detected that the application wrote to memory after the end of the heap buffer. Can anyone explain what this means and how I could fix it?

  • 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-15T11:30:33+00:00Added an answer on June 15, 2026 at 11:30 am

    You’re incrementing before using the values… so that basically makes the checks obsolete…

    use copied++ instead.

    You need the post-increment operator, because if you increment it before you use it, you get out of bounds.
    Increment and decrement operators

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

Sidebar

Related Questions

I'm writing a recursive function in Ocaml that's supposed to count the number of
I've had trouble writing this code. I'm supposed to make a function that can
I have a question about a function that I am writing with Jquery posted
I am writing a sorting program that needs to take a an array, fill
I am writing a meta function replace_type<C, X, Y> that is supposed to replace
I'm working in a CakePHP Model class, writing a function that is supposed to
I am writing a function that is supposed to do powers, but only display
i am writing pl/pgsql function that does some statistics processing. using 8.2 of postgres.
Suppose I'm writing a function that takes a list of integers and returns only
I am writing function that involve other function from base R with a lot

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.