lucaver89 - 2020-03-22

Hi to all,

In my project I need to use a memory dynamic allocation, able to use an oriented object program. I have enabled the right option doing the following steps:

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

after that I am able to use the __NEW operator in this way:

VAR
    n_token          :UDINT;
    init        :BOOL :=FALSE;
END_VAR
IF(NOT init) THEN

    token := __NEW(STRING, 4);
    init := TRUE;

END_IF

In this way the code working fine, but doing this I got an exception in runtime mode

VAR
    n_token               :UDINT;
    init             :BOOL :=FALSE;
    length           :UDINT := 4;
END_VAR
IF(NOT init) THEN

    token := __NEW(STRING, length);
    init := TRUE;

END_IF

Now, I would like understand why in this case the code dosn't work giving me an exception error. Am I doing something of wrong?

Thank you
Luca