An error cannot convert from 'void *' to 'float *' ` - c++

I write a c++ function and its associated mex. But the one kind of input of c++ function is double *.
The output of function pointwise_search is a pointer. I was told that I should delete it. But I do not know where I should delete it since I need it as an output.
From the answer, I know I should check the type of input by mxIsSingle. So I correct the function mexFunction. But there is an error error C2440: '=' : cannot convert from 'void *' to 'float *'.
In matlab, I should call like pointwise_search(float *p,float q, num_thres,float n, len ). If I have a vector v_in_matlab=rand(5,1) in matlab. I should I get it pointer by p=single(v_in_matlab); and then pointwise_search(p...
Thanks in advance.
#include "mex.h"
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
using namespace std;
float * pointwise_search(float *p,float *q,int num_thres, float* n, int len )
{
vector<float> P(p, p + num_thres);
vector<float> Q(q, q + num_thres);
int size_of_threshold = P.size();
float *Y=new float[len];
float *z=new float[len];
typedef vector<float > ::iterator IntVectorIt ;
IntVectorIt start, end, it, location ;
start = P.begin() ; // location of first
// element of Numbers
end = P.end() ; // one past the location
// last element of Numbers
for (int i=0;i<len;i++)
{
location=lower_bound(start, end, n[i]) ;
z[i]=location - start;
if(z[i]>0&&z[i]<size_of_threshold)
{
Y[i]=(n[i]-P[z[i]])/(P[z[i]-1]-P[z[i]])*(Q[z[i]-1]-Q[z[i]])+Q[z[i]];
}
else
{
Y[i]=Q[z[i]];
}
}
return (&Y[0]);
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
float * Numbers, *Q;
if (nrhs != 5)
{
mexErrMsgTxt("Input is wrong!");
}
float *n = (float*) mxGetData(prhs[3]);
int len = (int) mxGetScalar(prhs[4]);
int num_thres = (int) mxGetScalar(prhs[2]);
/* Input gs */
if(mxIsComplex(prhs[0])
||!mxIsSingle(prhs[0]))
mexErrMsgTxt("Input 0 should be a class Single");
/* get the pointer to gs */
Numbers=mxGetData(prhs[0]);
if(mxIsComplex(prhs[0])
||!mxIsSingle(prhs[0]))
mexErrMsgTxt("Input 0 should be a class Single");
/* get the pointer to gs */
Q=mxGetData(prhs[1]);
// float * Numbers= (float *)mxGetData(prhs[0]);
// float * Q= (float *)mxGetData(prhs[1]);
float * out= pointwise_search(Numbers,Q,num_thres,n,len );
//float* resizedDims = (float*)mxGetPr(out);
}

In Matlab use single() to convert the data before calling the mexFunction. On the C++ side verify that the type is indeed single by mxIsSingle(). After this you can happily cast to float*.

Before you worry about your MEX code, have another look at your C++ function first. You have some really obvious memory leaks (new but no delete[]).
Regarding MEX you should never see this:
(float *)mxGetPr(prhs[0])
You can't cast a double* to a float* and expect the numbers to make any sense. Input single from MATLAB and use:
(float *)mxGetData(prhs[0])
And do as Trilarion suggests and test all of your input mxArrays for the expected data type.

Related

error invalid conversion from 'float*' to 'int' [-fpermissive]

Ok so, i'm trying to covnert some number into others via function. The vector "tempc" contains 7 celsius numbers. The type "tempf" will contain fahrenheit. But i get this error in the title "error invalid conversion from 'float*' to 'int' [-fpermissive]". I'm new to programming , so i'm kinda really newbie. Sorry for the code but it's in italian
#include <iostream>
using namespace std;
//dichiarazione variabili e vettori
float tempc[7] = {23,25,22,19,18,20,16};
float tempf[7];
const int MAX_GRADI = 7;
//funzione conversione
int ConversioneInF ()
{
for (int i=0;i<MAX_GRADI;i++)
tempf[i]=(9/5)*tempc[i]+32; //C in F
return tempf;
}
int main ()
{
ConversioneInF();
for (int i=0;i<MAX_GRADI;i++)
cout<<"La temperatura in fahrenheit e' :"<<tempf[i]<<endl;
return 0;
}
In your function ConversioneInf you are returning tempf.
This is a float-array, which in c++ is a pointer to float (float*)
This type cannot be converted to the specified return type int.
As you are not using the return value, just change it to void:
void ConversioneInF ()
void ConversioneInF ()
{
for (int i=0;i<MAX_GRADI;i++)
tempf[i]=(9.0/5.0)*tempc[i]+32;
}
Anyway, tempf is global, you don't need to return it.

Error error: ‘void*’ is not a pointer-to-object type

Hello Guys I am facing a slight issue in a code
This program is supposed to calculate
( (a+b) x (c+d) ) / e
Create three threads, one for
Addition Only
Multiplication Only
Division
ONLY one thread should be created in main()
Display the results in division thread.
The int values(a, b, c, d, e) should be taken from the user in main and passed on the thread created in main. The other result of each step should be passed on the next step.
And this is the program that i wrote for above scenario
#include <iostream>
#include <pthread.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
void *Division(void *arg_div)
{
int result = arg_div[0]/arg_div[1];
cout << result ;
pthread_exit(NULL);
}
void *Multiplication(void *arg_mul)
{
int arg1[2];
arg1[0]=arg_mul[0]*arg_mul[1];
arg1[1]=arg_mul[2];
pthread_t div;
pthread_create(&div,NULL,Division,(void*)arg1);
pthread_join(div,NULL);
pthread_exit(NULL);
}
void *Addition(void *arg_add)
{
int arg[3];
arg[0]=arg_add[0]+arg_add[1];
arg[1]=arg_add[2]+arg_add[3];
arg[2]=arg_add[4];
pthread_t ad;
pthread_create(&ad,NULL,Multiplication,(void*)arg);
pthread_join(ad,NULL);
pthread_exit(NULL);
}
int main()
{
int values[5];
for(int i=0;i<5;i++)
{
cin >> values[i];
}
pthread_t pa;
pthread_create(&pa,NULL,Addition,(void*)values);
pthread_join(pa,NULL);
return 0;
}
Why did you change the pointer type (C-style...) to void*? (trick question to make you walk on the proper path).
Just get the int* back, they are pointers that you can dereference. void* are pointers, but you can't get a void object and even less do operations on it.
int* args = static_cast<int*>(arg);
arithmetic on void pointer is not allowed, you need to convert void * to target type here i.e. int*
after converting to void* to int*, your Addition() looks like below, you have to make same changes to all function to make it work.
void *Addition(void *arg_add)
{
int *input = (int *)arg_add;
int arg[3];
arg[0]=input[0]+input[1];
arg[1]=input[2]+input[3];
arg[2]=input[4];
}
You can't dereference a void* - you need to cast the thread functions' parameters back to int*.
For example,
void *Division(void *arg_div)
{
int* args = static_cast<int*>(arg_div);
int result = args[0]/args[1];
...
void * Division(void * arg_div) {
int * arg_div_int = (int * ) arg_div;
int result = arg_div_int[0] / arg_div_int[1];
...
}
void * Multiplication(void * arg_mul) {
int * arg_mul_int = (int * ) arg_mul;
int arg1[2];
arg1[0] = arg_mul_int[0] * arg_mul_int[1];
arg1[1] = arg_mul_int[2];
...
}
void * Addition(void * arg_add) {
int * arg_add_int = (int * ) arg_add;
int arg[3];
arg[0] = arg_add_int[0] + arg_add_int[1];
arg[1] = arg_add_int[2] + arg_add_int[3];
arg[2] = arg_add_int[4];
..
}
Here is a fix to your problem. You have to convert the void* to a int* and then use the values in the int array.

How to use realloc in cpp

I have following cpp code
#include <stdio.h> /*utiliser printf*/
#include <fcntl.h>
#include <math.h> /*utiliser pour les formules de math*/
#include <malloc.h>
#include <iostream.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/* Le type nombre complexe */
typedef struct {
double Preel;
double Pimaginaire;
} COMPLEXE;
#define ALLOC_ERROR 1
void indienne(double *MatE, int tE, int nE, double *SortieExp, double *Tempbis)
{
double *TempE=NULL, *SortieE=NULL;
int *dec=NULL;
int i, tampon, kE;
kE=(int)(log(nE)/log(2));
if(nE==8)
kE=3;
/* ALLOCATION DES MATRICES*/
if (!(TempE = (double *)calloc(tE * tE, sizeof(double))))
exit(ALLOC_ERROR);
printf("check1 te=%d, nE=%d",tE,nE);
if (!(dec = (int *)realloc(kE , sizeof(int))))
exit(ALLOC_ERROR);
if (!(SortieE = (double *)calloc(tE * tE, sizeof(double))))
exit(ALLOC_ERROR);
printf("check2 te=%d",tE);
memcpy(TempE,MatE,tE * tE * sizeof(double));
for (i=0; i<tE; i++)
*(Tempbis+(tE * i) + i) = 1.0;
if (nE==1)
{
memcpy(SortieExp, MatE, tE*tE*sizeof(double));
}
else
{
printf("kE=%d, nE=%d\n", kE, nE);
if (nE%2==0)
decompose(kE, nE,dec);
else
decompose(kE, nE-1, dec);
for (i=0; i<kE; i++)
{
carre(TempE, tE, SortieE);
memcpy(TempE, SortieE, tE*tE*sizeof(double));
tampon=*(dec+i);
if (tampon==1)
{
mult(Tempbis, tE, tE, SortieE, tE, tE, SortieExp);
memcpy(Tempbis, SortieExp, tE*tE*sizeof(double));
}
}
if (nE%2 !=0)
{
memcpy(Tempbis, SortieExp, tE*tE*sizeof(double));
mult(Tempbis, tE, tE, MatE, tE, tE, SortieExp);
}
}
free(TempE);
free(SortieE);
free(dec);
}
When I compile this code following error occurres
invalid conversion from 'int' to 'void*' [-fpermissive]|
that is about following line of code
if (!(dec = (int *)realloc(kE , sizeof(int))))
How can I remove this error?
You are passing int kE as the first parameter here:
realloc(kE , sizeof(int))
However, realloc is declared like this:
void *realloc(void *ptr, size_t size);
In other words it expects a pointer! Please read the manual page I link to above to get more details. In short, you probably want something like this for the error line:
if (!(dec = (int *)realloc(dec , sizeof(int))))
Note that this is slightly bad, because if realloc fails, you lose the original value of dec, causing a memory leak. It doesn't really matter if you are going to exit on error, but otherwise you should keep the original value of dec, so you can handle error more gracefully than just exiting.
Another note, you really should use C++ container classes like vector instead of fooling around with C memory allocation functions.
There are probably other issues in your code, but this answer doesn't try to be code review, but just explain why you get the error you get.
Try without do it like this :
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int *pa = malloc(10 * sizeof *pa); // allocate an array of 10 int
if(pa) {
printf("%zu bytes allocated. Storing ints: ", 10*sizeof(int));
for(int n = 0; n < 10; ++n)
printf("%d ", pa[n] = n);
}
int *pb = realloc(pa, 1000000 * sizeof *pb); // reallocate array to a larger size
if(pb) {
printf("\n%zu bytes allocated, first 10 ints are: ", 1000000*sizeof(int));
for(int n = 0; n < 10; ++n)
printf("%d ", pb[n]); // show the array
free(pb);
} else { // if realloc failed, the original pointer needs to be freed
free(pa);
}
}

C++ Deep Copy Object

I am trying to deep copy objects back and forth. When I run the gdb, I get the following error after one iteration of the loop.
Program received signal SIGSEGV, Segmentation fault.
0x0804ab96 in DGCPM::DGCPM (this=0x844b760, cur=0x1) at DGCPM.C:27
27 memcpy(vRCells, cur->vRCells,sizeof(float)*nThetaCells);
I suspect the problem has to do with creating the "new class," but I'm not sure. Any suggestions?
(Note: The "_initialize" code calls a FORTRAN subroutine that sets the values in the program.)
Here is the run.C main file:
#include "../include/DGCPM.h"
#define particle_num 5
class DGCPM **mallocModels(int n);
int main(int argc, char *argv[]){
class DGCPM **m;
class DGCPM **cur;
m=mallocModels(particle_num);//update
for(int t = 0; t < 48; t++){
//Update m, and then...
cur = m;
m = (DGCPM**)malloc(sizeof(class DGCPM *)*particle_num);
for(int i=0;i<particle_num;i++){
randomidx = ((double)rand() / ((double)RAND_MAX + 1));
currentidx = find(cumPw,randomidx,particle_num);
m[i] = new class DGCPM(cur[currentidx]);
}
for(int i=0;i<particle_num;i++){
delete cur[i];
}
free(cur);
}
return 0;
}
/*============================================================================
mallocModels - allocate the ensemble of models
============================================================================*/
class DGCPM **mallocModels(int n){
class DGCPM **m;
m=(class DGCPM **)amjSafeMalloc(sizeof(class DGCPM *)*n,
(char *)"mallocModels:m");
for(int i=0;i<n;i++)
m[i]=new class DGCPM();
return m;
}
/*============================================================================
Find - Return a particle index that has a high probability of having a high weight.
============================================================================*/
int find(float *cumPw, double randomidx, int nM){
/*Wrong implementation*/
int index = 0;
flag = 0;
while(flag == 0){
if(cumPw[i] >= randomidx){
flag = 1;
i++;
}
else{
index ++;
}
}
return index; //Sometimes, index was going to number of models, or number of models + 1, which are out of bounds.
/*Correct implementation*/
int index = 0;
for(int i = 0; i < nM-1; i++){
if(cumPw[i] >= randomidx){
index = i;
break;
}
}
if(index >= nM){
index = nM-1;
printf("Error: random index exceeds bounds");
}
return index;
}
Here is the DGCPM.h header file:
class DGCPM{
public:
DGCPM(); /* Initialized with defaults setup */
DGCPM(class DGCPM *cur); //Copy constructor
DGCPM(int nThetaCells, int nPhiCells, float thetaMin, float thetaMax);
~DGCPM(); /* Free memory */
private:
int internal; /* 1=memory allocated internally and should be deallocated when ~DGCPM is called, 2=memory is internal except for mGridN which is external */
int nThetaCells,nRCells,nPhiCells;
float thetaMin,thetaMax;
float rMin,rMax;
float delR,delPhi;
float deltMax;
float *vRCells; /* [nThetaCells] */
float *vThetaCells; /* [nThetaCells] */
float *vPhiCells; /* [nPhiCells] */
float **mGridB; /* [nPhiCells][nThetaCells] */
float **mGridBi; /* [nPhiCells][nThetaCells] */
float **mGridPot; /* [nPhiCells][nThetaCells] */
float **mGridEr; /* [nPhiCells][nThetaCells] */
float **mGridEp; /* [nPhiCells][nThetaCells] */
float **mGridVr; /* [nPhiCells][nThetaCells] */
float **mGridVp; /* [nPhiCells][nThetaCells] */
float **mGridN; /* [nPhiCells][nThetaCells] */
float **mGridHalf; /* [nPhiCells][nThetaCells] Particles / weber (workspace for upwind and superbee) */
float **mGridDen; /* [nPhiCells][nThetaCells] */
float **mGridVol; /* [nPhiCells][nThetaCells] */
float **mGridX; /* [nPhiCells][nThetaCells] */
float **mGridY; /* [nPhiCells][nThetaCells] */
float **mGridOc; /* [nPhiCells][nThetaCells] */
float **std; /* [nPhiCells][nThetaCells] */
float parI[2];
float delTMax;
float Re;
void initialize(int nThetaCells, int nPhiCells, float thetaMin,
float thetaMax);
};
And finally the DGCPM.C object wrapper:
/******************************************************************************
* DGCPM.C - This implements the DGCPM plasmasphere model class *
******************************************************************************/
#define TWO_PI 6.2831853071795864769252866
#include "../include/DGCPM.h"
# include <cstdlib>
# include <cmath>
/*============================================================================
DGCPM::DGCPM()
Initialize with default setup
============================================================================*/
DGCPM::DGCPM(){
internal=1;
initialize(200,200,14.963217,60.0);/*(180,200,14.963217,60.0);*/
}
//Copy Constructor
DGCPM::DGCPM(class DGCPM *cur){
internal=1;
initialize(200,200,14.963217,60.0);/*(180,200,14.963217,60.0);*/
memcpy(vRCells, cur->vRCells,sizeof(float)*nThetaCells);
memcpy(vPhiCells, cur->vPhiCells,sizeof(float)*nPhiCells);
memcpy(vThetaCells, cur->vThetaCells,sizeof(float)*nThetaCells);
memcpy(mGridB[0], cur->mGridB[0],sizeof(float)*nThetaCells*nPhiCells);
memcpy(mGridBi[0], cur->mGridBi[0],sizeof(float)*nThetaCells*nPhiCells);
memcpy(mGridPot[0], cur->mGridPot[0],sizeof(float)*nThetaCells*nPhiCells);
memcpy(mGridEr[0], cur->mGridEr[0],sizeof(float)*nThetaCells*nPhiCells);
memcpy(mGridEp[0], cur->mGridEp[0],sizeof(float)*nThetaCells*nPhiCells);
memcpy(mGridVr[0], cur->mGridVr[0],sizeof(float)*nThetaCells*nPhiCells);
memcpy(mGridVp[0], cur->mGridVp[0],sizeof(float)*nThetaCells*nPhiCells);
memcpy(mGridN[0], cur->mGridN[0],sizeof(float)*nThetaCells*nPhiCells);
memcpy(mGridHalf[0], cur->mGridHalf[0],sizeof(float)*nThetaCells*nPhiCells);
memcpy(mGridDen[0], cur->mGridDen[0],sizeof(float)*nThetaCells*nPhiCells);
memcpy(mGridVol[0], cur->mGridVol[0],sizeof(float)*nThetaCells*nPhiCells);
memcpy(mGridOc[0], cur->mGridOc[0],sizeof(float)*nThetaCells*nPhiCells);
memcpy(mGridX[0], cur->mGridX[0],sizeof(float)*nThetaCells*nPhiCells);
memcpy(mGridY[0], cur->mGridY[0],sizeof(float)*nThetaCells*nPhiCells);
memcpy(std[0], cur->std[0],sizeof(float)*nThetaCells*nPhiCells);
}
/*============================================================================
DGCPM::~DGCPM()
Free allocated memory
============================================================================*/
DGCPM::~DGCPM(){
if(internal>=1){
amjFree1dFloat(vRCells);
amjFree1dFloat(vThetaCells);
amjFree1dFloat(vPhiCells);
amjFree2dFloat(mGridB);
amjFree2dFloat(mGridBi);
amjFree2dFloat(mGridEr);
amjFree2dFloat(mGridEp);
amjFree2dFloat(mGridVr);
amjFree2dFloat(mGridVp);
if(internal==1) amjFree2dFloat(mGridN);
amjFree2dFloat(mGridHalf);
amjFree2dFloat(mGridDen);
amjFree2dFloat(mGridVol);
amjFree2dFloat(mGridX);
amjFree2dFloat(mGridY);
amjFree2dFloat(mGridOc);
amjFree2dFloat(std);
}
}
/******************************************************************************
******************************************************************************
** Private functions **
******************************************************************************
******************************************************************************/
/*============================================================================
DGCPM::initialize(int nThetaCells, int nPhiCells, float thetaMin,
float thetaMax);
This is the initialization function used when all memory should be
allocated internally.
============================================================================*/
void DGCPM::initialize(int nThetaCells, int nPhiCells, float thetaMin,
float thetaMax){
initialize(nThetaCells,nPhiCells,thetaMin,thetaMax,
amjMalloc1dFloat(nThetaCells,(char *)"DGCPM::DGCPM:vRCells"),
amjMalloc1dFloat(nThetaCells,(char *)"DGCPM::DGCPM:vThetaCells"),
amjMalloc1dFloat(nPhiCells,(char *)"DGCPM::DGCPM:vPhiCells"),
amjMalloc2dFloat(nPhiCells,nThetaCells,
(char *)"DGCPM::DGCPM:mGridB"),
amjMalloc2dFloat(nPhiCells,nThetaCells,
(char *)"DGCPM::DGCPM:mGridBi"),
amjMalloc2dFloat(nPhiCells,nThetaCells,
(char *)"DGCPM::DGCPM:mGridPot"),
amjMalloc2dFloat(nPhiCells,nThetaCells,
(char *)"DGCPM::DGCPM:mGridEr"),
amjMalloc2dFloat(nPhiCells,nThetaCells,
(char *)"DGCPM::DGCPM:mGridEp"),
amjMalloc2dFloat(nPhiCells,nThetaCells,
(char *)"DGCPM::DGCPM:mGridVr"),
amjMalloc2dFloat(nPhiCells,nThetaCells,
(char *)"DGCPM::DGCPM:mGridVp"),
amjMalloc2dFloat(nPhiCells,nThetaCells,
(char *)"DGCPM::DGCPM:mGridN"),
amjMalloc2dFloat(nPhiCells,nThetaCells,
(char *)"DGCPM::DGCPM:mGridHalf"),
amjMalloc2dFloat(nPhiCells,nThetaCells,
(char *)"DGCPM::DGCPM:mGridDen"),
amjMalloc2dFloat(nPhiCells,nThetaCells,
(char *)"DGCPM::DGCPM:mGridVol"),
amjMalloc2dFloat(nPhiCells,nThetaCells,
(char *)"DGCPM::DGCPM:mGridX"),
amjMalloc2dFloat(nPhiCells,nThetaCells,
(char *)"DGCPM::DGCPM:mGridY"),
amjMalloc2dFloat(nPhiCells,nThetaCells,
(char *)"DGCPM::DGCPM:mGridOc"),
//Added by J.Wise
amjMalloc2dFloat(nPhiCells,nThetaCells,
(char *)"DGCPM::DGCPM:std"));
}
/*============================================================================
DGCPM::initialize(int nThetaCells, int nPhiCells, float thetaMin,
float thetaMax);
This is the initialization function used when mGridN is passed from
the outside but all other memory is allocated internally.
============================================================================*/
void DGCPM::initialize(int nThetaCells, int nPhiCells, float thetaMin,
float thetaMax, float **mGridN){
initialize(nThetaCells,nPhiCells,thetaMin,thetaMax,
amjMalloc1dFloat(nThetaCells,(char *)"DGCPM::DGCPM:vRCells"),
amjMalloc1dFloat(nThetaCells,(char *)"DGCPM::DGCPM:vThetaCells"),
amjMalloc1dFloat(nPhiCells,(char *)"DGCPM::DGCPM:vPhiCells"),
amjMalloc2dFloat(nPhiCells,nThetaCells,
(char *)"DGCPM::DGCPM:mGridB"),
amjMalloc2dFloat(nPhiCells,nThetaCells,
(char *)"DGCPM::DGCPM:mGridBi"),
amjMalloc2dFloat(nPhiCells,nThetaCells,
(char *)"DGCPM::DGCPM:mGridPot"),
amjMalloc2dFloat(nPhiCells,nThetaCells,
(char *)"DGCPM::DGCPM:mGridEr"),
amjMalloc2dFloat(nPhiCells,nThetaCells,
(char *)"DGCPM::DGCPM:mGridEp"),
amjMalloc2dFloat(nPhiCells,nThetaCells,
(char *)"DGCPM::DGCPM:mGridVr"),
amjMalloc2dFloat(nPhiCells,nThetaCells,
(char *)"DGCPM::DGCPM:mGridVp"),
mGridN,
amjMalloc2dFloat(nPhiCells,nThetaCells,
(char *)"DGCPM::DGCPM:mGridHalf"),
amjMalloc2dFloat(nPhiCells,nThetaCells,
(char *)"DGCPM::DGCPM:mGridDen"),
amjMalloc2dFloat(nPhiCells,nThetaCells,
(char *)"DGCPM::DGCPM:mGridVol"),
amjMalloc2dFloat(nPhiCells,nThetaCells,
(char *)"DGCPM::DGCPM:mGridX"),
amjMalloc2dFloat(nPhiCells,nThetaCells,
(char *)"DGCPM::DGCPM:mGridY"),
amjMalloc2dFloat(nPhiCells,nThetaCells,
(char *)"DGCPM::DGCPM:mGridOc"),
amjMalloc2dFloat(nPhiCells,nThetaCells,
(char *)"DGCPM::DGCPM:std"));
}
/*
initialize() - this initialization function uses pre-allocated
memory areas passed in from the outside. This function is used both
when DGCPM allocates memory itself and when it receives
pre-allocated memory from the outside in order to eliminate
duplication of code with the associated risk of errors.
============================================================================*/
void DGCPM::initialize(int nThetaCells, int nPhiCells, float thetaMin,
float thetaMax, float *vRCells, float *vThetaCells,
float *vPhiCells, float **mGridB, float **mGridBi,
float **mGridPot, float **mGridEr, float **mGridEp,
float **mGridVr, float **mGridVp, float **mGridN,
float **mGridHalf, float **mGridDen, float **mGridVol,
float **mGridX, float **mGridY, float **mGridOc, float **std){
DGCPM::nThetaCells=nThetaCells;
DGCPM::nPhiCells=nPhiCells;
DGCPM::thetaMin=thetaMin;
DGCPM::thetaMax=thetaMax;
DGCPM::vRCells=vRCells;
DGCPM::vThetaCells=vThetaCells;
DGCPM::vPhiCells=vPhiCells;
DGCPM::mGridB=mGridB;
DGCPM::mGridBi=mGridBi;
DGCPM::mGridPot=mGridPot;
DGCPM::mGridEr=mGridEr;
DGCPM::mGridEp=mGridEp;
DGCPM::mGridVr=mGridVr;
DGCPM::mGridVp=mGridVp;
DGCPM::mGridN=mGridN;
DGCPM::mGridHalf=mGridHalf;
DGCPM::mGridDen=mGridDen;
DGCPM::mGridVol=mGridVol;
DGCPM::mGridX=mGridX;
DGCPM::mGridY=mGridY;
DGCPM::mGridOc=mGridOc;
DGCPM::std=std;
Re=6.378e6;
initialize_(&nThetaCells,&nRCells,&nPhiCells,&thetaMin,&thetaMax,&rMin,&rMax,
&delR,&delPhi,vRCells,vThetaCells,vPhiCells,mGridB[0],mGridBi[0],
mGridN[0],mGridDen[0],mGridVol[0],mGridX[0],mGridY[0],mGridOc[0],std[0]);
}
Here's a sample custom memory function, which takes care of initialization and allocation:
void *amjSafeMalloc(int n, char *message){
void *d;
d=malloc(n);
if(d==NULL){
fprintf(stderr,"amjSafeMalloc error: Could not allocate %d bytes "
"for %s. Exiting.\n",n,message);
exit(1);
}
return d;
}
float *amjMalloc1dFloat(int a, char *message){
float *d;
sprintf(msg,"%s:amjMalloc1DFloat:d",message);
d=(float *)amjSafeMalloc(sizeof(float)*a,msg);
return d;
}
float **amjMalloc2dFloat(int a, int b, char *message){
float **d;
int i;
sprintf(msg,"%s:amjMalloc2DFloat:d",message);
d=(float **)amjSafeMalloc(sizeof(float *)*a,msg);
sprintf(msg,"%s:amjMalloc2DFloat:d[0]",message);
d[0]=(float *)amjSafeMalloc(sizeof(float)*a*b,msg);
for(i=1;i<a;i++) d[i]=d[i-1]+b;
return d;
}
class DGCPM
{
public:
DGCPM(int nThetaCells, int nPhiCells)
: nThetaCells(nThetaCells)
, nPhiCells(nPhiCells)
, mGridB(nThetaCells, vector<float>(nPhiCells)) // first Y then X
{
}
private:
int nThetaCells, nPhiCells;
vector<vector<float>> mGridB;
};
Deep copies for free. Deletes memory for free.
By free I mean you don't have to write the code..
From your comment /* [nPhiCells][nThetaCells] */ in your class definition, I take it that you intent the float** to be 2D arrays. However, if you can use them like 2D arrays, they are actually arrays of pointers to arrays. That is a huge difference: it means, you have to copy nPhiCells individual arrays of nThetaCells elements and you have to setup the pointer array itself. Now, when you do
memcpy(mGridHalf[0], cur->mGridHalf[0],sizeof(float)*nThetaCells*nPhiCells);
in your copy constructor, you assume that there is no pointer array, and that all line arrays are sequential in memory. Either this copy exceeds the bounds of the pointer array (segfaulting), or accessing you array via mGridHalf[i][j] simply does the wrong thing, reinterpreting float data as pointers (and segfaulting).
Unfortunately, C++ is a horrible language for interacting with fortran multidimensional arrays because it has no notion of variable sized arrays. So the following is C code, not C++ code. In C, you can tackle the issue like this:
float (*mGridHalf)[nThetaCells] = malloc(nPhiCells*sizeof(*mGridHalf));
will correctly allocate and type a 2D array (i. e. an array of arrays) that can be accessed with
mGridHalf[phi][theta] = 7.3;
Since all elements are consecutive in memory, the entire thing can correctly be copied with
memcpy(mGridHalf, cur->mGridHalf, nPhiCells*sizeof(*mGridHalf));
and freed with
free(mGridHalf);
Technically, mGridHalf is now a pointer to an array, the pointer arithmetic that is invoked by the array access effectively does the same computation as if you had written:
float* foo = malloc(nPhiCells*nThetaCells*sizeof(*foo));
foo[phi*nThetaCells + theta] = 7.3;
However, using the correct pointer type float (*)[nThetaCells] allows you to avoid doing the index computation yourself.
The issue is more than likely you're assuming that float** has data that is one contiguous chunk of memory. If so, here is one way of accomplishing this. First, I show the wrong way (but used often):
float** createFloat2D(int nRows, int nCols)
{
float** p1 = new float*[nRows];
for (int i = 0; i < nCols; ++i )
p1[i] = new float[nCols];
return p1;
}
void destroyFloat2D(float**f, int nRows, int nCols)
{
for (int i = 0; i < nCols; ++i )
delete [] f[i];
delete [] f;
}
Looks simple, and works for most purposes, but will fail if the assumption is made that the data is in a contiguous chunk of memory.
The other way to create a 2D array is to make the data contiguous.
float** createFloat2D(int nRows, int nCols)
{
float** p1 = new float*[nRows]; // allocate row pointers
float* p2 = new float[nRows * nCols]; // allocate data in one chunk
for (int i = 0; i < nCols; ++i, p2 += nCols )
p1[i] = p2; // point the row pointers into the pool of memory
return p1;
}
void destroyFloat2D(float**f)
{
delete [] f[0];
delete [] f;
}
Note above that the data is created in one contiguous "pool". Now, using yourArray[0] actually points to the beginning of this memory. Also note that destruction is done without having to know the number of rows or columns, since f[0] points to the pool of memory.
So now, code like this should work
float** mGridB = createFloat2D(nThetaCells, nPhiCells);
//...
memcpy(mGridB[0], cur->mGridB[0], sizeof(float)*nThetaCells*nPhiCells);
The code above now works correctly, if we use the second method of creating the 2d array.
I would still stick with the vector for 1-d float arrays, as you have the pointer to the data (see my earlier comment). For the code above, I would wrap it in a class that handles creation and destruction easily.
The last thing is the copy constructor. A copy constructor in C++ has the following possible signatures:
DGCPM(const DGCPM&);
DGCPM(DGCPM&);
DGCPM(volatile DBCPM&);
I may have missed one, but the signature should be one of those above, more than likely, the first one (you can also have additional arguments after the reference argument, but they all must have default values).
Note that a DBCPM* is not a valid argument for a copy constructor as your code stated -- remember that a copy constructor is not only for use, but also the compiler will use it to make copies. So to signal the compiler that "yes, this function is used to make copies", your function must match one of the signatures above.
In addition, you need an assignment operator, in other words, the class needs to implement the "rule of 3".
This going to sound so stupid (elementary programming error): my index "i" was going beyond (number of models - 1), so I was getting a segmentation fault from accessing memory that didn't exist.

Reading dynamically allocated arrays into lists

Currently, I have been reading lists of data from a binary data file programmatically as follows:
tplR = (double*) malloc(sampleDim[0]*sizeof(double));
printf("tplR = %d\n", fread(tplR, sizeof(double), sampleDim[0], dfile));
However, as I want to use find_if() function on those lists, I would need to get tplR into a list type in stl. In terms of general C++ programming practice, is it usually good practice to make tplR into a list only when I really have to?
If I do make another member variable, for example, tplRList, what would be the easiest way of pushing all sampleDim[0] number of double precision entries into tplRList from tplR? Pushing them one by one until the incremental counter is equal to sampleDim[0]?
Thanks in advance.
You can use find_if with the array like this:
bool equals(int p)
{
return p == 9;
}
int main(int argc,char *argv[])
{
int a[10];
for(int i = 0; i < 10; ++i)
{
a[i] = i;
}
int* p = std::find_if(a, a+10, equals);
cout<<*p;
return 0;
}
You're mistaken in your assumptions. std::find_if() merely requires an iterator, not necessarily an STL iterator. As it happens, double* supports both * and ++, so it too is an iterator.
bool checkValue(double val);
std::find_if(tplR, tplR + sampleDim[0], checkValue);
#include <boost/lambda/lambda.hpp\>
using namespace boost::lambda;
static const double whateverValueYouWant(12.);
double *const tplR = (double*) malloc(sampleDim[0]*sizeof(double));
const size_t actualItemsRead = fread(tplR, sizeof(double), sampleDim[0], dfile);
printf("tplR = %d\n", actualItemsRead );
const double *begin = tplR;
const double *const end = tplR + actualItemsRead;
const double *const foundItem = std::find_if( begin, end, _1== whateverValueYouWant);
if( foundItem!=end )
{
//value found
}
else
{
//no such value
}