Greetings,
I need to pass arrays to a LabView DLL that will (eventually) perform math routines on data. For now I want to understant the principle so I wrote a simple VI (result = i + i). The memory allocation seems alright but it crashes when I call the function (Error #3, "memory.cpp" line 563). Here's a chunk of code for the on-click event on the Get button :
/* here's the declaration of TD1 - TD1Hdl in the header file generated by LabView :
typedef struct {
int32 dimSize;
int32 Numeric[1];
} TD1;
typedef TD1 **TD1Hdl;
void __stdcall MatLine(TD1Hdl *Array);
*/
// When the Get button is pressed, we create a [1x4] dynamic array, fill it,
// send it to a LV DLL, then display the results (result = i + i)
//---------------------------------------------------------------------------
void __fastcall TFORM_Matrice1::CMD_GetClick(TObject *Sender)
{
const int NB_ELEMENTS = 4; // specify the length
// Allocates memory space for a struct of type TD1 (length + value[length]),
// make a reference to it (typecast it as a pointer of TD1)
TD1 * ArrayPtr = (TD1 *) malloc(sizeof(int32) + NB_ELEMENTS*sizeof(int32));
if (ArrayPtr == NULL)
ShowMessage("Memory allocation - failure");
else
{
TD1Hdl ArrayHdl = &ArrayPtr; // Associates the handle to the ptr address
(*ArrayPtr).dimSize = NB_ELEMENTS; // Fill the dynamic array
(*ArrayPtr).Numeric[0] = 1;
(*ArrayPtr).Numeric[1] = 2;
(*ArrayPtr).Numeric[2] = 4;
(*ArrayPtr).Numeric[3] = 8;
// Calls the function, passes address of the handle :
// returns a memory error #3 (line 563)
MatLine(&ArrayHdl); // crashes at run-time
// display the results
LBL_A11->Caption = IntToStr((*ArrayPtr).Numeric[0]);
LBL_A12->Caption = IntToStr((*ArrayPtr).Numeric[1]);
LBL_A13->Caption = IntToStr((*ArrayPtr).Numeric[2]);
LBL_A14->Caption = IntToStr((*ArrayPtr).Numeric[3]);
free(ArrayPtr);
}
}
I need to pass arrays to a LabView DLL that will (eventually) perform math routines on data. For now I want to understant the principle so I wrote a simple VI (result = i + i). The memory allocation seems alright but it crashes when I call the function (Error #3, "memory.cpp" line 563). Here's a chunk of code for the on-click event on the Get button :
/* here's the declaration of TD1 - TD1Hdl in the header file generated by LabView :
typedef struct {
int32 dimSize;
int32 Numeric[1];
} TD1;
typedef TD1 **TD1Hdl;
void __stdcall MatLine(TD1Hdl *Array);
*/
// When the Get button is pressed, we create a [1x4] dynamic array, fill it,
// send it to a LV DLL, then display the results (result = i + i)
//---------------------------------------------------------------------------
void __fastcall TFORM_Matrice1::CMD_GetClick(TObject *Sender)
{
const int NB_ELEMENTS = 4; // specify the length
// Allocates memory space for a struct of type TD1 (length + value[length]),
// make a reference to it (typecast it as a pointer of TD1)
TD1 * ArrayPtr = (TD1 *) malloc(sizeof(int32) + NB_ELEMENTS*sizeof(int32));
if (ArrayPtr == NULL)
ShowMessage("Memory allocation - failure");
else
{
TD1Hdl ArrayHdl = &ArrayPtr; // Associates the handle to the ptr address
(*ArrayPtr).dimSize = NB_ELEMENTS; // Fill the dynamic array
(*ArrayPtr).Numeric[0] = 1;
(*ArrayPtr).Numeric[1] = 2;
(*ArrayPtr).Numeric[2] = 4;
(*ArrayPtr).Numeric[3] = 8;
// Calls the function, passes address of the handle :
// returns a memory error #3 (line 563)
MatLine(&ArrayHdl); // crashes at run-time
// display the results
LBL_A11->Caption = IntToStr((*ArrayPtr).Numeric[0]);
LBL_A12->Caption = IntToStr((*ArrayPtr).Numeric[1]);
LBL_A13->Caption = IntToStr((*ArrayPtr).Numeric[2]);
LBL_A14->Caption = IntToStr((*ArrayPtr).Numeric[3]);
free(ArrayPtr);
}
}