LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

dynamic array allocation labwindows CVI

Solved!
Go to solution

Does LabWindows CVI have a way to create a dynamic array? An array that has a variable amount of memory.

 

 

0 Kudos
Message 1 of 5
(5,426 Views)
Solution
Accepted by topic author chinook1

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

Message Edited by Roberto Bozzolo on 03-16-2009 09:27 PM


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 2 of 5
(5,422 Views)

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

0 Kudos
Message 3 of 5
(5,411 Views)

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);

0 Kudos
Message 4 of 5
(5,379 Views)

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...



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 5 of 5
(5,370 Views)