Memory error: free (): invalid next size (fast)

I am trying to implement radix sort, and this code causes a memory failure:

(free(): invalid next size (fast))

The code is as follows:

unsigned int * radix(unsigned int *x,int n, int g,bool verbose)
{
    int elem_size = sizeof(unsigned int)*8;
    int mask = ((1<<g)-1);    
    float num_rounds= ((float)elem_size/(float)g);
    int B = 1 << g; // 2^g BTW

    vector<unsigned int> buckets[B];

    // begin radix sort
    for( unsigned int round=0 ; round<num_rounds ; ++round)
      {

        // count
        for(int elem=0 ; elem<n ; ++elem)
        {
            // calculate the bucket number:
            unsigned int const bucket_num = ( x[elem] >> g*round) & mask;
    --->        buckets[bucket_num].push_back(x[elem]);
        }
        // more stuff
    }
    return x;
}

GDBindicates that the error is inside push_back but elemalways smaller n(where nis the size x[]). So, I thought it could only be on bucket_num. However, before it breaks down, GDBit gives me its meanings:

Breakpoint 1, radix (x=0x604010, n=50, g=4, verbose=true)
    at radix.mpi.seq.cpp:38
38                buckets[bucket_num].push_back(x[elem]);
2: B = 16
1: bucket_num = 2

any ideas?

+3
source share
1 answer

From your comment, it is clear that: access to unsigned int *xis the cause of an erroneous write error that occurs when accessing it in your function radix.

unsigned int *x = new unsigned int(n); unsigned int n. unsigned ints: unsigned int *x = new unsigned int[n];

, Valgrind . , , , .

+4