Spirit Posted May 20, 2006 Report Share Posted May 20, 2006 Please explain me, why LabVIEW Memory Manager don't like this code. LabVIEW crashes when I call dll to find roots of polynomial. (For my University I should make this programm without using LabVIEW function - Polinomial Roots) :headbang: #include "extcode.h" #include <math.h> #define maxiter 500 #define DBL_EPSILON 1e-9 int roots(double *a,int n,double *wr,double *wi) {...} void deflate(double *a,int n,double *b,double *quad,double *err) {...} void find_quad(double *a,int n,double *b,double *quad,double *err, int *iter) {...} void diff_poly(double *a,int n,double *b) {...} void recurse(double *a,int n,double *b,int m,double *quad, double *err,int *iter) {...} void get_quads(double *a,int n,double *quad,double *x, short *error) {...} typedef struct { long dimSize; double elt[1]; } TD1; typedef TD1 **TD1Hdl; extern "C"{ __declspec (dllexport) long Polynom(TD1Hdl coef, TD1Hdl mr, TD1Hdl mi, short *error); } __declspec (dllexport) long Polynom(TD1Hdl coef, TD1Hdl mr, TD1Hdl mi, short *error) { double a[21], x[21], quad[2], wr[21], wi[21]; int n, numr, i; n=(*coef)->dimSize-1; for(i=0; i<=n; i++){ a=(*coef)->elt; } if ((n < 1) || (n > 20)) { *error=1; goto out; } *error=0; // get coefficients of polynomial if (a[0] == 0) { *error=1; goto out; } *error=0; if (a[n] == 0) { *error=1; goto out; } *error=0; // initialize estimate for 1st root pair quad[0] = 2.71828e-1; quad[1] = 3.14159e-1; // get roots get_quads( a, n, quad, x, error); roots( x, n, wr, wi); numr = roots( x, n, wr, wi); (*mr)->dimSize=numr; (*mi)->dimSize=numr; for (i=0; i<=numr; i++){ (*mr)->elt=wr; (*mi)->elt=wi; } out: return 0; } Full code and VI:Download File:post-4254-1148137671.zip Quote Link to comment
Rolf Kalbermatter Posted May 21, 2006 Report Share Posted May 21, 2006 Please explain me, why LabVIEW Memory Manager don't like this code.LabVIEW crashes when I call dll to find roots of polynomial. (For my University I should make this programm without using LabVIEW function - Polinomial Roots) n=(*coef)->dimSize-1; for(i=0; i<=n; i++){ a=(*coef)->elt; } if ((n < 1) || (n > 20)) { *error=1; goto out; } *error=0; Are you sure the coef array never contains more than 21 elements? You index it before you error out if it would contain more! (*mr)->dimSize=numr; (*mi)->dimSize=numr; for (i=0; i<=numr; i++){ (*mr)->elt=wr; (*mi)->elt=wi; } You can't just set the dimSize of an array without making sure it is big enough to hold that many information. Before setting the size you should at least use the LabVIEW memory manager function NumericArrayResize or something like that. Rolf Kalbermatter Quote Link to comment
Spirit Posted May 22, 2006 Author Report Share Posted May 22, 2006 Thanks for your answer! This really work. Changed code: if (err = NumericArrayResize (fD, 1, (UHandle*) &mr, numr)) goto out; if (err = NumericArrayResize (fD, 1, (UHandle*) &mi, numr)) goto out; for (i=0; i<numr; i++){ (*mr)->elt=wr; (*mi)->elt=wi; } (*mr)->dimSize=numr; (*mi)->dimSize=numr; Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.