1. C array pointers

hi,

is probably stupid, but i'll ask.

i like to copy a sequence of integers '{182735, 475644, ...}' to an allocated atom to hand it over to a c function such as func(int *integerarray). is there an easy way, elegant, fast way to do that?

richard

new topic     » topic index » view message » categorize

2. Re: C array pointers

Maybe something like that ?

sequence s = {182735, 475644, ...} 
a = allocate(length(s)) 
poke(a, s) 
object res = c_func(myFunc, {a}) 
free(a) 

Jean-Marc

new topic     » goto parent     » topic index » view message » categorize

3. Re: C array pointers

no, that doesn't work, it gives bytes. i fell for this too. richard

new topic     » goto parent     » topic index » view message » categorize

4. Re: C array pointers

Try this?

sequence s = {182735, 475644, ...} 
a = allocate(length(s)*4) 
poke4(a, s) 
object res = c_func(myFunc, {a}) 
free(a) 
new topic     » goto parent     » topic index » view message » categorize

5. Re: C array pointers

yes that works, but what threw me was, that the the 64bit integer is also only 4 bytes long

public procedure drawpoly(sequence polypointegers) 
  integer len = 0, j=0 
  ifdef BITS32 then 
    len = 4 
  elsedef 
    len = 4  --8 64bit ????????? 
  end ifdef 
  integer plen = length(polypointegers) 
  atom poly = allocate(len*plen, true)  
  for i=1 to plen do 
    pokeN(poly+j, polypointegers[i], len) 
    j += len 
  end for 
  c_proc(_drawpoly,{plen, poly})  
end procedure 

but maybe there is even a better way then the one above.

thanks richard

new topic     » goto parent     » topic index » view message » categorize

6. Re: C array pointers

begin said...

yes that works, but what threw me was, that the the 64bit integer is also only 4 bytes long

No, it is 8 bytes long.

poke4() will only poke 4 bytes of those 8 however, so that might be the problem. Unless your integers on 64bit are soo large that they don't fit into a 32bit int, you'd never notice this problem...

You need to use poke8() to poke a 64bit integer. You say pokeN() here but don't share the implementation so I'm not sure if that's the problem or if it is something else.

new topic     » goto parent     » topic index » view message » categorize

7. Re: C array pointers

correct i just checked it - but i change len to 8 for 64 bit it still doesn't work

  ifdef BITS32 then 
    len = 4 
  elsedef 
    len = 4--8 
  end ifdef 

on the c end, i get 64 bit and atom poly = allocate(len*plen, true) in int array for ex. 100,0,100,0 instead if 100,100.

have no clue yet - or i don't see the forest because of all the trees.

richard

new topic     » goto parent     » topic index » view message » categorize

8. Re: C array pointers

could that be a byte order thing?

new topic     » goto parent     » topic index » view message » categorize

9. Re: C array pointers

jimcbrown said...

No, it is 8 bytes long.

Not so, it varies, see https://en.wikipedia.org/wiki/64-bit_computing#64-bit_data_models

jimcbrown said...

You say pokeN() here but don't share the implementation so I'm not sure if that's the problem or if it is something else.

pokeN() is a Phix builtin

Pete

new topic     » goto parent     » topic index » view message » categorize

10. Re: C array pointers

following feed running 64bit phix: sequence poly = {100,100,200,300,100,150,200,100,400,100,200,300,380,150,100,100

goes into function:

public procedure drawpoly(sequence polypointegers) 
  integer len = 0, j=0 
  ifdef BITS32 then 
    len = 4 
  elsedef 
    len = 8    
  end ifdef 
  integer plen = length(polypointegers) 
  atom poly = allocate(len*plen, true)  
  for i=1 to plen do 
    ifdef BITS32 then 
      poke4(poly+j, polypointegers[i]) 
    elsedef 
      poke8(poly+j, polypointegers[i]) 
    end ifdef 
    j += len 
  end for 
  c_proc(_drawpoly,{plen, poly})  
end procedure 

c function recieves compiled 64bit:

void  bgi_drawpoly(int numpoints, const int *polypoints) 
{ 
  CHECK_GRAPHCS_INITED 
  FILE *x = fopen("test.txt", "wx"); 
  POINT *points = (POINT *) malloc(numpoints * sizeof(POINT)); 
	for(int i = 0; i < numpoints/2; i++) 
	{ 
		points[i].x = *(polypoints++); 
		points[i].y = *(polypoints++); 
    fprintf(x, "points[i].x %d points[i].y %d\n",points[i].x, points[i].y);  
	} 
  fclose(x); 
	BEGIN_DRAW 
	    Polyline(activeDC, points, numpoints/2); 
	END_DRAW 
	free(points); 
} 

points[i].x 100 points[i].y 0 
points[i].x 100 points[i].y 0 
points[i].x 200 points[i].y 0 
points[i].x 300 points[i].y 0 
points[i].x 100 points[i].y 0 
points[i].x 150 points[i].y 0 
points[i].x 200 points[i].y 0 
points[i].x 100 points[i].y 0 

what do i do wrong?? 32 bit is no problem

richard

ps soccer - what a bummer

new topic     » goto parent     » topic index » view message » categorize

11. Re: C array pointers

petelomax said...

pokeN() is a Phix builtin

Pete

D'oh. Of course. That makes more sense.

I was thinking of OE, where our C backend is LP64 and we're effectively ILP64 (as we don't have separate int/long types). Phix might be different of course.

