We have (for all intents and purposes) something similar to the following C++ code.
calc.h
#pragma once
extern "C" {
void doCalc(uint8_t** buffer);
}
calc.cpp
#include <cstdint>
#include "calc.h"
void doCalc(uint8_t** buffer) {
uint8_t lb[256];
for (int i = 0; i < 256; i++) {
lb[i] = i;
}
*buffer = new uint8_t[256];
std::memcpy(*buffer, lb, 256);
}
We need to call this method from Go, but unsure how to construct the buffer variable on the Go side to pass in (seeing as the length is unknown prior to calling - the 256 in the C++ function should be considered unknown).
mallocin C++, not even for simple buffers. Usenew[]instead; The second problem is that the argument variablebufferis a local variable within the function, and whose life-time ends when the function returns. If you want to modify the variable inside the function you either need to pass it by reference or return it; Thirdly, to even be able to call the function you need to declare it asextern "C".