I don't what is the meaning of these errors

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>
using namespace 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;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
nanger@nanger-laptop:~/Desktop/try$ ./test
7
*** glibc detected *** ./test: double free or corruption (!prev): 0x0804a018 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xb7d75a85]
/lib/tls/i686/cmov/libc.so.6(cfree+0x90)[0xb7d794f0]
/usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0xb7f41b11]
/usr/lib/libstdc++.so.6(_ZdaPv+0x1d)[0xb7f41b6d]
./test(__gxx_personality_v0+0x399)[0x8048aad]
./test[0x8048b2e]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe0)[0xb7d20450]
./test(__gxx_personality_v0+0x3d)[0x8048751]
======= Memory map: ========
08048000-08049000 r-xp 00000000 08:09 196042     /home/nanger/Desktop/try/test
08049000-0804a000 rw-p 00000000 08:09 196042     /home/nanger/Desktop/try/test
0804a000-0806b000 rw-p 0804a000 00:00 0          [heap]
b7c00000-b7c21000 rw-p b7c00000 00:00 0 
b7c21000-b7d00000
b7d09000-b7d0a000 rw-p b7d09000 00:00 0 
b7d0a000-b7e53000 r-xp 00000000 08:09 343633     /lib/tls/i686/cmov/libc-2.7.so
b7e53000-b7e54000 r--p 00149000 08:09 343633     /lib/tls/i686/cmov/libc-2.7.so
b7e54000-b7e56000 rw-p 0014a000 08:09 343633     /lib/tls/i686/cmov/libc-2.7.so
b7e56000-b7e59000 rw-p b7e56000 00:00 0 
b7e59000-b7e63000 r-xp 00000000 08:09 310084     /lib/libgcc_s.so.1
b7e63000-b7e64000 rw-p 0000a000 08:09 310084     /lib/libgcc_s.so.1
b7e64000-b7e65000 rw-p b7e64000 00:00 0 
b7e65000-b7e88000 r-xp 00000000 08:09 343637     /lib/tls/i686/cmov/libm-2.7.so
b7e88000-b7e8a000 rw-p 00023000 08:09 343637     /lib/tls/i686/cmov/libm-2.7.so
b7e8a000-b7f72000 r-xp 00000000 08:09 751292     /usr/lib/libstdc++.so.6.0.9
b7f72000-b7f75000 r--p 000e8000 08:09 751292     /usr/lib/libstdc++.so.6.0.9
b7f75000-b7f77000 rw-p 000eb000 08:09 751292     /usr/lib/libstdc++.so.6.0.9
b7f77000-b7f7d000 rw-p b7f77000 00:00 0 
b7f8c000-b7f8f000 rw-p b7f8c000 00:00 0 
b7f8f000-b7f90000 r-xp b7f8f000 00:00 0          [vdso]
b7f90000-b7faa000 r-xp 00000000 08:09 310101     /lib/ld-2.7.so
b7faa000-b7fac000 rw-p 00019000 08:09 310101     /lib/ld-2.7.so
bfd28000-bfd3d000 rw-p bffeb000 00:00 0          [stack]
Aborted
Last edited on
the function definition is as below:

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

void sort_merge(int* a,int size)
{
  if (size==1) return;
  int middle=size/2;
  int *first =new int[middle];
  int *second =new int[size-middle];
  for(int i=0;i<middle;i++)
  {
    first[i]=a[i];
  }
  
  for(int i=0;i<size-middle;i++)
  {
    second[i]=a[i+middle];
  }  
  
  sort_merge(first,middle); 
  sort_merge(second,size-middle);
  
  int j=0;
  int k=0;
  *(first+middle)=2000000; (used as an INF)
  *(second-middle+size)=200000;
  
  for(int i=0;i<size;i++)
  {
    if(*(first+j)<=*(second+k))
    {
      a[i]=*(first+j);
      j++;
    }
    else
    {
      a[i]=*(second+k);
      k++;
    }
  }
  delete[] first;
  delete[] second;
} 
Last edited on
Uhh, you know? You don't need to make a copy of the array to perform a merge sort.
A properly written merge sort can take less than twenty lines.

And what is line 24 supposed to be? You know it's the same as second[size-middle];?
Last edited on
the line 24 is written in this way because it has been allocated a space of size-middle integers

int *second =new int[size-middle];
anyway, before the function returns( line 12 in the main())
sort_merge(b,size);

the array has been sorted in the right order
but when this function returns to the main() domain
the error happens
what is wrong with this?
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
Topic archived. No new replies allowed.