Get length of dynamic variable in INTERFACE 4

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:


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

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

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.

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

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:


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:


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

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

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:

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.