in the debugging of my program below, I found that before that the sort_merge(b,size) is about to return to the main()
the local variable array b[]=2 3 4 5 5 7 9
so the sort_merge() seems to function successfully
why the errors happen when this function is about to return?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main()
{
int b[]={2,9,5,7,4,3,5};
int size=sizeof(b)/sizeof(b[0]);
cout<<size<<endl;
sort_merge(b,size);
for(int i=0;i<size;i++)
{
cout<<b[i]<<" ";
}
cout<<endl;
return 0;
}
In lines 23 and 24, you appear to be assigning an element that is out of the range of the declared int array (e.g. *(first+middle) = 2000000). Have you tried increasing the size of the allocated arrays by 1?
That is:
int first* = new int[middle+1];
int second* = new int[size-middle+1];
That will make the assignments *(first+middle) = 2000000 and *(second-middle+size) = 2000000 legal. I believe it will also resolve the error you're getting.
In lines 23 and 24, you appear to be assigning an element that is out of the range of the declared int array (e.g. *(first+middle) = 2000000). Have you tried increasing the size of the allocated arrays by 1?
That is:
int first* = new int[middle+1];
int second* = new int[size-middle+1];
____________________________________
It is not neccessary, as I said the sort_merge function finally provided the right ordering
but it just can't return to main()
andn if I allocate one unit more the sort_merge will not be suitabe