new topic     » goto parent     » topic index » view message » categorize

12. Re: C array pointers

begin said...

what do i do wrong??

what happens if you just do this (on 64 bit [and on 32 bit])?

public procedure drawpoly(sequence polypointegers) 
  integer plen = length(polypointegers) 
  atom poly = allocate(4*plen, true)  
  poke4(poly, polypointegers) 
  c_proc(_drawpoly,{plen, poly})  
end procedure 

Pete

new topic     » goto parent     » topic index » view message » categorize

13. Re: C array pointers

jimcbrown said...

I was thinking of OE, where our C backend is LP64 and we're effectively ILP64 (as we don't have separate int/long types). Phix might be different of course.

Oh, yeah, on 64-bit Phix, like OE, integers are 64-bit; I was talking about calling a C function in a dll/so that accepts an int* parameter - that varies.

Pete

new topic     » goto parent     » topic index » view message » categorize

14. Re: C array pointers

petelomax said...
begin said...

what do i do wrong??

what happens if you just do this (on 64 bit [and on 32 bit])?

public procedure drawpoly(sequence polypointegers) 
  integer plen = length(polypointegers) 
  atom poly = allocate(4*plen, true)  
  poke4(poly, polypointegers) 
  c_proc(_drawpoly,{plen, poly})  
end procedure 

Pete

doesn't work in 64 bit

points[i].x 100 points[i].y 0 
points[i].x 100 points[i].y 0 
points[i].x 200 points[i].y 0 
points[i].x 300 points[i].y 0 
points[i].x 100 points[i].y 0 
points[i].x 150 points[i].y 0 
points[i].x 200 points[i].y 0 
points[i].x 100 points[i].y 0 

32bit:

points[i].x 100 points[i].y 100 
points[i].x 200 points[i].y 300 
points[i].x 100 points[i].y 150 
points[i].x 200 points[i].y 100 
points[i].x 400 points[i].y 100 
points[i].x 200 points[i].y 300 
points[i].x 380 points[i].y 150 
points[i].x 100 points[i].y 100 

with poke8 on 64bit doesn't work:

points[i].x 100 points[i].y 0 
points[i].x 100 points[i].y 0 
points[i].x 200 points[i].y 0 
points[i].x 300 points[i].y 0 
points[i].x 100 points[i].y 0 
points[i].x 150 points[i].y 0 
points[i].x 200 points[i].y 0 
points[i].x 100 points[i].y 0 

new topic     » goto parent     » topic index » view message » categorize

15. Re: C array pointers

petelomax said...
begin said...

what do i do wrong??

what happens if you just do this (on 64 bit [and on 32 bit])?

public procedure drawpoly(sequence polypointegers) 
  integer plen = length(polypointegers) 
  atom poly = allocate(4*plen, true)  
  poke4(poly, polypointegers) 
  c_proc(_drawpoly,{plen, poly})  
end procedure 

Pete

doesn't work in 64 bit

points[i].x 100 points[i].y 0 
points[i].x 100 points[i].y 0 
points[i].x 200 points[i].y 0 
points[i].x 300 points[i].y 0 
points[i].x 100 points[i].y 0 
points[i].x 150 points[i].y 0 
points[i].x 200 points[i].y 0 
points[i].x 100 points[i].y 0 

32bit:

points[i].x 100 points[i].y 100 
points[i].x 200 points[i].y 300 
points[i].x 100 points[i].y 150 
points[i].x 200 points[i].y 100 
points[i].x 400 points[i].y 100 
points[i].x 200 points[i].y 300 
points[i].x 380 points[i].y 150 
points[i].x 100 points[i].y 100 

with poke8 on 64bit doesn't work:

points[i].x 100 points[i].y 0 
points[i].x 100 points[i].y 0 
points[i].x 200 points[i].y 0 
points[i].x 300 points[i].y 0 
points[i].x 100 points[i].y 0 
points[i].x 150 points[i].y 0 
points[i].x 200 points[i].y 0 
points[i].x 100 points[i].y 0 

