tack around the variable 'a' was corrupted

Error>> Run-Time Check Failure #2 - Stack around the variable 'a' was corrupted.

Could someone please explain Why the above error occurs when I run the following code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  #include <iostream>
using namespace std;
void quicksort(int[], int, int);
int partitioning(int[], int, int);
int main()
{
	int a[9], b[9], c[9], d[9], i, j = 0, k;
	for (k = 0; k < 4; ++k)
	{
		if (k == 0) cout << "Enter the elements of array A: ";
		if (k == 1) cout << "Enter the elements of array B: ";
		if (k == 2) cout << "Enter the elements of array C: ";
		if (k == 3) cout << "Enter the elements of array D: ";
		for (i = 0; i < 9; ++i)
		{
			if (k == 0) cin >> a[i];
			else if (k == 1) cin >> b[i];
			else if (k == 2) cin >> c[i];
			else cin >> d[i];
		}
	}
	for (k = 0; k < 4; ++k)
	{
		if (k == 0) cout << "Elements of array A Before     Quick sorting: ";
		if (k == 1) cout << "Elements of array B before Insertion sorting: ";
		if (k == 2) cout << "Elements of array C before     Shell sorting: ";
		if (k == 3) cout << "Elements of array D before     Merge sorting: ";
		for (i = 0; i < 9; ++i)
		{
			if (k == 0) cout << a[i] << " ";
			else if (k == 1) cout << b[i] << " ";
			else if (k == 2) cout << c[i] << " ";
			else cout << d[i] << " ";
		}
		
		if(k==0) quicksort(a, 0, 9);
		if(k==1) quicksort(b, 0, 9);
		if(k == 2) quicksort(c, 0, 9);
		if (k == 3) quicksort(d, 0, 9);
		if (k == 0) cout << "Elements of array A After     Quick sorting: ";
		for (i = 0; i < 9; ++i)
		{
			if (k == 0) cout << a[i] << " ";
		}
		cout << '\n';
	}
}
void quicksort(int a[], int l, int h)
{
	int u;
	if (l < h)
	{
		u = partitioning(a, l, h);
		quicksort(a, l, u - 1);
		quicksort(a, u + 1, h);
	}
}
int partitioning(int a[], int i, int j)
{
	int pivote=0, temp=0, k=0;
	pivote = a[i]; k = i;
	do
	{
		do ++i; while (a[i] < pivote);
		while (a[j] > pivote) j--;
		if (i < j)
		{
			temp = a[i];
			a[i] = a[j];
			a[j] = temp;
		}
	} while (i < j);
	a[k] = a[j];
	a[j] = pivote;
	return(j);
}
Last edited on
It means you're writing outside of the array. You need to pass the highest offset in the array as the last argument to quicksort, not the size of the array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
using namespace std;

void quicksort(int[], int, int);
int partitioning(int[], int, int);

int main()
{
    const int NumArrays{4}, Size{9};
    int a[NumArrays][Size]
    {
        { 5, 4, 2, 9, 7, 1, 3, 6, 8 },
        { 1, 3, 6, 8, 5, 4, 2, 9, 7 },
        { 8, 5, 4, 2, 1, 3, 6, 9, 7 },
        { 9, 7, 1, 3, 6, 5, 4, 2, 8 },
    };
/*
    for (int k = 0; k < NumArrays; ++k)
    {
        cout << "Enter the elements of array " << char('A' + k) << ": ";
        for (int i = 0; i < Size; ++i) cin >> a[k][i];
    }
*/
    for (int k = 0; k < NumArrays; ++k)
    {
        cout << "Elements of array " << char('A' + k) << " before sorting: ";
        for (int i = 0; i < Size; ++i) cout << a[k][i] << ' ';
        cout << '\n';
        quicksort(a[k], 0, Size - 1);
        cout << "Elements of array " << char('A' + k) << " after sorting : ";
        for (int i = 0; i < Size; ++i) cout << a[k][i] << ' ';
        cout << '\n';
    }
}

void quicksort(int a[], int l, int h)
{
    if (l < h)
    {
        int u = partitioning(a, l, h);
        quicksort(a, l, u - 1);
        quicksort(a, u + 1, h);
    }
}

int partitioning(int a[], int i, int j)
{
    int pivote = a[i], k = i;
    do
    {
        do ++i; while (a[i] < pivote);
        while (a[j] > pivote) j--;
        if (i < j) swap(a[i], a[j]);
    } while (i < j);
    a[k] = a[j];
    a[j] = pivote;
    return j;
}

Last edited on
Thanks dutch! I have learned a lot of key points of C++ language from your replay! Manny Manny thanks again!
Last edited on
Topic archived. No new replies allowed.