<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en" xmlns="http://www.w3.org/2005/Atom"><title>Recent posts to Variable length arrays/ Dynamic Memory Allocation</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/82e8a18b3c/" rel="alternate"/><link href="https://forge.codesys.com/forge/talk/Engineering/thread/82e8a18b3c/feed.atom" rel="self"/><id>https://forge.codesys.com/forge/talk/Engineering/thread/82e8a18b3c/</id><updated>2021-01-07T08:51:01.600000Z</updated><subtitle>Recent posts to Variable length arrays/ Dynamic Memory Allocation</subtitle><entry><title>Variable length arrays/ Dynamic Memory Allocation</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/82e8a18b3c/?limit=100#9295" rel="alternate"/><published>2021-01-07T08:51:01.600000Z</published><updated>2021-01-07T08:51:01.600000Z</updated><author><name>dFx</name><uri>https://forge.codesys.com/u/dfx/</uri></author><id>https://forge.codesys.com518d740f34de4f23e89df14fb5f9f6b8d2c79ccb</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Then just use an external length variable.&lt;/p&gt;
&lt;p&gt;For instance, your array max length is 10.&lt;br/&gt;
If you only fill it with 5 items, then reset lefts items (5..9) and set your length variable to 5.&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Variable length arrays/ Dynamic Memory Allocation</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/82e8a18b3c/?limit=25#e397" rel="alternate"/><published>2020-12-28T12:21:56.279000Z</published><updated>2020-12-28T12:21:56.279000Z</updated><author><name>jeetshah</name><uri>https://forge.codesys.com/u/jeetshah/</uri></author><id>https://forge.codesys.com94bad6d8f31acdde49f4c275149c419971b99b8f</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Hello dFx,&lt;/p&gt;
&lt;p&gt;Firstly, in the actual data i may get null values also. I need to find some other key value maybe.&lt;/p&gt;
&lt;p&gt;I didnt understand you properly. How can i handle my array of DUT??&lt;/p&gt;
&lt;p&gt;Could you please elaborate a little?&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Variable length arrays/ Dynamic Memory Allocation</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/82e8a18b3c/?limit=100#eb56" rel="alternate"/><published>2020-12-26T17:34:00.108000Z</published><updated>2020-12-26T17:34:00.108000Z</updated><author><name>dFx</name><uri>https://forge.codesys.com/u/dfx/</uri></author><id>https://forge.codesys.comfbdb732bc4684ac05c1eb8bb71d3796ad19eaf30</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Common approach may be to size your array to the maximum size (if size is not a problem, ie on small arrays), then to use a key value to detect array end (ie nullbyte 16#00).&lt;br/&gt;
This way you can fill your dut array with values and optionnaly indicate the end of dut array.&lt;br/&gt;
This approach is valid only with small arrays, as reserved memory may be an issue.&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Variable length arrays/ Dynamic Memory Allocation</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/82e8a18b3c/?limit=25#c4d8" rel="alternate"/><published>2020-12-24T16:04:59.476000Z</published><updated>2020-12-24T16:04:59.476000Z</updated><author><name>i-campbell</name><uri>https://forge.codesys.com/u/i-campbell/</uri></author><id>https://forge.codesys.com9ce58ef10f0fdfb42b0b01435fdc46b77a2168f6</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;You might need dynamic Function Blocks, Option B.) from:&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;p&gt;The examples are in the standard CODESYS Library Template.&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Variable length arrays/ Dynamic Memory Allocation</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/82e8a18b3c/?limit=25#7053" rel="alternate"/><published>2020-12-24T12:57:11.237000Z</published><updated>2020-12-24T12:57:11.237000Z</updated><author><name>jeetshah</name><uri>https://forge.codesys.com/u/jeetshah/</uri></author><id>https://forge.codesys.com194d8e696d7df425cb76f74d64653ffe5d8bf0a5</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Hello h-hermsen&lt;/p&gt;
&lt;p&gt;Thank you for the reply.&lt;/p&gt;
&lt;p&gt;However i made a small mistake while posting the question.&lt;/p&gt;
&lt;p&gt;1) VAR_IN_OUT is not permissible in DUT objects. I can somehow make it fix as it wont be greater than 64bytes.&lt;br/&gt;
2) Secondly, i dont want to use VAR_IN_OUT as it is mandatory for the user to pass a fixed length array as input parameter, which is not possible for the user as he doesnt have that information.&lt;/p&gt;
&lt;p&gt;I hope now i make it more clear....&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Variable length arrays/ Dynamic Memory Allocation</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/82e8a18b3c/?limit=250#6911" rel="alternate"/><published>2020-12-24T11:30:40.048000Z</published><updated>2020-12-24T11:30:40.048000Z</updated><author><name>h-hermsen</name><uri>https://forge.codesys.com/u/hermsen/</uri></author><id>https://forge.codesys.com71f197efca980b4e81de8e5658f30cbbc2d9868d</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Size isn't needed, it's implicitly given by the ARRAY via the Lowerbound and Upperbound function call.&lt;/p&gt;
&lt;p&gt;// DUT&lt;br/&gt;
FOR DUTIndex := LOWER_BOUND(structure ,1) TO UPPER_BOUND(structure ,1) DO&lt;br/&gt;
    // structure&lt;span&gt;[DUTIndex]&lt;/span&gt;&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="nv"&gt;abyReadBuffer&lt;/span&gt;
