Вопрос

If the merge below is confusing, here is what the high level method I am trying to implement:

I have two vectors each vector<void *>

say vectorA = has three elements : void * aa, void * ab, void *ac
and vectorB = has three elements : void * ba, void * bb, void *bc

the combination is vectorC: three elements: void * ca, void * cb, void *cc where void * ca is combination of aa and ba

I have the following code to merge two vectors of type std::vector into a resultant new one: It is part of my class (QuadTree)

void QuadTree::combineInsertData( const vector <void *> &data, size_t numDataTuples, const vector <void *> &addData, size_t numAddDataTuples, vector<void *> &resData){
    cout << "\t\t in combine Function" << endl;
    cout << " \t\tnumDataTuples: " << numDataTuples << endl;
    cout << " \t\tnumAddDataTuples: " << numAddDataTuples << endl;
    for (int c = 0; c != numCols_; c++) {
        resData[c] = malloc( (numDataTuples + numAddDataTuples) * colElemSizes_[c]);
        memcpy(resData[c] ,  data[c] , numDataTuples * colElemSizes_[c]);
        memcpy(resData[c] + numDataTuples , addData[c] , numAddDataTuples * colElemSizes_[c]);
    }
}

It does not seem to be doing what it should and I am not sure why. Here is how I use it: (simple manual test)

   vector<void *> newInsertData;
    newInsertData.resize(numCols_, 0);  //numCols_ = 3 in my test case
    combineInsertData(insertData, numTuples, allSubdivData, subDivTupNum, newInsertData); 

    cout << "TEST TEST " << endl;
    cout << " insertData[0][400]" << *((double *)insertData[0] + 400) << endl;
    cout << " newInsertData[0][400]" << *((double *)newInsertData[0] + 400) << endl;

    cout << " insertData[0][5]" << *((double *)insertData[0] + 5) << endl;
    cout << " newInsertData[0][5]" << *((double *)newInsertData[0] + 5) << endl;
    cout << " insertData[0][499]" << *((double *)insertData[0] + 499) << endl;
    cout << " newInsertData[0][499]" << *((double *)newInsertData[0] + 499) << endl;

    cout << " allSubdivData[0][0]" << *((double *)allSubdivData[0] ) << endl;
    cout << " newInsertData[0][500]" << *((double *)newInsertData[0] + 500) << endl;
    cout << " allSubdivData[0][1]" << *((double *)allSubdivData[0] + 1) << endl;
    cout << " newInsertData[0][501]" << *((double *)newInsertData[0] + 501) << endl;

NOTE: I am new to C/C++ , any help is appreciated. In the test code, it seems that only the first memcpy is working while the second is not (From the combine function).

?? question ?? Is it because it is void * so that the line resData[c] + numDataTuples doesn't make sense? as to where the offset should be?

Here is what it prints out

TEST TEST                                                                                             |         CHUNK: writing -5.69494e+06
 insertData[0][400]-7.97392e+06                                                                       |         CHUNK: writing -3.51803e+06
 newInsertData[0][400]-7.97392e+06                                                                    |                tup index = 116
 insertData[0][5]-6.83384e+06                                                                         |         CHUNK: writing -5.43468e+06
 newInsertData[0][5]-6.83384e+06                                                                      |         CHUNK: writing -3.03083e+06
 insertData[0][499]-7.83881e+06                                                                       |                tup index = 131
 newInsertData[0][499]-7.83881e+06                                                                    |         CHUNK: writing -6.50737e+06
 allSubdivData[0][0]-6.86119e+06                                                                      |         CHUNK: writing -4.1256e+06
 newInsertData[0][500]649399                                                                          |                tup index = 132
 allSubdivData[0][1]-8.94236e+06                                                                      |         CHUNK: writing -6.67862e+06
 newInsertData[0][501]0    

FIXED The bug is in this line:

memcpy(resData[c] + numDataTuples , addData[c] , numAddDataTuples * colElemSizes_[c]);

it should be

memcpy(resData[c] + numDataTuples * colElemSizes_[c] , addData[c] , numAddDataTuples * colElemSizes_[c]); It is what I suspected earlier.

Это было полезно?

Решение 3

bug in this line

memcpy(resData[c] + numDataTuples , addData[c] , numAddDataTuples * colElemSizes_[c]);

it should be

memcpy(resData[c] + numDataTuples * colElemSizes_[c] , addData[c] , numAddDataTuples * colElemSizes_[c]);

since it is void pointer it doesn't know where copy starts I guess.

Другие советы

A good alternative would be simply:

std::copy(data.begin(), data.end(), std::back_inserter(newInsertData));

In general I would encourage delegating work to STL containers.

Trying to understand what you want. So start with this:

#include <vector>
#include <algorithm>
#include <iterator>

template <typename T>
std::vector<T> Combine(const std::vector<T>& v1, const std::vector<T>& v2)
{
   std::vector<T> result;
   std::copy(v1.begin(), v1.end(), std::back_inserter(result));
   std::copy(v2.begin(), v2.end(), std::back_inserter(result));
   return result;
}

int main()
{
   std::vector<void*> v1;
   std::vector<void*> v2;
   // assume that v1 and v2 have data
   //...
   std::vector<void*> r = Combine(v1, v2);
   // Now r has v1 and v2 combined as one vector.
}

Admittedly not the greatest, but if this is what you are trying to achieve, note the usage of copy algorithm with the back_inserter. You can make the code more generic by using iterators and not peg it to only use vector, but I leave that for others to improve.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top