<div dir="ltr"><div>Zoltan,</div><div>for the record, from the SDL_Rect.h header file...</div><div><br></div><font face="courier new, monospace"><b>typedef struct SDL_Point<br>{<br> int x;<br> int y;<br>} SDL_Point;<br>typedef struct SDL_Rect<br>{<br> int x, y;<br> int w, h;<br>} SDL_Rect;</b></font><br><div><br></div><div>so they are not pointers but actual structures. If I wrote a funtion that allocated memory then I would have to de-allocate it as well ...a level of complexity I wouldn't have hoped not to have been presented with for just shuffling rectangles around. I had hoped that some kind of local stack based allocation would have taken place (no idea how the compiler works, can you tell?) e/g/ like RAII in C++ sort of thing.</div><div><br></div><div>However, I think I have enough information from you and Julien and Julian to be able to move forward in a reasonable manner.</div><div>Thank you all once again,</div><div>Sean.</div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, 14 Jul 2019 at 00:24, Zoltan Somogyi <<a href="mailto:zoltan.somogyi@runbox.com">zoltan.somogyi@runbox.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><br>
<br>
On Sat, 13 Jul 2019 23:53:28 +0100, emacstheviking <<a href="mailto:objitsu@gmail.com" target="_blank">objitsu@gmail.com</a>> wrote:<br>
> * % SDL_Rect:- type sdl_rect.:- pragma foreign_type("C", sdl_rect,<br>
> "SDL_Rect", [can_pass_as_mercury_type]).*<br>
<br>
If you have that definition in your code, but not ...<br>
<br>
> *:- type sdl_rect ---> sdl_rect(x::int, y::int, w::int, h::int).*<br>
<br>
... this definition, then only C code knows the structure of this type, and<br>
therefore only C code can create values of this type.<br>
<br>
> * sdl_renderfillrect(Rnd, sdl_rect(0, 50, 200, 100), !IO),*<br>
<br>
This means that if you want to be able to write code like that, you will<br>
need to define a Mercury function sdl_rect/4, whose implementation<br>
will be a foreign_proc that allocates memory for a structure and fills in its fields<br>
before returning a pointer to it. (I presume that SLD_Rect is a typedef for a<br>
pointer to a structure type.)<br>
<br>
In C, the allocation should be done with the MR_GC_NEW macro,<br>
which is defined in runtime/mercury_memory.h, or its MR_GC_NEW_ATTRIB<br>
variant, which help with memory profiling.<br>
<br>
Julien, Peter: I see that the reference manual is silent on how foreign code should<br>
allocate memory whose address is passed to Mercury code. Since you have more<br>
experience with interfacing to foreign language code than I do, could one of you<br>
please take a crack at adding a paragraph about this to the sections dealing<br>
with the interfaces to C, C#, Java and Erlang?<br>
<br>
Zoltan.<br>
</blockquote></div>