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.