<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en" xmlns="http://www.w3.org/2005/Atom"><title>Recent posts to dereferencing a pointer in a function block</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/372dcbc63d/" rel="alternate"/><link href="https://forge.codesys.com/forge/talk/Engineering/thread/372dcbc63d/feed.atom" rel="self"/><id>https://forge.codesys.com/forge/talk/Engineering/thread/372dcbc63d/</id><updated>2020-02-27T14:00:30.411000Z</updated><subtitle>Recent posts to dereferencing a pointer in a function block</subtitle><entry><title>dereferencing a pointer in a function block</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/372dcbc63d/?limit=25#9c8f" rel="alternate"/><published>2020-02-27T14:00:30.411000Z</published><updated>2020-02-27T14:00:30.411000Z</updated><author><name>tvm</name><uri>https://forge.codesys.com/u/tvm/</uri></author><id>https://forge.codesys.com2bb2abf73f9be808de146e653dc12b843b35b346</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Thank you for the replies.  Yes, it was an alignment problem.  If I get a pointer to BYTE, it can only be dereferenced as a WORD type if the address is divisible by 2, and as a DWORD type if the address is divisible by 4.  So depending on where the pointer landed, it would work or not work.&lt;br/&gt;
Tim&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>dereferencing a pointer in a function block</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/372dcbc63d/?limit=25#3ab9" rel="alternate"/><published>2020-02-27T14:00:30.165000Z</published><updated>2020-02-27T14:00:30.165000Z</updated><author><name>josepmariarams</name><uri>https://forge.codesys.com/u/josepmariarams/</uri></author><id>https://forge.codesys.com1dbf6c2ec28a7da0c234d25b4c4a4d3c95366462</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;You are right Yannick&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>dereferencing a pointer in a function block</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/372dcbc63d/?limit=25#dc02" rel="alternate"/><published>2020-02-27T14:00:29.915000Z</published><updated>2020-02-27T14:00:29.915000Z</updated><author><name>yannickasselin1</name><uri>https://forge.codesys.com/u/yannickasselin1/</uri></author><id>https://forge.codesys.comecb3f8fec8430881283fcbc8dc318e57bd734c45</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Instead of using a POINTER TO ARRAY&lt;span&gt;[1..255]&lt;/span&gt; OF BYTE;  just use a pointer to BYTE and pass it the array.&lt;br/&gt;
You will still be able to iterate through the array in your FB.&lt;/p&gt;
&lt;p&gt;This is what I do in TwinCAT and it works fine.&lt;br/&gt;
I tried using variable arrays but found some bugs. See this post:   l &lt;a class="" href="https://forum.codesys.com/viewtopic.php?t=7558&amp;amp;p=17267&amp;amp;lan=en" rel="nofollow"&gt;viewtopic.php?t=7558#p17267&lt;/a&gt; l &lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>dereferencing a pointer in a function block</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/372dcbc63d/?limit=25#2281" rel="alternate"/><published>2020-02-27T14:00:29.656000Z</published><updated>2020-02-27T14:00:29.656000Z</updated><author><name>josepmariarams</name><uri>https://forge.codesys.com/u/josepmariarams/</uri></author><id>https://forge.codesys.com523b6c7a8f9adbf79d455692aa8dd6e755ed8cbe</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Hi.&lt;/p&gt;
&lt;p&gt;It could be an alignment trouble.&lt;/p&gt;
&lt;p&gt;Try to create an array of 64 reals. Point to him an pointer to array of 254 bytes.&lt;/p&gt;
&lt;p&gt;Buffer: array &lt;span&gt;[0..63]&lt;/span&gt; of real.&lt;/p&gt;
&lt;p&gt;Bytes:pointer to array&lt;span&gt;[0..255]&lt;/span&gt; of byte....&lt;/p&gt;
&lt;p&gt;Bytes:=adr(buffer).&lt;/p&gt;
&lt;p&gt;Then the array of bytes will be aligned as an real.&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>dereferencing a pointer in a function block</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/372dcbc63d/?limit=25#4890" rel="alternate"/><published>2020-02-27T14:00:28.853000Z</published><updated>2020-02-27T14:00:28.853000Z</updated><author><name>tvm</name><uri>https://forge.codesys.com/u/tvm/</uri></author><id>https://forge.codesys.com45a344ff98df797314e432fb574e567f5379624d</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;I'm having some issues with dereferencing a pointer in a function block.  I've tried to simplify the issue down to the following code:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="nt"&gt;VAR&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nt"&gt;VarPlaceholder&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nt"&gt;ARRAY&lt;/span&gt;&lt;span class="cp"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="nx"&gt;..255&lt;/span&gt;&lt;span class="cp"&gt;]&lt;/span&gt; &lt;span class="nt"&gt;OF&lt;/span&gt; &lt;span class="nt"&gt;BYTE&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nt"&gt;ptrPlaceholder&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nt"&gt;POINTER&lt;/span&gt; &lt;span class="nt"&gt;TO&lt;/span&gt; &lt;span class="nt"&gt;ARRAY&lt;/span&gt;&lt;span class="cp"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="nx"&gt;..255&lt;/span&gt;&lt;span class="cp"&gt;]&lt;/span&gt; &lt;span class="nt"&gt;OF&lt;/span&gt; &lt;span class="nt"&gt;BYTE&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nt"&gt;VarFloatingPoint&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nt"&gt;REAL&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nt"&gt;ptrFloatingPoint&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nt"&gt;POINTER&lt;/span&gt; &lt;span class="nt"&gt;TO&lt;/span&gt; &lt;span class="nt"&gt;REAL&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nt"&gt;END_VAR&lt;/span&gt;
&lt;span class="nt"&gt;ptrPlaceholder&lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nt"&gt;ADR&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;VarPlaceholder&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="nt"&gt;get&lt;/span&gt; &lt;span class="nt"&gt;a&lt;/span&gt; &lt;span class="nt"&gt;pointer&lt;/span&gt; &lt;span class="nt"&gt;to&lt;/span&gt; &lt;span class="nt"&gt;a&lt;/span&gt; &lt;span class="nt"&gt;variable&lt;/span&gt; &lt;span class="nt"&gt;of&lt;/span&gt; &lt;span class="nt"&gt;type&lt;/span&gt; &lt;span class="nt"&gt;ARRAY&lt;/span&gt;&lt;span class="cp"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="nx"&gt;..255&lt;/span&gt;&lt;span class="cp"&gt;]&lt;/span&gt; &lt;span class="nt"&gt;OF&lt;/span&gt; &lt;span class="nt"&gt;BYTE&lt;/span&gt;
&lt;span class="nt"&gt;ptrFloatingPoint&lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nt"&gt;ptrPlaceholder&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="nt"&gt;pass&lt;/span&gt; &lt;span class="nt"&gt;the&lt;/span&gt; &lt;span class="nt"&gt;pointer&lt;/span&gt; &lt;span class="nt"&gt;to&lt;/span&gt; &lt;span class="nt"&gt;a&lt;/span&gt; &lt;span class="nt"&gt;different&lt;/span&gt; &lt;span class="nt"&gt;type&lt;/span&gt; &lt;span class="nt"&gt;of&lt;/span&gt; &lt;span class="nt"&gt;pointer&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nt"&gt;POINTER&lt;/span&gt; &lt;span class="nt"&gt;TO&lt;/span&gt; &lt;span class="nt"&gt;REAL&lt;/span&gt;
&lt;span class="nt"&gt;VarFloatingPoint&lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nt"&gt;ptrFloatingPoint&lt;/span&gt;&lt;span class="o"&gt;^;&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="nt"&gt;dereference&lt;/span&gt; &lt;span class="nt"&gt;the&lt;/span&gt; &lt;span class="nt"&gt;pointer&lt;/span&gt; &lt;span class="nt"&gt;as&lt;/span&gt; &lt;span class="nt"&gt;a&lt;/span&gt; &lt;span class="nt"&gt;REAL&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This code works as expected if I put it in a test program.  If I change the values of the array, I get different REAL numbers in VarFloatingPoint.&lt;/p&gt;
&lt;p&gt;However, if I put the exact same code in a function block and call it from the same test program, I will get an access violation exception on the third line, where it dereferences the pointer. Line 1 does get a valid pointer, and Line 2 does copy that pointer over to ptrFloatingPoint (see attached image).  &lt;/p&gt;
&lt;p&gt;Is there something different about the way a pointer is handled in a function block?&lt;/p&gt;
&lt;p&gt;&lt;img alt="IMG: pointer exception.PNG" rel="nofollow" src="https://forum.codesys.com//download/file.php?id=3538"/&gt;&lt;/p&gt;&lt;/div&gt;</summary></entry></feed>