calling statically linked c functions in pb (by value and by reference)

Just starting out? Need help? Post your questions and find answers here.
superadnim
Enthusiast
Enthusiast
Posts: 480
Joined: Thu Jul 27, 2006 4:06 am

calling statically linked c functions in pb (by value and by reference)

Post by superadnim »

so ive been coding a library wrapper that requires certain functions to be converted from "by value" to "by reference"
i know this isn't strictly a PB question but could someone show me the way as to how one would go about converting the functions?

my interpretation is that I must use * before the variable name and then I must access the members by using -> is this correct? and also is that all of it? anyone who has written such a wrapper could please kindly offer an example?

maybe there is a guide out there but i couldn't find it. i tried wrapping the way i described but i am still getting several MAVs at strange offsets.

just as a side comment... im trying but my eyes are not what they used to be and my patience is nowhere to be found these days so thank you all in advance for your patience!

right now only a small portion of the code works and i have a lot of memory related issues which makes me think maybe i am accessing things wrong (i am NOT a C programmer!)

i know it takes time and dedication to help someone you dont know over a forum so i really appreciate it. thanks!

:lol: should I bash the keyboard and give up?
:?
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: calling statically linked c functions in pb (by value and by reference)

Post by idle »

we would need to see the code in question.
yes when you see * in a function parameter its a pointer and yes you dereference a pointer in c with ->
superadnim
Enthusiast
Enthusiast
Posts: 480
Joined: Thu Jul 27, 2006 4:06 am

Re: calling statically linked c functions in pb (by value and by reference)

Post by superadnim »

oh sorry!
here is an example, first pseudo code of a "get" function that returns by value from what i understand:

Code: Select all

dkVector
dkHandlerGetSomeValue(const dkArbiter *arb)
{
	return dkVectorMultiply(arb->n, arb->swapped ? -1.0f : 1.0);
}
so in PB one would have to pass a second argument namely a pointer of the neccesary structure so the result could be stored in that portion of the memory instead of obtaining a result "by value" which we cannot use directly in PB from what I gather.

Code: Select all

void
dkHandlerGetSomeValue_pb(const dkArbiter* arb, dkVector *result)
{
	dkVector temporary_result = dkVectorMultiply(arb->n, arb->swapped ? -1.0f : 1.0);
	result->x = temporary_result.x;
	result->y = temporary_result.y;
}
in the original "get" function the library would return a by-value of the dkVector and in ours we copy the variables to the "result" dkVector we passed from PB as the destination memory.

however im not sure this is the way to do it i need to read more on C and pointers specially because with the "set" functions when i check the results they are wrong as if im passing an address instead of the contents

:lol: should I bash the keyboard and give up?
:?
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: calling statically linked c functions in pb (by value and by reference)

Post by idle »

what you have looks like the right idea but what is the header. I can't really help unless I can debug.
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: calling statically linked c functions in pb (by value and by reference)

Post by mk-soft »

How does a C function return a struct as a retval anyway?
As a pointer or on the stack.

dkVector temporary_result = dkVectorMultiply(...)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: calling statically linked c functions in pb (by value and by reference)

Post by idle »

mk-soft wrote: Wed Jan 25, 2023 8:18 pm How does a C function return a struct as a retval anyway?
As a pointer or on the stack.

dkVector temporary_result = dkVectorMultiply(...)
I think in c it's generally acceptable to returns structures or pass structs by value up to the size of quads but that's just for portability I don't think there's a limit just stack space. Why a quad it will fit on x86 registers eax edx so nothing special needs to be done.

we could do this but its not really practical

Code: Select all

Structure vec2 
  x.f
  y.f
EndStructure 

Procedure.q newVec(x.f,y.f) 
  Protected vec.vec2 , ret.q   
  vec\x = x 
  vec\y = y 
  PokeQ(@ret,PeekQ(@vec)) 
  ProcedureReturn ret 
EndProcedure 
 
q.q = NewVec(2.3,3.3)
*v.vec2 = @q : ;returns vector as a quad contains two floats   

Debug *v\x 
Debug *v\y 

User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: calling statically linked c functions in pb (by value and by reference)

Post by mk-soft »

Who releases the memory again ... ?

C-backend

Code: Select all

