Chapter 1 LabWindows/CVI Compiler
©
National Instruments Corporation 1-15 LabWindows/CVI Programmer Reference Manual
Dynamic Memory
LabWindows/CVI provides run-time error checking for pointers and arrays in dynamically
allocated memory.
You can use the ANSI C library functions malloc or calloc to allocate dynamic memory.
These functions return void * values that you must cast to some other type before the
memory can be used. During program execution, LabWindows/CVI uses the first such cast
on the return value of each call to these functions to determine the type of the object that will
be stored in the dynamic memory. Subsequent casts to different types can disable checking on
the dynamic data, as explained in the Pointer Casting discussion in this section.
You can use the realloc function to resize dynamically allocated memory. This function
increases or decreases the size of the object associated with the dynamic memory.
LabWindows/CVI adjusts the user protection information accordingly.

Avoid Unassigned Dynamic Allocation in Function Parameters

The LabWindows/CVI run-time error checking mechanism dynamically allocates data to
keep track of pointers that you dynamically allocate in your program. When you no longer
use the pointers, LabWindows/CVI uses garbage collection to deallocate its corresponding
dynamic memory.
A case exists where the garbage collection fails to retrieve all the memory it allocated. This
occurs when you pass the return value of one function to another function, the return value is
a pointer to dynamically allocated memory, and you do not assign the pointer to a variable in
the argument expression. The following is an example:
MyFunc (1, 2, malloc(7));
This call passes the return value from malloc to MyFunc but does not assign it to a variable.
If you make this call repeatedly in your program with run-time checking enabled, you lose a
small amount of memory each time.
Change the code as follows to avoid this problem.
void *p;
MyFunc (1, 2, p = malloc(7));
The following code also works and uses better programming style.
void *p;
p = malloc(7);
MyFunc (1, 2, p);

00ProRef.book : 06chap01.fm Page 15 Monday, March 9, 1998 3:23 PM