1. C array pointers
- Posted by begin Jul 11, 2018
- 1896 views
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
2. Re: C array pointers
- Posted by jmduro Jul 11, 2018
- 1878 views
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
3. Re: C array pointers
- Posted by begin Jul 11, 2018
- 1883 views
no, that doesn't work, it gives bytes. i fell for this too. richard
4. Re: C array pointers
- Posted by jimcbrown (admin) Jul 11, 2018
- 1854 views
Try this?
sequence s = {182735, 475644, ...} a = allocate(length(s)*4) poke4(a, s) object res = c_func(myFunc, {a}) free(a)
5. Re: C array pointers
- Posted by begin Jul 11, 2018
- 1851 views
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
6. Re: C array pointers
- Posted by jimcbrown (admin) Jul 11, 2018
- 1877 views
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.
7. Re: C array pointers
- Posted by begin Jul 11, 2018
- 1859 views
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
9. Re: C array pointers
- Posted by petelomax Jul 11, 2018
- 1877 views
No, it is 8 bytes long.
Not so, it varies, see https://en.wikipedia.org/wiki/64-bit_computing#64-bit_data_models
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
10. Re: C array pointers
- Posted by begin Jul 11, 2018
- 1837 views
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
11. Re: C array pointers
- Posted by jimcbrown (admin) Jul 11, 2018
- 1848 views
pokeN() is a Phix builtin
Pete
D'oh. Of course. That makes more sense.
Not so, it varies, see https://en.wikipedia.org/wiki/64-bit_computing#64-bit_data_models
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.
12. Re: C array pointers
- Posted by petelomax Jul 11, 2018
- 1973 views
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
13. Re: C array pointers
- Posted by petelomax Jul 11, 2018
- 1808 views
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
14. Re: C array pointers
- Posted by begin Jul 12, 2018
- 1802 views
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
15. Re: C array pointers
- Posted by begin Jul 12, 2018
- 1828 views
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
16. Re: C array pointers
- Posted by petelomax Jul 12, 2018
- 1793 views
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()...
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
17. Re: C array pointers
- Posted by begin Jul 12, 2018
- 1779 views
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
18. Re: C array pointers
- Posted by petelomax Jul 12, 2018
- 1786 views
If you upload the 32 and 64 bit dlls I will have a play
19. Re: C array pointers
- Posted by begin Jul 12, 2018
- 1772 views
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?
20. Re: C array pointers
- Posted by petelomax Jul 12, 2018
- 1755 views
- Last edited Jul 18, 2018
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.
21. Re: C array pointers
- Posted by begin Jul 12, 2018
- 1780 views
pete - thank you so very much, that explains my problems. an i can deal with this.
richard
22. Re: C array pointers
- Posted by SDPringle Jul 18, 2018
- 1509 views
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.