From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

'malloc' for return a vector

Greetings! I am a freshmen for LabWindows/LabVIEW. Thank you for any response in advance.

 

Recently, I need to develop a VI using C code, so I am trying to use LabWindows to develop a .DLL file. My function looks like:

int32_t myFunc(double output[], int32_t input_len, double input[], int32_t interleaver[])

{

      .....

         if ((output=(double *)malloc(length_total*sizeof(double)))==NULL)
         {
                 printf("\n fail to allocate memory of tempout \n");
                 exit(1); 
         }

    .........

}

as you can see, a vector called 'output' is required for return. For convenience, I allocate space by using 'malloc' here without 'free' it, of course.

 

The 'output' port is then connected to the next VI.

 

My question is whether this function will lead to memory leak if I schedule this VI in a loop structure?  

0 Kudos
Message 1 of 3
(2,631 Views)

Hi Jaden,

 

Are you passing the output array as reference? Right now the function only returns an int. 

To answer your question, if you do call this function in your VI in a loop, it will lead to a memory leak. 

 

I hope this helps!

T. Le
Vision Product Support Engineer
National Instruments
0 Kudos
Message 2 of 3
(2,585 Views)

Hi thuyanhl,

 

Thank you so much for the response. I found the original code didn't return the ouput[] as a 1D-array by using this method. Thus I finally initialized output[] in the VI and passed it to the C code, then malloc was not required any longer. 

 

Thank you again for your help.

0 Kudos
Message 3 of 3
(2,552 Views)