0

I have a C function, that is calling a go-Function with an char array parameter. The go-Function has to modify the content of the parameter. How to accomplish this?

void cFunction() {
 char buffer[9] = "aaabbbccc"; // 9 is in this case correct, it is not a null-terminated-string
 goFunction(buffer);
 // buffer shall be modified
}
func goFunction(cBuffer *C.char) {
  // how to modify 3-5?
  //strncpy(cBuffer+3, "XXX")
}

EDIT: to be more precise. I have to implement a callbackfunction, that accepts an outparameter, which I have to manipulate.

  void callback(char outbuffer[9]) {
    goFunction(outbuffer);
  }

as I understand Franks answer, I should do something like

  allocate new go buffer
  convert C buffer to go buffer
  manipulate go buffer
  allocate new C buffer
  convert go buffer to C buffer
  memcpy C buffer into outbuffer

That is too much allocation and conversion for my taste

peterh
  • 11,875
  • 18
  • 85
  • 108
Peter Miehle
  • 5,984
  • 2
  • 38
  • 55

2 Answers2

2

See the documentation for Turning C arrays into Go slices to get a indexable go slice containing the C data.

Because you are modifying the C buffer data in place, using the Go slice as a proxy, you can simply pass the same buffer to the callback. Note that using append may allocate a new Go array for your slice, so you need to avoid it and make sure you have sufficient space available in the buffer beforehand.

func goFunction(cBuffer *C.char, length int) {
    slice := (*[1 << 28]C.char)(unsafe.Pointer(cBuffer))[:length:length]
    // slice can now be modified using Go syntax, without pointer arithmetic
    
    C.callback(cBuffer)
}
JimB
  • 104,193
  • 13
  • 262
  • 255
1

It is not recommanded to modify C struct in Go, or Go struct in C. Convert it at the interface, Ref1.

A few special functions convert between Go and C types by making copies of the data. In pseudo-Go definitions

More, give you a way to convert string with zero copy, Ref2.

func char2Slice(data unsafe.Pointer, len C.int) []byte {
    var value []byte
    sH := (*reflect.SliceHeader)(unsafe.Pointer(&value))
    sH.Cap, sH.Len, sH.Data = int(len), int(len), uintptr(data)
    return value
}
Frank Wang
  • 788
  • 7
  • 23
  • what if I have to modify the C buffer directly (legacy-code, i have a callback-function that manipulates the data of the out-parameter)? – Peter Miehle Jan 28 '21 at 12:19
  • @PeterMiehle I can't get you right. Can you update your question and add this 'legacy-CallBack' in your description and pseudocode? – Frank Wang Jan 28 '21 at 12:25