About Monkey 2 › Forums › Monkey 2 Programming Help › const pointers
This topic contains 5 replies, has 2 voices, and was last updated by
codifies
2 years, 5 months ago.
-
AuthorPosts
-
November 12, 2016 at 2:13 pm #4975
in a module I have something like this
Monkey123Struct constFloat="const float"Function someFunction:constFloat ptr()which I use like this
Monkey1234Local mypointer:Float Ptr = Cast<Float Ptr>(someFunction())print mypointer[4] ' its a const pointer to an array of floatsI gotta be doing this wrong ! it seems a bit fugly!!!
If I try to use a const pointer like a normal pointer I see g++ errors (off the top of my head about casting pointers)
November 12, 2016 at 4:10 pm #4976Defining extern with ‘const something’ is very limited. It can lead to some mess if you don’t use for functions arguments only. Because the transpiler will just copy the defined name and ‘const something’ is more than just the name on The C side. Mark has manualy made some glue code for these situation. For example for the chipmunk debugdraw drawpoly callback function that have a ‘const ptr’ as argument.
I had also a similar problem with pure virtual methods for the box2d wrap but I chose to remove the const in the cpp code because box2d is solid enough not to need the const barrier imo. So if you may remove ‘const’ from the C code, it’s probably the fastest way.
It’s one of the difficulties with c to mx2. The only one I encoutered for now.November 13, 2016 at 1:05 am #4982I *really* don’t want to modify the C++ code its a large code base and will require periodic updating
I am aware of what a const pointer is on the C side of things having used them in previous C projects, just a shame that MX2 doesn’t have some mechanism to handle them (or rather more gracefully) I might have misunderstood your reply but did you get that I am only using constFloat specifically where I need to handle a const pointer and no where else?
glue code could slow things down, an extra call per call when there could be the need to retrieve several hundred const pointers per drawn frame doesn’t seem ideal either…
Do you see any issues with my solution (given that yes it is a tad ungraceful) ?
November 13, 2016 at 7:58 pm #4988lol – now this server isn’t taking 30 seconds to serve a page, I managed to use the search…
seems I’m not the only one to “discover” the
Struct constFloat = “const float”
hack!
November 14, 2016 at 5:18 pm #5026Here some working code (with an int). But you probably already get it.. or may be this is not relevant at all but I wanted to play with the ‘cast’ solution for const ptr. And this is nothing official, so could be deprecated? Never saw it in the docs..
.monkey2
[/crayon]Monkey12345678910111213141516171819[crayon-5cba90a195c38095812623 inline="true" ]#Import "<std>"#Import "facto_c.h"ExternStruct c_int="const int"EndFunction Factorial:c_int Ptr (M:int)PublicFunction Main()Local ci:c_int ptrLocal ip:Int Ptrci=Factorial(4) 'should be 24..ip = Cast<int Ptr>(ci)Print ip[0]Endfacto_c.h:
[/crayon]Monkey123456789101112[crayon-5cba90a195c42942053325 inline="true" ]const int* Factorial(int M){int factorial=1;int* returner;for(int i=1; i<=M; i++){factorial = factorial*i;}returner=&factorial;return returner; //' returning directly &factorial was not working, it had to be assigned to a 'named' int pointer}November 14, 2016 at 5:33 pm #5027yeah – thanks, that is basically what I’m doing and checking the generated code afaict its doing what I’d have done using C
I assume you realise you don’t need the ci pointer, ie
Monkey1234Local ip:Int Ptrip = Cast<int Ptr>(Factorial(4))Print ip[0]looking at a different example
Monkey1Local pos:float ptr = Cast<float ptr>(dGeomGetPosition(geom)) '// redraw sphere at new locationgets you
Monkey1bbFloat* l_pos=((bbFloat*)(dGeomGetPosition(g_default_geom)));Which looks sane to me!
-
AuthorPosts
You must be logged in to reply to this topic.