&lt;span class="k"&gt;FOR&lt;/span&gt; &lt;span class="nv"&gt;BufferIndex&lt;/span&gt; :&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;LOWER_BOUND&lt;/span&gt;&lt;span class="ss"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;structure&lt;/span&gt;[&lt;span class="nv"&gt;DUTIndex&lt;/span&gt;].&lt;span class="nv"&gt;abyReadBuffer&lt;/span&gt; ,&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="ss"&gt;)&lt;/span&gt; &lt;span class="nv"&gt;TO&lt;/span&gt; &lt;span class="nv"&gt;UPPER_BOUND&lt;/span&gt;&lt;span class="ss"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;structure&lt;/span&gt;[&lt;span class="nv"&gt;DUTIndex&lt;/span&gt;].&lt;span class="nv"&gt;abyReadBuffer&lt;/span&gt; ,&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="ss"&gt;)&lt;/span&gt; &lt;span class="k"&gt;DO&lt;/span&gt;
    &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="nv"&gt;structure&lt;/span&gt;[&lt;span class="nv"&gt;DUTIndex&lt;/span&gt;].&lt;span class="nv"&gt;abyReadBuffer&lt;/span&gt;[&lt;span class="nv"&gt;BufferIndex&lt;/span&gt;]
    &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="k"&gt;Do&lt;/span&gt; &lt;span class="nv"&gt;stuff&lt;/span&gt; &lt;span class="nv"&gt;with&lt;/span&gt; &lt;span class="nv"&gt;structure&lt;/span&gt;[&lt;span class="nv"&gt;DUTIndex&lt;/span&gt;].&lt;span class="nv"&gt;abyReadBuffer&lt;/span&gt;[&lt;span class="nv"&gt;BufferIndex&lt;/span&gt;] &lt;span class="nv"&gt;here&lt;/span&gt;

&lt;span class="nv"&gt;END_FOR&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;END_FOR&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Variable length arrays/ Dynamic Memory Allocation</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/82e8a18b3c/?limit=25#a889" rel="alternate"/><published>2020-12-24T11:11:50.505000Z</published><updated>2020-12-24T11:11:50.505000Z</updated><author><name>jeetshah</name><uri>https://forge.codesys.com/u/jeetshah/</uri></author><id>https://forge.codesys.come7103d65c7d498bdcf308af633c71d2b41fae4a1</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;I am library developer &amp;amp; new to CodeSys OOPs programming. I have a problem where it is required to define variable size arrays &amp;amp; varaible size structure(DUT) objects. As i will get the information of size at runtime only.&lt;/p&gt;
&lt;p&gt;I want to create something like below. &lt;/p&gt;
&lt;p&gt;I know the below code doesnt work, but i am just trying to explain what i want to do.&lt;/p&gt;
&lt;p&gt;TYPE DUT :&lt;br/&gt;
STRUCT&lt;br/&gt;
    abyReadBuffer : ARRAY&lt;span&gt;[*]&lt;/span&gt; OF BYTE;// I dont know the size of this array. It could be maximum of 64bytes but i                                             get this information at runtime only.&lt;br/&gt;
    usiVar0 : USINT;&lt;br/&gt;
    uiVar1  : UINT;&lt;br/&gt;
END_STRUCT&lt;br/&gt;
END_TYPE&lt;/p&gt;
&lt;p&gt;FUNCTION or FUNCTIONBLOCK (in library)&lt;br/&gt;
VAR &lt;br/&gt;
    structure : ARRAY&lt;span&gt;[*]&lt;/span&gt; OF DUT;// I dont know the size of this array. I get this information at runtime only.&lt;br/&gt;
END_VAR&lt;/p&gt;
&lt;p&gt;Also the problem with above approach is i dont get the SIZE information from user, but from a status word of another FB. So i dont want any input parameters which the user needs to pass regarding size. Also the size parameter is going to change in probably every cycle/instance.&lt;/p&gt;
&lt;p&gt;Do i need to use Dynamic Memory Allocation for above?? If yes could anyone show me some example of it??&lt;/p&gt;&lt;/div&gt;</summary></entry></feed>