xcode - C++ function with pointer argument -
i writing c++ program outputs data file, generates python script , calls pyplot
make plotting.
however, when pass arguments in terms of pointers, can compiled properly, cannot run. returns error. when use xcode debug mode , execute step step, gives correct result chance not always. returns error.
i doubt might caused memory allocation problem, cannot identify problem.
my codes following:
1) main
#include <iostream> #include <stdlib.h> #include <cmath> #include "pycplot.h" using namespace std; double pi = 3.1415926; int main(int argc, const char * argv[]) { int nline = 100; double * np_list = new double(nline); double * pack_fraction_np = new double (nline); (int i=0; i<nline; i++){ np_list[i] = double(i)/double(nline)*2*pi; pack_fraction_np[i] = cos(np_list[i]); } pycplot_data_fout("randompacking", nline, np_list, pack_fraction_np); pycplot_pythonscript("randompacking", "random packing"); pycplot_pyplot("randompacking", "random packing"); return 0; }
2) head file
#ifndef pycplot_h #define pycplot_h #include <iostream> #include <cmath> #include <fstream> #include <string> #include <stdlib.h> using namespace std; int pycplot_data_fout(string datafilename, int nline, double * x, double *y){ ofstream fout; fout.open(datafilename+".txt"); fout << nline << endl; (int i=0; i<nline; i++){ fout << x[i] << " " << y[i] << endl; } fout.close(); return 0; } int pycplot_pythonscript(string datafilename, string plttitle){ string strhead = "import numpy np\nimport matplotlib.pyplot plt\n"; string strpltfig = "plt.figure()\n"; string strpltplt = "plt.plot(xlist, ylist)\n"; string strplttit = "plt.title('"+plttitle+"')\n"; string strpltshw = "plt.show()\n"; string strfileopen ="f = open('"+datafilename+".txt', 'r')\n"; string strreadline ="size = map(int, f.readline().split())\n"; string strpltlist ="xlist = np.zeros((size))\nylist = np.zeros((size))\n"; string strfor = "for in range(size[0]):\n xlist[i], ylist[i] = map(float, f.readline().split())\n"; ofstream pyout; pyout.open("pycplot_"+datafilename+".py"); pyout << strhead << strfileopen << strreadline << strpltlist << strfor; pyout << strpltfig << strpltplt << strplttit << strpltshw; pyout.close(); return 0; } int pycplot_pyplot(string datafilename, string plttitle){ string strsystemsh ="source ~/.bashrc; python pycplot_"+datafilename+".py"; system(strsystemsh.c_str()); return 0; } #endif /* pycplot_h */
when ran, below error message
malloc: *** error object 0x1002002e8: incorrect checksum freed object - object modified after being freed.
you want pass array, pass actual array can sized @ runtime (std::vector
), not random pointer pointing first element of array (and in case, doesn't)
your mistake using new double(x)
instead of new double[x]
. former allocates single double
value equal x
, , latter allocates array of double
of size x
, returns pointer first element, but, i've said, wouldn't have problem @ had used std::vector
, not dabbled pointers 90s style (not mention, wouldn't have memory leak had used std::vector
).
Comments
Post a Comment