!struct emp {
!    int id;
!    int num;
!    char *name;
!};
!
!struct emp get() {
!    char *name = "John";
!
!    struct emp e1 = {100, 200, name};
!
!    return (e1);
!}

Structure emp Align #PB_Structure_AlignC
  id.l
  num.l
  *name
EndStructure

Global *r1.emp

!struct emp e2 = get();
!p_r1 = &e2;

;Debug *r1
Debug *r1\id
Debug *r1\num
name.s = PeekS(*r1\name, -1, #PB_Ascii)
Debug name
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: calling statically linked c functions in pb (by value and by reference)

Post by mk-soft »

P.S.
I thinks its static or stack ...

Code: Select all

!struct emp {
!    int id;
!    int num;
!    char *name;
!};
!
!struct emp get() {
!    char *name = "John";
!
!    struct emp e1 = {100, 200, name};
!
!    return (e1);
!}

Structure emp Align #PB_Structure_AlignC
  id.l
  num.l
  *name
EndStructure

Global *r1.emp, *r2.emp

For i = 1 To 10
  !struct emp e2 = get();
  !p_r1 = &e2;
  !struct emp e3 = get();
  !p_r2 = &e3;
  
  Debug *r1
  Debug *r2
  Debug *r1\id
  Debug *r1\num
  Debug "Name r1 " + @*r1\name
  Debug "Name r2 " + @*r2\name
  name.s = PeekS(*r1\name, -1, #PB_Ascii)
  Debug name
Next
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: calling statically linked c functions in pb (by value and by reference)

Post by idle »

It works exactly the same as passing in and returning normal types, it's on the stack. You get a copy of the structure which will probably consist of a bunch of movs or a memcopy
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: calling statically linked c functions in pb (by value and by reference)

Post by mk-soft »

In any case, this leads to a memory leak if you try to receive structures from c-functions as retval in PB. The memory management for releasing the structure data is missing.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
superadnim
Enthusiast
Enthusiast
Posts: 480
Joined: Thu Jul 27, 2006 4:06 am

Re: calling statically linked c functions in pb (by value and by reference)

Post by superadnim »

that sounds about right unless you could somehow use the same memory stack?
but im no expert im just thinking out loud!

so i am still getting memory access violations on PB and i find it very hard to debug because the compiled library in C cannot be debugged while I run the PB program (or can it?)

if I compile as debug PB claims there are missing functions related to the debugging process and any way both debuggers are very different so that would not work anyway

in the case of simple variables such as a float should I also do it through a pointer or then it doesnt matter? i asked about the structures because they often use a lot more memory than a single variable but now i wonder about the rest!

:lol: should I bash the keyboard and give up?
:?
superadnim
Enthusiast
Enthusiast
Posts: 480
Joined: Thu Jul 27, 2006 4:06 am

Re: calling statically linked c functions in pb (by value and by reference)

Post by superadnim »

maybe this is a stupid question but the memory allocated with cpcalloc can be accessed by pb without issues as long as i free the memory in the c code later on?

cpalloc is not initialized data (not all 0s) but is it all compatible or do i have to somehow pass a pointer to pb's allocatememory and use it through c?

:lol: should I bash the keyboard and give up?
:?
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: calling statically linked c functions in pb (by value and by reference)

Post by idle »

mk-soft wrote: Thu Jan 26, 2023 8:18 pm In any case, this leads to a memory leak if you try to receive structures from c-functions as retval in PB. The memory management for releasing the structure data is missing.
no leak

Code: Select all

Structure emp Align #PB_Structure_AlignC
   id.l
   num.l
   *name
EndStructure

!s_emp get(int id,int num,const char *name) {
!    s_emp e1 = {id ,num, name};
!    return (e1);
!}

Global i.l, r1.emp, r2.emp

For i = 1 To 10
  !s_emp v_r1 = get(v_i-1,v_i-1,"bob");
  !s_emp v_r2 = get(v_i,v_i,"john");
    
  Debug r1\id
  Debug r1\num
  
  Debug r2\id 
  Debug r2\num 
    
  name.s = PeekS(r1\name, -1, #PB_Ascii)
  Debug name
  name.s = PeekS(r2\name, -1, #PB_Ascii)
  Debug name
Next


Post Reply