Dynamic memory

2011-10-11
2012-11-09
  • Andoni Crespo - 2011-10-11

    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!

     
  • singleton - 2011-10-11

    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
     
  • Andoni Crespo - 2011-10-11

    Thanks a lot!

     
  • Patmo1 - 2011-10-25

    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.

     
  • Andoni Crespo - 2011-11-07

    Thanks for your support!

     
  • Anonymous - 2012-11-09

    Originally created by: rickj

    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).

     

Log in to post a comment.