我遇到了一個問題。我試圖創建一個dll,它可以計算兩個點之間的距離,當我發送兩組不同的值時。 然而,當我在第二組中值的發送,我意識到我的第一組值也水漲船高陣列中丟失(數組被用來存儲值)在C++中創建一個用於不斷計算數學函數的dll
下面是我的代碼:
int click = 0; //click is used to measure the number of times i have clicked, ie to say the number of times im injecting new (a,b) points into the function below.
double MeasureDistance(double a, double b)
{
/******* Create Array to Store The Points ********/
/** Initializing the array **/
double xDistance =0;
double yDistance =0;
double TDistance = 0;
static double **Array;
int column = 0; //used to toggle the column number
int width = 100;
int height = 100;
Array = new double *[width];
for (int i=0; i <width; i++)
{
Array [i] = new double [height];
}
/*** Now a and b are stored inside the Array[0][0] ***/
for (column =0; column <2; column ++)
{
if ((column % 2)==0)
{
Array [click][column] = a; //storing at [0,0]
}
else
{
Array [click][column] = b; //storing at [0,1]
}
}
for (int row = 2; row < click; row ++)
{
for (column = 0; column <2; column ++)
{
if ((column % 2) == 0)
{
xDistance = Array [0][column] - Array [row][column];
}
else
{
yDistance = Array [0][column] - Array [row][column];
}
}
TDistance = sqrt((xDistance * xDistance) + (yDistance * yDistance));
}
/*** Clearing up of array ***/
for (int i = 0; i < width; i++)
{
delete[] Array[i];
}
delete[] Array;
click++;
return TDistance ;
}
我意識到,當我注入第二組a和b值時,我的數組[0] [0]和[0] [1]中的值丟失,但我的第二組值存儲在[1] [0]和[1] [1]。任何想法如何我可以運行此腳本而不會丟失以前的值? 感謝提前加載。編輯代碼以清除一些查詢。
每次調用函數時,所有的自動變量初始化。除此之外,你並不是在''刪除'你的'數組',這將導致你的進程最終餓死。 –
分配時數組下標中的「click」是什麼? – arne
@ bash.d不僅自動變量被重新初始化,數組也會被覆蓋,所以難怪數值「消失」了。它們只是留在舊數組中,新數組當然是空的。 – arne