new topic     » goto parent     » topic index » view message » categorize

16. Re: C array pointers

begin said...

doesn't work in 64 bit

points[i].x 100 points[i].y 0 
points[i].x 100 points[i].y 0 

That's plain weird, at this point I might be tempted to try poke2()...

begin said...

with poke8 on 64bit doesn't work:

points[i].x 100 points[i].y 0 
points[i].x 100 points[i].y 0 

But that's just too bizarre - poke8() and poke4() give exactly the same results?!
At this point I am tempted to suggest you are editing one file but running another...
Or maybe reading the wrong version\location of your test.txt output file?
I might also suggest putting a ?peek({poly,4|8*plen}) in drawpoly() after the poke4|8().

Pete

new topic     » goto parent     » topic index » view message » categorize

17. Re: C array pointers

done

sequence poly = {100,100,200,300,100,150,200,100,400,100,200,300,380,150,100,100} 
drawpoly(poly) 

32bit 
?peek({poly,4*plen}) 
{100,0,0,0,100,0,0,0,200,0,0,0,44,1,0,0,100,0,0,0,150,0,0,0,200,0,0,0,100,0,0,0,144,1,0,0,100,0,0,0,200,0,0,0,44,1,0,0,124,1,0,0,150,0,0,0,100,0,0,0,100,0,0,0} 
 
64bit 
poke8 
?peek({poly,8*plen}) 
{100,0,0,0,0,0,0,0,100,0,0,0,0,0,0,0,200,0,0,0,0,0,0,0,44,1,0,0,0,0,0,0,100,0,0,0,0,0,0,0,150,0,0,0,0,0,0,0,200,0,0,0,0,0,0,0,100,0,0,0,0,0,0,0} 
 
64bit 
poke4 
?peek({poly,8*plen}) 
{100,0,0,0,100,0,0,0,200,0,0,0,44,1,0,0,100,0,0,0,150,0,0,0,200,0,0,0,100,0,0,0,144,1,0,0,100,0,0,0,200,0,0,0,44,1,0,0,124,1,0,0,150,0,0,0,100,0,0,0,100,0,0,0} 
 
poke4 
?peek({poly,4*plen}) 
{100,0,0,0,100,0,0,0,200,0,0,0,44,1,0,0,100,0,0,0,150,0,0,0,200,0,0,0,100,0,0,0,144,1,0,0,100,0,0,0,200,0,0,0,44,1,0,0,124,1,0,0,150,0,0,0,100,0,0,0,100,0,0,0} 

richard

new topic     » goto parent     » topic index » view message » categorize

18. Re: C array pointers

If you upload the 32 and 64 bit dlls I will have a play

new topic     » goto parent     » topic index » view message » categorize

19. Re: C array pointers

craps, i did something wrong.

i tried to attach and uploaded

bgi.zip Δ ... 87,174 bytes ... July 12, 2018, at 12:35 PM

should be there? what to do?

new topic     » goto parent     » topic index » view message » categorize

20. Re: C array pointers

I found that bgi.zip.

I couldn't find anything wrong!

This works perfectly, for me, on both 32 and 64 bit (draws something that vaguely resembles a v-shaped-diamond-stone):

atom bgiDll = 0 
integer _drawpoly, _openbgi 
 
procedure init() 
ifdef BITS32 then 
  bgiDll = open_dll("bgi32.dll") 
elsedef 
  bgiDll = open_dll("bgi64.dll") 
end ifdef 
 
    _openbgi = define_c_proc(bgiDll,"bgi_openbgi",{C_INT, C_INT,C_INT, C_INT, C_POINTER}) 
    c_proc(_openbgi,{800, 800, -1,-1, "test"}) 
 
    _drawpoly = define_c_proc(bgiDll,"bgi_drawpoly",{C_INT, C_POINTER})  
end procedure 
init() 
 
sequence poly = {100,100,200,300,100,150,200,100,400,100,200,300,380,150,100,100} 
 
integer len = 4 
integer plen = length(poly) 
atom pPoly = allocate(len*plen, true)  
poke4(pPoly,poly) 
c_proc(_drawpoly,{plen, pPoly})  
sleep(5) 

Pete

PS I cut it down to something small enough to run through OllyDbg/FDBG, and can confirm that both the 32 and 64 bit dlls are indeed loading 32-bit values.

new topic     » goto parent     » topic index » view message » categorize

21. Re: C array pointers

pete - thank you so very much, that explains my problems. an i can deal with this.

richard

new topic     » goto parent     » topic index » view message » categorize

22. Re: C array pointers

Your ints are only four bytes long even on 64 bit. So when the Euphoria code has poke8 and the C routine tries to load the y coordinate from the array, it gets the top half of the 8 byte word. After, poke8 this would be 0.

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu