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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:39:00+00:00 2026-06-02T19:39:00+00:00

I am writing a c++ program implementing the Merge Sort iteratively. The main code

  • 0

I am writing a c++ program implementing the Merge Sort iteratively. The main code is shown below and I cannot understand why I am having the “Access violation writing location 0xXXXXXXXX” error, even though I allocated much more memory (1 gb) in the same way in another program.

void main()
{
    //int a[size];
    int* a =  new int(size); //initialising an int array dynamically contains 16777216 el
    srand(time(NULL));
    for(int i = 0 ; i < size; i++)
    {
        a[i]= 1 + rand() % 10;
    }

    for(int i = 0;  (size / 2) / pow((double)2, i)>= 1; i++)
    {
        int n = pow((double)2, i);
        int offset = 0;
        for(int j = 0; j < (size / 2) / pow((double)2, i); j++)
        {
            int* tmp = new int(n);
            merge(a + offset, n, a + offset + n, n, tmp);
            memcpy(a + offset, tmp, n*2 * sizeof(int));
            offset += pow((double)2, i+1);
        }
    }

    for(int i = 0; i < size; i++)
    {
        cout<<a[i]<<" ";
        //printf("%d ", a[i]);
    }
    cout<<endl;
    system("PAUSE");
}
  • 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-02T19:39:01+00:00Added an answer on June 2, 2026 at 7:39 pm

    You allocate only memory enough for 1 integer: new int(size) and assign it as size. So you don’t have 1GB of memory pointed by a. Accessing it beyond 1 integer is undefined behavior, which can give you an access violation.

    Change new int(size) to square brackets: new int[size].

    And better still, use a std::vector<int>.

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

Sidebar

Related Questions

While writing my program I got cannot find symbol message when I tried to
I am writing a program to read stdin and output it. int main() {
I am writing my own shell program. I am currently implementing the cd command
I am writing a program which has 8 threads. I am implementing a barrier
I am writing a grading program for an assignment in which students are implementing
Im writing a program that should read input via stdin, so I have the
I'm writing a program which aims to generate a lot of activity in Active
I was writing a program to illustrate the effects of cache contention in multithreaded
I have been writing a program that is quite complex compared to what I
I'm writing a program that sends an email out at a client's specific local

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.