increment the number

54 views
Skip to first unread message

me you

unread,
Oct 9, 2022, 1:50:19 PM10/9/22
to The Ring Programming Language
Hi, I just want to support this C function in Ring:
When I call the function, the function does not increment the number

// c funktion
void p1(int *var)
{
    *var = *var + 1;
}

// ring test
loadlib("mylib.so")
num = 10
p1("num")
see num + nl   // out = 1

// he function does not increnetate the number 

Ilir

unread,
Oct 9, 2022, 3:16:38 PM10/9/22
to The Ring Programming Language
Hello,

we don't see Ring function generated from the configuration file. Generated function looks incorrect because "num" is expected to generate an error in function expecting data passed by the Ring pointer (since p1 expects int *).

There is no definite answer to your question without source code of the generated ring_p1 function.

me you

unread,
Oct 9, 2022, 4:38:18 PM10/9/22
to The Ring Programming Language
Hello illr,
thanks for your reply. here is the code which generated bei code generator.

RING_FUNC(ring_p1)
{
    if ( RING_API_PARACOUNT != 1 ) {
        RING_API_ERROR(RING_API_MISS1PARA);
        return ;
    }
    if ( ! RING_API_ISSTRING(1) ) {
        RING_API_ERROR(RING_API_BADPARATYPE);
        return ;
    }
    p1(RING_API_GETINTPOINTER(1));
    RING_API_ACCEPTINTVALUE(1) ;
}

RING_LIBINIT
{
    RING_API_REGISTER("p1",ring_p1);
}

Ilir

unread,
Oct 9, 2022, 4:54:25 PM10/9/22
to The Ring Programming Language
Hello,

yes, generated function is not correct, expecting string instead of the pointer. We also need entry from the configuration, I expect you use something like

void p1(char *var)

That is not correct, it should be

void p1(int *var)

and in Ring code you then use

spc = int2bytes(10) // allocates memory for the integer of value 10
ptr = varptr(:spc,"int") // makes integer pointer
p1("num")
see num 


Ilir

unread,
Oct 9, 2022, 4:57:44 PM10/9/22
to The Ring Programming Language
Hello,

sorry, you need to pass pointer, not "num" (just copy-pasted old code), and you need bytes2int conversion, so correct code is

spc = int2bytes(10) // allocates memory for the integer of value 10
ptr = varptr(:spc,"int") // makes integer pointer
p1(ptr)
see bytes2int(spc) 

me you

unread,
Oct 9, 2022, 7:20:55 PM10/9/22
to The Ring Programming Language
Hallo Illir,
thank for your help.
>>I expect you use something like   void p1(char *var)

no, I have the right one.  void p1(int *var)

it worked. you don't need a pointer. the code should look like this

spc = int2bytes(10)
p1("spc")
see bytes2int(spc) +nl

Ilir

unread,
Oct 9, 2022, 7:40:18 PM10/9/22
to The Ring Programming Language
Hello,

excellent, this is how we learn a new things every day.

Thank you for the update!

Greetings,
Ilir

Ilir

unread,
Oct 10, 2022, 7:17:54 AM10/10/22
to The Ring Programming Language
Hello,

after I revised "spc" case, I see two problems here

1. Using variable name prevents type check because pointer is taken from the variable not containing value type. That means implicit type conversion like in this case:

num = float2bytes(10)
? p1("num")
? bytes2int(num)// prints 1092616193 on Windows

This is a bad situation for Ring2C and I think I will disallow it in Ring2C+ (Ring2C will match whatever official Ring supports)

2. Access via variable name is slow. Requires current scope lookup (minor) and hashtable lookup (major) slowdown of access in comparison to direct pointer access and type check.

Reply all
Reply to author
Forward
0 new messages