What is the correct way to pass pointers to an array from one C++ program to another linked C++ program?
I have a C++ program and it calls a function named SaveGatheredVec() in another C++ program that is included using the header file. SaveGatheredVec() shall return a pointer to an array that I can print from the function I called. But when I do something like below, the pointer is not getting the correct address of the array and just prints Segmentation Fault.
AS THE CODE IS LITTLE MESSY AND A LOT OF THINGS ARE GOING ON, just looking at NodesVisited[] and pointer p is what I need help with. I even tried declaring a global variable in the cpp program from where I call SaveGatheredVec and pass the array to the function, but it still gives the same error.
#include<iostream>
#inlcude "AnotherC++Program.h"
...
main(){
BFS();
}
void BFS(PSpMat<ElementType>::MPI_DCCols D, PSpMat<ElementType>::MPI_DCCols B, int ACol, string output){
int *p=0;
PSpMat<ElementType>::MPI_DCCols C = Mult_AnXBn_DoubleBuff<PTDOUBLEDOUBLE, ElementType, PSpMat<ElementType>::DCCols>(D,B);
int64_t cnnz = C.getnnz();
ostringstream tinfo;
p=C.SaveGatheredVec(output);
for(int i=0; i<cnnz; i++)
cout << "\n-----" << *(p+i);// on return it says Segmentation Fault
}
SaveGatheredVec() is defined in another C++ program linked by a header as below:
#include<iostream>
...
template <class IT, class NT, class DER>
template <class HANDLER>
int * SpParMat< IT,NT,DER >::SaveGatheredVec( string filename, HANDLER handler, bool transpose) const
{
int proccols = commGrid->GetGridCols();
int procrows = commGrid->GetGridRows();
IT totalm = getnrow();
IT totaln = getncol();
//IT totnnz = getnnz();
IT totnnz = 1;
static int NodesVisited[1]; //for example purpose, this is made just of size 1
int increament=0;
int flinelen = 0;
ofstream out;
if(commGrid->GetRank() == 0)
{
std::string s;
std::stringstream strm;
strm << "%%MatrixMarket matrix coordinate real general" << endl;
strm << totalm << " " << totaln << " " << totnnz << endl;
s = strm.str();
out.open(filename.c_str(),ios_base::trunc);
flinelen = s.length();
out.write(s.c_str(), flinelen);
out.close();
}
int colrank = commGrid->GetRankInProcCol();
int colneighs = commGrid->GetGridRows();
...
for(int j = 0; j < 2; ++j)
{
IT rowcnt = 0;
sort(csr[j].begin(), csr[j].end());
int mysize = csr[j].size();
MPI_Gather(&mysize, 1, MPI_INT, gsizes, 1, MPI_INT, 0, commGrid->GetRowWorld());
if(commGrid->GetRankInProcRow() == 0)
{
rowcnt = std::accumulate(gsizes, gsizes+proccols, static_cast<IT>(0));
std::partial_sum(gsizes, gsizes+proccols-1, dpls+1);
ents = new pair<IT,NT>[rowcnt];
}
MPI_Gatherv(SpHelper::p2a(csr[j]), mysize, datatype, ents, gsizes, dpls, datatype, 0, commGrid->GetRowWorld());
if(commGrid->GetRankInProcRow() == 0)
{
for(int k=0; k< rowcnt; ++k)
{
if (!transpose){
//NodesVisited is assigned below
NodesVisited[increament]= j + roffset + 1; increament++;
out << j + roffset + 1 << "\t" << ents[k].first + 1 << "\t";
handler.save(out, ents[k].second, j + roffset, ents[k].first);}
else
{
out << ents[k].first + 1 << "\t" << j + roffset + 1 << "\t";
handler.save(out, ents[k].second, j + roffset, ents[k].first);
}
out << endl;
}
delete [] ents;
}
}
if(commGrid->GetRankInProcRow() == 0)
{
DeleteAll(gsizes, dpls);
out.close();
}
}
MPI_Barrier(commGrid->GetWorld());
}
for(int j=0; j<totnnz;j++)
cout << "\nHere" << NodesVisited[j]; //values are printed correctly here
return NodesVisited;
}
If someone can help with an example or some ideas how should I retrieve the values of an array from a function defined in another C++, it would be of great help as I have to use the function defined in another C++ function. Thanks.