Code generator void arrayFunk( char *arr[ ] )

36 views
Skip to first unread message

Smar

unread,
Oct 22, 2022, 7:22:57 PM10/22/22
to The Ring Programming Language
hello ring team,

i support a c++ lib and in this lib come so many function that have such a function prototype:

void arrayFunk( char *arr[ ] )

Code generator does not support this prototype.

i tried to support it like this in ring.

my question is if there are other better ways that can work better.

i am also trying to clear the memory i have reserved.

// c funktion
void arrayFunk(const char *const arr[])
{
    int i = 0;
    while (arr[i] != NULL)
    {
        printf("... %s\n", arr[i]);
        i++;
    }
}

//+++++++++++++++++  my CF file

<register>
void arrayFunk(char *arr[ ] )
</register>

<code>


RING_FUNC(ring_arrayFunk)
{
    if (RING_API_PARACOUNT != 1)
    {
        RING_API_ERROR(RING_API_BADPARACOUNT);
        return;
    }

    if (!RING_API_ISLIST(1))
    {
        RING_API_ERROR(RING_API_BADPARATYPE);
        return;
    }

    List *pList = RING_API_GETLIST(1);
    int x;

    int list_size = ring_list_getsize(pList);

    char **str;

    str = malloc(list_size * sizeof(char *));

    for (x = 1; x <= ring_list_getsize(pList); x++)
    {
        if (ring_list_isstring(pList, x))
        {
            // printf("%s\n", ring_list_getstring(pList, x));
            str[x - 1] = ring_list_getstring(pList, x);
            //printf("%s\n", ring_list_getstring(pList, x));
        }
    }
    str[x] = NULL;
    arrayFunk(str);

    /*
        printf("____   %p\n", str);
        for (int i = 0; i < list_size; i++)
        {
            printf("%p\n", str[i]);
            free(str[i]);
        }
    */
}

</code>

//++++++++++++++++++++++++  test.ring

loadlib("build/Debug/test.dll")

ls =["hi","hallo","welt","marc","sluca"]

arrayFunk(ls)

my question is if there are other better ways that can work better.

i am also trying to clear the memory i have reserved. after calling c funktion but 

char **str;
str = malloc(list_size * sizeof(char *));


   arrayFunk(str);   // the code after hier dont work. how can i free memory

    /*
        printf("____   %p\n", str);
        for (int i = 0; i < list_size; i++)
        {
            printf("%p\n", str[i]);
            free(str[i]);
        }
    */


Ilir

unread,
Oct 23, 2022, 10:19:38 AM10/23/22
to The Ring Programming Language
Hello Shadi,

as you noted, code generator doesn't support  char *arr[ ]. Code generator supports char **arr (which is the same), but you can't use it because pointer to pointer generates pointer to pointer stored in internal Item structure of the Ring VM.

You free memory by simply calling free(str), you aren't allowed to free strings (str[i]) stored inside the list (which is referenced BTW)
Reply all
Reply to author
Forward
0 new messages