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

  • SEARCH
  • Home
  • 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 8985269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:17:39+00:00 2026-06-15T21:17:39+00:00

int arr2[5]={5,4,3,2,1}; sortArray(arr2, 5); void sortArray(int data[], int size) { int i, j; int

  • 0
int arr2[5]={5,4,3,2,1};
sortArray(arr2, 5);

void sortArray(int data[], int size)
{
    int i, j;
    int element;

    for (i = 1; i < size; i++)
    {
        element = data[i];
        j = i-1;

        while (j >= 0 && element < data[j])
        {
            data[j+1] = data[j];
            j--;
        }

        data[j] = element;
    }
}

My function gets this error and my array look like this {5,5,5,5,5} when the function ends, why so?

  • 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-15T21:17:40+00:00Added an answer on June 15, 2026 at 9:17 pm

    As stated above: during the first iteration of your for loop, after the while loop has executed, j is decremented to -1 and then used in as the index in your data array; this is the cause of your stack corruption.

    Some more info about stack corruption: when you reference a certain index of your array (IE data[j]), you’re basically saying ‘start at the location in memory pointed to by the pointer named data, add j * sizeof(int) bytes, and grab that value’.

    In code, data[j] is equivalent to *(data + (j * sizeof(int))). When you give a negative value, you reference memory not allocated to the data array; in this case the memory happens to be part of the stack. Because you’re modifying it, you get the stack corruption error.

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

Sidebar

Related Questions

I need to test if an array int arr2[4]; has specific data in a
int a = 10; int* pA = &a; long long b = 200; long
int main(void) { char testStr[50] = Hello, world!; char revS[50] = testStr; } I
int a[2]; This in memory actually looks like: //Assuming int is 2 bytes add=2000,
If i have the following: class Array{ public: Array (int n=2) : _n(n), buf(new
Suppose you have a Java method void foobar(int id, String ... args) and want
Consider this code: using namespace std; int* get() { unique_ptr<int> p (new int[4]); return
Basically you have two ways for doing this: for (int x = 0; x
What is the difference between the following declarations: int* arr1[8]; int (*arr2)[8]; int *(arr3[8]);
I read here @ java.sun that int[] iarr is a primitive array and int[][]

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.