<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en" xmlns="http://www.w3.org/2005/Atom"><title>Recent posts to FB_Init to define length of array in FB</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/49d65ccd84/" rel="alternate"/><link href="https://forge.codesys.com/forge/talk/Engineering/thread/49d65ccd84/feed.atom" rel="self"/><id>https://forge.codesys.com/forge/talk/Engineering/thread/49d65ccd84/</id><updated>2021-02-02T01:03:25.815000Z</updated><subtitle>Recent posts to FB_Init to define length of array in FB</subtitle><entry><title>FB_Init to define length of array in FB</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/49d65ccd84/?limit=25#56ed" rel="alternate"/><published>2021-02-02T01:03:25.815000Z</published><updated>2021-02-02T01:03:25.815000Z</updated><author><name>gabe-ctrl</name><uri>https://forge.codesys.com/u/gabe-ctrl/</uri></author><id>https://forge.codesys.com98b44a9da45849ad3f16ef0c4e016529b4b8b74f</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Thanks for clarifying. I did not realize they ran at different times.&lt;br/&gt;
I will look into the FB_Factory and see what I can do. Thank you.&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>FB_Init to define length of array in FB</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/49d65ccd84/?limit=25#53b5" rel="alternate"/><published>2021-02-01T22:38:22.558000Z</published><updated>2021-02-01T22:38:22.558000Z</updated><author><name>i-campbell</name><uri>https://forge.codesys.com/u/i-campbell/</uri></author><id>https://forge.codesys.com9ec780cfddee81d96e17d269aff7d847a58f0228</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;FB_Init happens when you boot up the application.&lt;br/&gt;
ARRAY &lt;span&gt;[0..ArrLen]&lt;/span&gt; OF INT; happens at compile time.&lt;br/&gt;
What you are looking at is allocating (and hopefully deallocating memory during runtime).&lt;br/&gt;
This is described in "B.)"here:&lt;br/&gt;
&lt;a href="https://help.codesys.com/webapp/fb_factory;product=LibDevSummary;version=3.5.15.0" rel="nofollow"&gt;https://help.codesys.com/webapp/fb_factory;product=LibDevSummary;version=3.5.15.0&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>FB_Init to define length of array in FB</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/49d65ccd84/?limit=25#f987" rel="alternate"/><published>2021-02-01T21:02:19.716000Z</published><updated>2021-02-01T21:02:19.716000Z</updated><author><name>gabe-ctrl</name><uri>https://forge.codesys.com/u/gabe-ctrl/</uri></author><id>https://forge.codesys.com21a17a5e21ed4ddd2c57d11e98c4924253ed4804</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Hi,is there a method to pass a number through FB_Init and use it to define the length of the array which is internal to the FB?&lt;/p&gt;
&lt;p&gt;Example&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="k"&gt;METHOD&lt;/span&gt; &lt;span class="k"&gt;PUBLIC&lt;/span&gt; &lt;span class="n"&gt;FB_Init&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;BOOL&lt;/span&gt;
&lt;span class="n"&gt;VAR_INPUT&lt;/span&gt;
    &lt;span class="n"&gt;bInitRetains&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;BOOL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;initializing&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="n"&gt;retain&lt;/span&gt; &lt;span class="k"&gt;variable&lt;/span&gt;
    &lt;span class="n"&gt;bInCopyCode&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;BOOL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="n"&gt;copied&lt;/span&gt; &lt;span class="k"&gt;to&lt;/span&gt; &lt;span class="k"&gt;copy&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt;
    &lt;span class="n"&gt;ArrLength&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;additional&lt;/span&gt; &lt;span class="k"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;number&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;COM&lt;/span&gt; &lt;span class="n"&gt;interface&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;that&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;to&lt;/span&gt; &lt;span class="n"&gt;be&lt;/span&gt; &lt;span class="n"&gt;observed&lt;/span&gt;
&lt;span class="n"&gt;END_VAR&lt;/span&gt;

&lt;span class="n"&gt;This&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ArrLen&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ArrLength&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Where ArrLen is used in the FB as shown, to define array length?&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="n"&gt;FUNCTION_BLOCK&lt;/span&gt; &lt;span class="n"&gt;FB_Example&lt;/span&gt;
&lt;span class="n"&gt;VAR&lt;/span&gt; &lt;span class="n"&gt;CONSTANT&lt;/span&gt;
    &lt;span class="n"&gt;ArrLen&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;END_VAR&lt;/span&gt;

&lt;span class="n"&gt;VAR&lt;/span&gt;
    &lt;span class="n"&gt;Array1&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;ARRAY&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;..&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;ArrLen&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;OF&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;END_VAR&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;I understand Codesys forces you to use a constant to define the Array length and I cannot write to a constant. So I am hoping for a workaround. The idea is to make the FB use the correct amount of memory for the array on instantiation.&lt;/p&gt;
&lt;p&gt;It would make sense that since the function block is instantiated with FB_Init I should be able to define the length of the array with the FB_Init. &lt;/p&gt;
&lt;p&gt;Thanks.&lt;/p&gt;&lt;/div&gt;</summary></entry></feed>