CODESYS - the IEC 61131-3 automation software
https://forum.codesys.com/

Dynamic memory
https://forum.codesys.com/viewtopic.php?f=11&t=2066
Page 1 of 1

Author:  Andoni Crespo [ Tue Oct 11, 2011 11:30 am ]
Post subject:  Dynamic memory

Good morning,

I would like to create a dynamic FIFO. I am using __NEW and __DELETE but when I am compiling the application, I always get this error:

typify code ...
[ERROR] (Line 2, Column 1 (Impl)): No memory for dynamic object creation defined for application
[ERROR] (Line 2, Column 1 (Impl)): A Functionblock or Structure needs the attribute '{attribute'enable_dynamic_creation'}' to be created with __NEW

I am trying to paste this atribute but I do not know where exactly {atribute 'enable_dynamic_creation'}.

Can anybody help me?

Thanks in advance!

Author:  singleton [ Tue Oct 11, 2011 3:25 pm ]
Post subject:  Re: Dynamic memory

Hi,
you have to enable the usage of dynamic memory allocation.

- Right click on the application
- properties
- Dynamic memory settings
- Enable "Use dynamic memory allocation" and set the maximum size of memory

Author:  Andoni Crespo [ Tue Oct 11, 2011 3:49 pm ]
Post subject:  Re: Dynamic memory

Thanks a lot!

Author:  Patmo1 [ Tue Oct 25, 2011 9:07 am ]
Post subject:  Re: Dynamic memory

I've tried using __NEW and it works.

As I have to set the maximum size of dynamically allocated memory (e.g. 1000 bytes), what is the real benefit of using dynamical allocation (__NEW) ?

In case of a FIFO you have the maximum memory size as a limit (e.g. 1000 bytes). So there's no advantage compared to an static array with 1000 bytes.

Can anyone tell me an example where the dynamic memory has an advantage over static variables?

I thought about handling different data types, but it didn't work because you have to set the data type in the declaration (e.g. pNumber : Pointer to XY).
In case of your FIFO I don't think the dynamic memory has an advantage over an static array.
I guess you've realised the FIFO as an array like in the V3 help (see __NEW entry of help file).

Solution 1:
You create a big "array" and you always stay within its bounds. -> Same behaviour like static array.

Solution 2:
You create an "array" with only one element. As the bounds of an "array" in the dynamic memory are only fictive, you never change the size of this "array", but use more than one element (e.g. pArray[9] := z). -> Very dangerous to use memory you have not explicitly allocated (could work if you use only this array and nothing else within the dynamic memory?).

Solution 3:
Everytime a new element is put into the FIFO or taken out of it, you create a new "array" with size -1/+1, copy all elements and delete the old one. -> Much work and copying is necessary which is probably slower than a static array. And as you have the maximum dynamic memory size as maximum bound, you could also use a static array of the same size.

PS: I have set "array" in quotes because it is declared as a pointer but used like an array with fictive bounds.

Author:  Andoni Crespo [ Mon Nov 07, 2011 3:48 pm ]
Post subject:  Re: Dynamic memory

Thanks for your support!

Author:  rickj [ Fri Nov 09, 2012 4:13 pm ]
Post subject:  Re: Dynamic memory

When is it required to de-allocate memory using _DELETE? Is there any documentation or discussion about doing proper cleanup. I am particularly interested about restart, online code changes, etc and correct usage of FB_exit, FB_reinit, and FB_init. Tanks

@Patmo1: Not all instances require the same list length (same array size). If the array is dynamically allocated then it can be appropriately sized for each instance. You would just have an init method or a size property to do the sizing (i.g. Fifo.Init(size) or Fifo.Size := size).

Page 1 of 1 All times are UTC+01:00
Powered by phpBB® Forum Software © phpBB Limited
https://www.phpbb.com/