Talk:Use another language to call a function: Difference between revisions

From Rosetta Code
Content added Content deleted
(A better name for this task)
Line 58: Line 58:


::clarified-review? I made the change and added that template. I hope I did it right! —[[User:Sonia|Sonia]] 07:48, 13 February 2011 (UTC)
::clarified-review? I made the change and added that template. I hope I did it right! —[[User:Sonia|Sonia]] 07:48, 13 February 2011 (UTC)

This might be better named as "Demonstrate how a provided function can be called from a foreign language". --[[User:Markhobley|Markhobley]] 14:57, 5 June 2011 (UTC)

I don't know why we chose that particular function for the task description. Couldn't we have just done something that produces a result from a couple of numbers? The example in the task description requires an unnecessary amount of work to demonstrate something that could in essence be much simpler. --[[User:Markhobley|Markhobley]] 14:57, 5 June 2011 (UTC)

Revision as of 14:57, 5 June 2011

I am unsure if this is same as C FFI, or not. If yes, then merge. --Dmitry-kazakov 13:57, 11 August 2009 (UTC)

It's not. Call foreign language function and C FFI are the same task in essence though (and there are other closely related ones too; this is an area needing some rationalization). —Donal Fellows 11:46, 17 August 2009 (UTC)

Task needs work/splitting?

While writing the Tcl implementation, it occurred to me that a number of languages might want to deal with the cases where a parameter is an ‘in’ parameter differently from the case given (really a single ‘out’ parameter, plus a bit of metadata to describe the buffer size). No time to work on this now though. —Donal Fellows 13:10, 18 August 2009 (UTC)

C code is wrong

<lang c>#include <stdio.h>

extern int Query (char * Data, size_t * Length);

int main (int argc, char * argv []) {

  char     Buffer [1024];
  unsigned Size = sizeof (Buffer);
  
  if (0 == Query (Buffer, &Size))
  {
     printf ("failed to call Query\n");
  }
  else
  {
     char * Ptr = Buffer;
     while (Size-- > 0) putchar (*Ptr++);
     putchar ('\n');
  }

}</lang>

<lang c>#include <stdio.h>

int Query (char * Data, size_t * Length) {

  printf("Length = %zu, 0x%zx\n", *Length, *Length);
  return 0;

}</lang>

$ cc -v
Reading specs from /usr/lib/gcc-lib/amd64-unknown-openbsd4.8/4.2.1/specs
Target: amd64-unknown-openbsd4.8
Configured with: OpenBSD/amd64 system compiler
Thread model: posix
gcc version 4.2.1 20070719 
$ cc -o main main.c query.c
main.c: In function 'main':
main.c:10: warning: passing argument 2 of 'Query' from incompatible pointer type
$ ./main 
Length = 7971459302400, 0x74000000400
failed to call Query

It should say 'Length = 1024', not 'Length = 7971459302400'. The problem is that main() passed an unsigned * but Query() expects size_t *. On my machine, unsigned is 4 bytes but size_t is 8 bytes.

I would like to fix the C code (by changing unsigned Size to size_t Size, and while there, by adding to main() some return statements), but I worry that if I fix the C code, then I will destroy all the examples that use the old C code. --Kernigh 15:45, 12 February 2011 (UTC)

I'd say fix it. Go saw that as a problem too. —Sonia 23:35, 12 February 2011 (UTC)
+1 on fix. There is a template to warn others to review their entries too, but I can't remember what it is ... ... nope, still can't remember. --Paddy3118 06:02, 13 February 2011 (UTC)
clarified-review? I made the change and added that template. I hope I did it right! —Sonia 07:48, 13 February 2011 (UTC)

This might be better named as "Demonstrate how a provided function can be called from a foreign language". --Markhobley 14:57, 5 June 2011 (UTC)

I don't know why we chose that particular function for the task description. Couldn't we have just done something that produces a result from a couple of numbers? The example in the task description requires an unnecessary amount of work to demonstrate something that could in essence be much simpler. --Markhobley 14:57, 5 June 2011 (UTC)