# Get length of dynamic variable in INTERFACE 4

**URL:** <https://techcommunity.softwareag.com/t/get-length-of-dynamic-variable-in-interface-4/188850>\
**Category:** Forum\
**Tags:** Adabas-Natural, Natural, Natural-Code-Samples\
**Created:** [June 7, 2016, 9:19am UTC](https://techcommunity.softwareag.com/t/get-length-of-dynamic-variable-in-interface-4/188850 "2016-06-07T09:19:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tom\_Engemann](https://avatars.discourse-cdn.com/v4/letter/t/8baadc/32.png) [@Tom\_Engemann](https://techcommunity.softwareag.com/u/Tom_Engemann)\
**Post date:** [June 7, 2016, 9:19am UTC](https://techcommunity.softwareag.com/t/get-length-of-dynamic-variable-in-interface-4/188850/1 "2016-06-07T09:19:54Z")

</div>

Hey,

i just tinkering around with the INTERFACE 4 C API. Everything works fine except dynamic variables in Arrays.  
When i define a array like this:

```auto

1 testarray (A/1:20, 1:30) DYNAMIC
```

i don’t get any length informations in the paramter\_description structure.  
The format, length, precision, byte\_length and length\_all entry are all set to zero.  
But now i dont’t know how much memory i must allocate to save the array. I have found the entrys dynp, and pops, wich are both void pointers, but they are just for internal use??

How can i get the max. length of the Array so i can allocate enough memory?

Regards,  
Tom

---

<div class="post-metadata">

**Author:** ![Steve\_Robinson](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/steve_robinson/32/107_2.png) [@Steve\_Robinson](https://techcommunity.softwareag.com/u/Steve_Robinson)\
**Post date:** [June 7, 2016, 12:12pm UTC](https://techcommunity.softwareag.com/t/get-length-of-dynamic-variable-in-interface-4/188850/2 "2016-06-07T12:12:35Z")

</div>

If you are going to allocate space based on the maximum length you might just be able to make the array non dynamic. Yes, there are other efficiencies in using dynamic variables, but perhaps the main reason is to not be concerned with maximum lengths and number of occurrences (X-arrays).

---

<div class="post-metadata">

**Author:** ![Tom\_Engemann](https://avatars.discourse-cdn.com/v4/letter/t/8baadc/32.png) [@Tom\_Engemann](https://techcommunity.softwareag.com/u/Tom_Engemann)\
**Post date:** [June 7, 2016, 1:34pm UTC](https://techcommunity.softwareag.com/t/get-length-of-dynamic-variable-in-interface-4/188850/3 "2016-06-07T13:34:24Z")

</div>

Yes of course it wouldn’t be really dynamic but my idea was that internaly all fields in a array with dynamic arrays have all the same length of allocated memory and they have an end char like ‘\0’ or something comparable.

So when i can’t get the full length of the array how should i know how much memory i have to allocate when i, for example, whant the pos. 1,1?

Even when i use the ncxr\_get\_parm\_array function with a particular index. The function whant’s a pointer to a allocated memory block.

---

<div class="post-metadata">

**Author:** ![Steve\_Robinson](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/steve_robinson/32/107_2.png) [@Steve\_Robinson](https://techcommunity.softwareag.com/u/Steve_Robinson)\
**Post date:** [June 7, 2016, 9:33pm UTC](https://techcommunity.softwareag.com/t/get-length-of-dynamic-variable-in-interface-4/188850/4 "2016-06-07T21:33:55Z")

</div>

I might well not understand what you are trying to do. However, if I have understood what you are trying to do, why not use \*length as shown below.

DEFINE DATA LOCAL  
1 #B (A/1:5,1:10) DYNAMIC  
END-DEFINE  
\*  
MOVE ‘qwert’ to #b (2,3)  
WRITE 'AFTER MOVE , LENGTH IS ’ \*LENGTH (#B (2,3))  
\*  
move ‘qwertasdfg’ to #b (2,3)  
WRITE 'AFTER second MOVE , LENGTH IS ’ \*LENGTH (#B (2,3))  
END  
Page 1 16-06-07 15:30:27

AFTER MOVE , LENGTH IS 5  
AFTER second MOVE , LENGTH IS 10

---

<div class="post-metadata">

**Author:** ![Tom\_Engemann](https://avatars.discourse-cdn.com/v4/letter/t/8baadc/32.png) [@Tom\_Engemann](https://techcommunity.softwareag.com/u/Tom_Engemann)\
**Post date:** [June 8, 2016, 9:49am UTC](https://techcommunity.softwareag.com/t/get-length-of-dynamic-variable-in-interface-4/188850/5 "2016-06-08T09:49:15Z")

</div>

On the Natural site it is clear but i think we have talk past each other.

I am working with the INTERFACE 4 C API.

Here a short example program:  
Natural site:

```auto

define data local
    1 returncode (B4)
    1 index (I2)
    1 index2 (I2)
    1 testarray (A/1:20/1:30) DYNAMIC
end-define

for index = 1 to 20
    for index2 = 1 to 30
         move "ffff" to testarray(index, index2)
    end-for
end-for

call INTERFACE4 "PRALL" testarray
compute returncode = ret("PRALL")
write returncode
end
```

C site:

```auto

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "natuser.h"

long read_vars(WORD nparm, void *parmhandle, void *traditional)
{
    void *value;
    FILE *fp;
    struct parameter_description var_info;

    if((fp = fopen("tmp/natvars", "w")) == NULL)
        return((long)1);

    if(ncxr_get_parm_info(0, parmhandle, &var_info) < 0)
    {
        fprintf(fp, "Error reading var infos\n");
        fclose(fp);
        return((long)2);
    }

    if((value = malloc(sizeof(char)*var_info.length_all)) == NULL)
    {
        fprintf(fp, "%s\n", strerror(errno));
        fprintf(fp)
        return((long)3);
    }

    if(ncxr_get_parm(0, parmhandle, var_info_length, value) < 0)
    {
        fprintf(fp, "Error reading var value\n");
        fclose(fp);
        return((long)4);
    }

    logHexDump(value, var_info.length_all, fp);
    fprintf(fp, "Format: [%c(%d)]\n", var_info.format, var_info.format);
    fprintf(fp, "Length VP: [%d]\n", var_info.length);
    fprintf(fp, "Length BY: [%d]\n", var_info.byte_length);
    fprintf(fp, "Length AP: [%d]\n", var_info.precision);
    fprintf(fp, "Length ALL: [%d]\n", var_info.length_all);
    fprintf(fp, "Dimensios: [%d]\n", var_info.dimensions);

    free(value);

    fclose(fp);
    return((long)0);
}
```

As i said previously the entrys length, precision, byte\_length and length\_all in the parameter\_description structure are all set to zero so i don’t know how much memory i have to allocate to copy the array data in my program.

---

<div class="post-metadata">

**Author:** ![Steve\_Robinson](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/steve_robinson/32/107_2.png) [@Steve\_Robinson](https://techcommunity.softwareag.com/u/Steve_Robinson)\
**Post date:** [June 8, 2016, 1:52pm UTC](https://techcommunity.softwareag.com/t/get-length-of-dynamic-variable-in-interface-4/188850/6 "2016-06-08T13:52:12Z")

</div>

> [@](#):
>
> On the Natural site it is clear but i think we have talk past each other.

You are correct. I really should try not to do two or three things at once.

I am totally unfamiliar with the interface. That said, in the CALL to the interface should you have testarray (_,_) or a similar construct rather than just testarray? Perhaps just one array member, testarray (1,1)?

---

<div class="post-metadata">

**Author:** ![Tom\_Engemann](https://avatars.discourse-cdn.com/v4/letter/t/8baadc/32.png) [@Tom\_Engemann](https://techcommunity.softwareag.com/u/Tom_Engemann)\
**Post date:** [June 8, 2016, 2:07pm UTC](https://techcommunity.softwareag.com/t/get-length-of-dynamic-variable-in-interface-4/188850/7 "2016-06-08T14:07:57Z")

</div>

> [@](#):
>
> That said, in the CALL to the interface should you have testarray (_,_) or a similar construct rather than just testarray? Perhaps just one array member, testarray (1,1)?

Yes you can do that when you want just one array entry.  
When you give it no index informations it will hand over the full array. The interface react like a “write work file” for example.

It work’s with a Array like this:

```auto
define data local
    1 returncode (B4)
    1 index (I2)
    1 index2 (I2)
    1 testarray (A20/1:5,1:5) /*I just rezised the array so you can see the HexDump better
end-define

for index = 1 to 5
    for index2 = 1 to 5
         compress "ffff " index " " index2 
            into testarray(index, index2)
    end-for
end-for

call INTERFACE4 "PRALL" testarray
compute returncode = ret("PRALL")
write returncode
end
```

Then i get the full size of the array (in this case 500Bytes ). I attached a screenshot from the logfile so you can see what i mean.

 ![Unbenannt.PNG](https://global.discourse-cdn.com/techcommunity/original/2X/2/2760a1246d07516afe99af9cabd37abdbcc83c52.png)

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/techcommunity/original/3X/c/d/cd19967bb605d28a72d77eb83a1cae22f3df2eb8.svg) [@system](https://techcommunity.softwareag.com/u/system)\
**Post date:** [April 2, 2021, 3:36pm UTC](https://techcommunity.softwareag.com/t/get-length-of-dynamic-variable-in-interface-4/188850/8 "2021-04-02T15:36:11Z")

</div>


