3

i want to get an int and string from C function. int - error code, string - the data.

Tried that way:

package main
/*
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

int test(char *out){
  snprintf(out, sizeof out, "1234567890abcd");
  return 0;
}

*/
import "C"

import (
  "fmt"
  "unsafe"
)

func main() {
  foo := C.CString("")
  defer C.free(unsafe.Pointer(foo))
  C.test(foo)
  fmt.Println("Output is: "+C.GoString(foo))
}

and it almost worked.

Output is: 1234567

but should be: 1234567890abcd

I think that size of C.String passed to C function is 8. But don't know how can I extend it (fixed size <> 8) or change into dynamic.

So the question is: How should I pass that variable to C?

3 Answers3

7

You are calling the function sizeof on a pointer. The size of a pointer is 8 bytes on your computer so you are getting back a string of length 8.

I don't know all the details of how exactly you should be managing your pointer size, since i'm not that good at C but this example illustrates how to solve your immediate issue.

package main
/*
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

int test(char *out){
  snprintf(out, 16, "1234567890abcd");
  return 0;
}

*/
import "C"

import (
    "fmt"
    "unsafe"
)

func main() {
    foo := C.CString("")
    defer C.free(unsafe.Pointer(foo))
    C.test(foo)
    fmt.Println("Output is: "+C.GoString(foo))
}
Benjamin Kadish
  • 1,472
  • 11
  • 20
  • Unfortunately it is still not working as expected. When You change snprintf(out, 16, "1234567890abcd"); into something longer than 26 chars, for example: snprintf(out, 30, "123456789012345678901234567"); program crashes: *** Error in `/tmp/go-build755328986/command-line-arguments/_obj/exe/strings': free(): invalid next size (fast): 0x0000000001f99010 *** ======= Backtrace: ========= /lib64/libc.so.6(+0x7d053)[0x7f3fa64f2053] /tmp/go-build755328986/command-line-arguments/_obj/exe/strings[0x436925] ======= Memory map: ======== ... – Marcin Szydelski Jul 12 '16 at 09:01
4

After a research i found a solution:

package main


/*
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

int test(char *out, int len){
  snprintf(out, len, "1234567890123456789012345671`iknvcap9nv1-93hbf1-39b");
  return 0;

}

*/
import "C"

import (
  "fmt"
  "unsafe"
)

func main() {
  buf := make([]byte, 8192)
  C.test((*C.char)(unsafe.Pointer(&buf[0])), C.int(len(buf)))
  fmt.Println(string(buf))
}

To receive 'string' from C function, i had to create a slice:

  buf := make([]byte, 8192)

and pass a pointer to it:

  C.test((*C.char)(unsafe.Pointer(&buf[0])), C.int(len(buf)))

Using C.String in that situation is not working.

-1

For anyone coming across this in the future, C.CString does actually work, you just can't use sizeof with a pointer as this is always 8 chars. I did it the following way:

package main

/*
#include <stdlib.h>
#include <string.h>

int test(char* out){
  strcpy(out, "1234567890123456789012345671`iknvcap9nv1-93hbf1-39b");
  return 0;
}

*/
import "C"

import (
    "fmt"
    "unsafe"
)

func main() {
    foo := C.CString("")
    defer C.free(unsafe.Pointer(foo))
    C.test(foo)
    fmt.Println("Output is: " + C.GoString(foo))
}
Adam B
  • 1,140
  • 2
  • 18
  • 30
  • What you did here is a big "no-no" in C world. You have just overwritten some memory after the pointer's address. – Wodzu Jan 09 '23 at 15:27