03-16-2009 01:42 PM
Does LabWindows CVI have a way to create a dynamic array? An array that has a variable amount of memory.
Solved! Go to Solution.
03-16-2009 03:25 PM - edited 03-16-2009 03:27 PM
calloc( ) can be used in CVI to dynamically create an array of desired size, either a standard datatype (int, double...) or a custom datatype like a struct.
The dynamically allocated memory must be freed with free( ) when no longer used.
double *array = NULL; // Define the array pointer
array = calloc (numElements, sizeof (double)); // ALlocate memory and inizialize al elements to 0
// .... use array elements
free (array); // Free dynamically allocated memory
03-16-2009 07:25 PM
Also, CVI 9.0 introduced support for variable length arrays (a C99 language feature), by which you can declare arrays that take a variable (or any other expression) as the size:
void foo(int x, int y)
{
double array[x][y]; // creates an array of x*y doubles on the stack.
...
}
The advantage of variable length arrays is that you don't have to remember to free them explicitly. They are cleaned up automatically when they go out of scope. The disadvantage is that few other C compilers support this feature, so if you want to be able to take your code to another compiler, like VC, you're better off not using them.
Mert A.
National Instruments
03-17-2009 07:37 AM
That worked out well. This will help me import test data and pick apart individual board test data and then I have to export it into excel. That is my next step. Below is what I have setup so far.
thanks,
results = SetDir ("C:\\xxx\\Testdata\\LOGDATA\\AA\\");
file_handle = OpenFile ("AK000001.dat",VAL_READ_WRITE, VAL_OPEN_AS_IS, VAL_ASCII);
file_present = GetFileInfo("C:\\xxx\\Testdata\\LOGDATA\\AA\\AK000001.dat",&file_size);
array = calloc (file_size, sizeof (char)); // ALlocate memory and inizialize al elements to 0
bytes_read = ReadFile (file_handle, array, file_size);
bytes_written = WriteFile (file_handle, array, file_size);
// .... use array elements
free (array); // Free dynamically allocated memory
Delay(1);
03-17-2009 09:25 AM
I'm happy that you find a solution here. I can only add two hints:
It is always a good thing to test for I/O errors while accessing physical files, so in your code I would put GetFileInfo before, next opening the file only if file is present; also, remember to test for function return values in I/O functions and memory allocation functions.
I don't understand why rewriting the entire contents to the file...