I'm trying to implement MergeSort using pointers , and everything works until i call the function multiple times in a row , then the code breaks and returns this error 0xC0000005 . I'm assuming it's due to memory getting full since i'm not deallocating it. I'm new to dynamic memory and pointers and don't really know how to fix it.
int* MergeV1(int *x,int n,int *y,int m)
{
int i=1,j=1,l=0;
int *aux=new int[n+m+1];
while(i<=n&&j<=m)
{
while(i<=n&&x[i]<=y[j])
aux[++l]=x[i++];
while(j<=m&&y[j]<=x[i])
aux[++l]=y[j++];
}
while(i<=n)
aux[++l]=x[i++];
while(j<=m)
aux[++l]=y[j++];
return aux;
}
int* MergeSortV1(int* v,int n)
{
if(n==1)
return v;
int m=n/2;
int lenst=m;
int lendr=m;
if(n%2)
lendr++;
int *s,*d;
s=MergeSortV1(v,lenst);
d=MergeSortV1(&v[m],lendr);
return MergeV1(s,lenst,d,lendr);
}
User contributions licensed under CC BY-SA 3.0