I am new with go and cgo and after browsing the internet for some time I have not figured out a good and fast way to change a char* from go.
What is the fastest way to change *C.char from go&
Here is my code and my attempt to change the string(It does not work)
package asciiEngine
// #include <windows.h>
import "C"
type Screen struct {
Width, Height int
Length C.ulong
Data *C.char
GoData string
HConsole C.HANDLE
BytesWritten C.DWORD
Start C.COORD
}
func (s Screen) Draw() {
C.WriteConsoleOutputCharacter(s.HConsole, s.Data, s.Length, s.Start, &s.BytesWritten)
}
func CreateScreen(width, height int) Screen {
screen := Screen{
Width: width,
Height: height,
Length: C.ulong(width * height),
Data: (*C.char)(C.malloc(C.ulonglong(width * height))),
HConsole: C.CreateConsoleScreenBuffer(C.GENERIC_READ|C.GENERIC_WRITE, 0, nil, C.CONSOLE_TEXTMODE_BUFFER, nil),
BytesWritten: 0,
}
screen.GoData = C.GoString(screen.Data) // my attempt to get a reference to the C string
//C.SetConsoleActiveScreenBuffer(screen.HConsole)
return screen
}
main.go:
package main
// #include "stdio.h"
// void print(char* data) {
// printf(data);
// }
import "C"
import (
"fmt"
"github.com/demantar/ascii-engine"
)
func main() {
screen := asciiEngine.CreateScreen(100, 50)
C.print((*C.char)(screen.Data))
fmt.Println()
screen.GoData = "askdssdfselkkskdkflsekfjdkjfksjeflsdkfjjekdjflskasdfkksdjjekdskdfjkskd"
C.print((*C.char)(screen.Data))
}
output
P
P
I'm also pretty new to C and am doing this because I could not find a library to do this
For example, use gData as a Go byte slice reference to the underlying cData C char array.
package main
import (
"fmt"
"unsafe"
)
/*
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int printData(unsigned char *data) {
return printf("cData: %lu \"%s\"\n", (long unsigned int)strlen(data), data);
}
*/
import "C"
func main() {
// Allocate C data buffer.
width, height := 8, 2
lenData := width * height
// add string terminating null byte
cData := (*C.uchar)(C.calloc(C.size_t(lenData+1), C.sizeof_uchar))
// When no longer in use, free C allocations.
defer C.free(unsafe.Pointer(cData))
// Go slice reference to C data buffer,
// minus string terminating null byte
gData := (*[1 << 30]byte)(unsafe.Pointer(cData))[:lenData:lenData]
// Write and read cData via gData.
for i := range gData {
gData[i] = '.'
}
copy(gData[0:], "Data")
gData[len(gData)-1] = 'X'
fmt.Printf("gData: %d %q\n", len(gData), gData)
C.printData(cData)
}
Output:
gData: 16 "Data...........X"
cData: 16 "Data...........X"
Reference: Command cgo
Related
I'm testing cgo and every simple hello world like code works well.
but i have a problem with C code below.
The C code is that traverse a directory tree and sums file size.
if i build with go command, then the build is OK with no error.
but when running, there is a "segmentation violation" error occurred
bash$./walkdir
fatal error: unexpected signal during runtime execution
[signal SIGSEGV: segmentation violation code=0x1 addr=0x1 pc=0x7f631e077c1a]
. . . .
-------------------------------------------------------------
package main
/*
#include <stdint.h>
#include <fts.h>
#include <sys/stat.h>
uintmax_t get_total_size(char *path)
{
uintmax_t total_size = 0;
FTS *fts = fts_open(&path, FTS_PHYSICAL, NULL);
FTSENT *fent;
while ((fent = fts_read(fts)) != NULL)
if (fent->fts_info == FTS_F)
total_size += fent->fts_statp->st_size;
fts_close(fts);
return total_size;
}
*/
import "C"
import "fmt"
func main() {
fmt.Println(C.get_total_size(C.CString("/usr")))
}
fts_open is defined like this:
fts_open()
The fts_open() function takes a pointer to an array of character
pointers naming one or more paths which make up a logical file
hierarchy to be traversed. The array must be terminated by a
null pointer.
C does not have direct support for arrays; it only has pointers.
In your case you pass fts_open a single valid pointer but it is not located in an array which has a NULL pointer as the immediately following element, so fts_open continues to scan the memory past &path — looking for a NULL pointer, — and eventually tries to read memory at some address it is forbidden to do so (usually because the page at that address was not allocated).
A way to fix it is to create that array and initialize it on the C side.
Looks like you're using a reasonably up-to-date standard of C, so let's just use direct literal to initialize the array:
package main
/*
#include <stddef.h> // for NULL
#include <stdint.h>
#include <stdlib.h> // for C.free
#include <fts.h>
#include <sys/stat.h>
uintmax_t get_total_size(char *path)
{
uintmax_t total_size = 0;
char * path_argv[2] = {path, NULL};
FTS *fts = fts_open(path_argv, FTS_PHYSICAL, NULL);
FTSENT *fent;
while ((fent = fts_read(fts)) != NULL)
if (fent->fts_info == FTS_F)
total_size += fent->fts_statp->st_size;
fts_close(fts);
return total_size;
}
*/
import "C"
import (
"fmt"
"unsafe"
)
func main() {
cpath := C.CString("/usr")
defer C.free(unsafe.Pointer(cpath))
fmt.Println(C.get_total_size(cpath))
}
Note that your program has one bug and one possible problem:
A bug is that the call C.CString allocates a chunk of memory by performing a call to malloc(3) from the linked C library, and you did not free that memory block.
The symbol NULL is defined in "stddef.h"; you might or might not get an error when compiling.
I've fixed both problems in my example.
A further improvement over our example might be leveraging the ability of fts_* functions to scan multiple paths in a single run; if we were to implement that, it would have more sense to allocate the array for the 1st argument of fts_open on the Go's side:
package main
/*
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <fts.h>
#include <sys/stat.h>
uintmax_t get_total_size(char * const *path_argv)
{
uintmax_t total_size = 0;
FTS *fts = fts_open(path_argv, FTS_PHYSICAL, NULL);
FTSENT *fent;
while ((fent = fts_read(fts)) != NULL)
if (fent->fts_info == FTS_F)
total_size += fent->fts_statp->st_size;
fts_close(fts);
return total_size;
}
*/
import "C"
import (
"fmt"
"unsafe"
)
func main() {
fmt.Println(getTotalSize("/usr", "/etc"))
}
func getTotalSize(paths ...string) uint64 {
argv := make([]*C.char, len(paths)+1)
for i, path := range paths {
argv[i] = C.CString(path)
defer C.free(unsafe.Pointer(argv[i]))
}
return uint64(C.get_total_size(&argv[0]))
}
Note that here we did not explicitly zero out the last argument of argv because — contrary to C, — Go initializes each allocated memory block with zeroes, so once argv is allocated, all its memory is already zeroed.
you are getting the error cause "fts_open" requires a character pointer to an array which is NULL terminating like char *argv[] = { path, NULL };..(https://linux.die.net/man/3/fts_open)
package main
/*
#include <stdint.h>
#include <fts.h>
#include <sys/stat.h>
uintmax_t get_total_size(char *path)
{
uintmax_t total_size = 0;
char *argv[] = { path, NULL };
FTS *fts = fts_open(argv, FTS_PHYSICAL, NULL);
if (fts == NULL)
return 0;
FTSENT *fent;
while ((fent = fts_read(fts)) != NULL)
if (fent->fts_info == FTS_F)
total_size += fent->fts_statp->st_size;
fts_close(fts);
return total_size;
}
*/
import "C"
import "fmt"
func main() {
fmt.Println(C.get_total_size(C.CString("/usr")))
}
so adding the array pointer will fix the code.
The same code works when compiled with GCC but fts_open returns NULL.I am guessing that there is some difference in optimization between gcc and cgo(not very sure)
I tried some test results and was able to find that when compiling with GCC the char **pointer is getting NULL-terminated but in the case of cgo it was not getting NULL-terminated so you were getting "SIGSEGV" as your code is reading invalid memory reference
#include <stdio.h>
#include <string.h>
void try(char **p)
{
while (*p != NULL)
{
printf("%zu\n", strlen(*p));
++p;
}
}
void get_total_size(char *path)
{
try(&path);
}
int main()
{
get_total_size("/usr");
}
c code (which works)
package main
/*
#include <stdio.h>
#include <string.h>
void try(char **p)
{
while (*p != NULL)
{
printf("%zu\n", strlen(*p));
++p;
}
}
void get_total_size(char *path)
{
try(&path);
}
*/
import "C"
func main() {
C.get_total_size(C.CString("/usr"))
}
same go code you will face error
I have C code in which I am calling golang functions. I am able to do it for primitives data types (int/float etc.) but I want to return some other data structure like array/list/slice.
I could not find any solution on internet.
Looking for help.
Want to return a array/slice/list of string data type.
It would be helpful if you provide additional information, i.e. example code you are currently working on.
As stated from the Cgo documentation page:
Go array types are not supported; use a C pointer
To do so
hello.go
package main
// #include <stdlib.h>
import "C"
import "unsafe"
// StringSlice is a wrapper arround GoStringSlice to make it usable in C.
//export StringSlice
func StringSlice() **C.char {
x := GoStringSlice()
ret := C.malloc(C.size_t(len(x)) * C.size_t(unsafe.Sizeof(uintptr(0))))
// convert to usable format so we are able to fill it with data
pRet := (*[1<<30 - 1]*C.char)(ret)
for i, item := range x {
pRet[i] = C.CString(item)
}
return (**C.char)(ret)
}
func GoStringSlice() []string {
return []string{
"Hello",
"World",
}
}
func main() {}
hello.c
#include <stdio.h>
#include "hello.h"
int main() {
printf("Hello from C!\n");
char **slice = StringSlice();
int numItems = sizeof(slice) / sizeof(char *);
printf("Number of items: %d\n", numItems+1);
printf("String #0: %s\n", *slice);
slice++;
printf("String #1: %s\n", *slice);
return 0;
}
You have to execute go build -buildmode=c-archive hello.go which will generate a hello.h and hello.a.
The hello.a has to be compiled with your C code: gcc -pthread hello.c hello.a -o hello.
I am trying to use cgo to use Go package in C code. Following is a piece of my code:
func LinearTransformToUInt8(frame []int64, winWidth int, winCenter int) []uint8 {
var transformed []uint8
// my cool code
return transformed
}
However, when calling from C, it says
panic: runtime error: cgo result has Go pointer
I believe the problem is the returned []uint8 is a Go type, which should be replaced by a C type. However, I don't know how to achieve it. Please help!
main.go
package main
import (
"C"
"unsafe"
)
import (
"reflect"
)
func main() {
}
//export phew
func phew() uintptr {
res := make([]uint8, 2)
for i := 0; i < 2; i++ {
res[i] = uint8(i + 1)
}
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&res))
return hdr.Data
}
main.c
#include <stdio.h>
#include <inttypes.h>
#include "libtemp.h"
int main(){
uintptr_t resPtr = phew();
uint8_t *res = (uint8_t*)resPtr;
for (int i = 0; i < 2; i++){
printf("%d\n", res[i]);
}
printf("Exiting gracefully\n");
}
You cannot pass a Go pointer which contains other Go Pointer, slice,string,channel,function, interface, map contain pointers.
So one cannot pass them around, rules to passing around pointers are documented here and go's representation of basic types is documented here.
But some Go contributors were saying, one shouldn't return a Go pointer to C code in the first place.
I'm trying to use the C function strfmon using cgo.
The example C code that works is:
#include <stdio.h>
#include <monetary.h>
int main(void)
{
char str[100];
double money = 1234.56;
strfmon(str, 100, "%i", money);
printf("%s\n", string);
}
The Go code I've written so far is:
package main
// #cgo CFLAGS: -g -Wall
// #include <stdlib.h>
// #include <monetary.h>
import "C"
import (
"fmt"
)
func main() {
str := [100]C.char{}
var money C.double = 1234.56
C.strfmon(str, 100, "%i", money)
fmt.Printf("%+v\n", str)
}
When I go run main.go I get the following error:
./main.go:14:2: unexpected type: ...
I believe the ... refers to the variadic argument in strfmon but I'm not sure how to work around that from Go.
According to the cgo command documentation:
Calling variadic C functions is not supported. It is possible to circumvent this by using a C function wrapper.
And strfmon(3p) is indeed a variadic function as indicated by the ... characters in the signature:
ssize_t strfmon(char *restrict s, size_t maxsize,
const char *restrict format, ...);
As such, you can create a wrapper function in C which has a fixed number of arguments and calls strfmon(...) as needed, for example:
package main
// #cgo CFLAGS: -g -Wall
//
// #include <locale.h>
// #include <monetary.h>
// #include <stdlib.h>
//
// size_t format_amount(char * s, size_t maxsize, char * format, double amount)
// {
// setlocale(LC_ALL, "en_US");
// return strfmon(s, maxsize, format, amount);
// }
//
import "C"
import "fmt"
import "unsafe"
const SIZE = 100
func main() {
str := C.CString(string(make([]byte, SIZE)))
money := C.double(1234.56)
format := C.CString("[%n]")
C.format_amount(str, SIZE-1, format, money) // Call our wrapper here.
fmt.Printf("OK: %s\n", C.GoString(str))
// OK: [$1,234.56]
C.free(unsafe.Pointer(str))
C.free(unsafe.Pointer(format))
}
My code is:
package main
/*
#include <stdio.h>
#include <string.h>
void fill_2d_array(char (*s)[16]) {
strcpy(s[0], "hello");
strcpy(s[1],"cgo");
}
*/
import "C"
import "fmt"
import "unsafe"
func main() {
dirs := make([][]byte, 4)
for i := 0; i < 4; i++ {
dirs[i] = make([]byte, 16)
}
C.fill_2d_array(((*C.char)[16])(unsafe.Pointer(&dirs)))
fmt.Println(dirs)
}
When I run with go run test.go, it failed and said:
./test.go:21: type *C.char is not an expression
My Question is how to pass a 2 dimensional slice to a C function like fill_2d_array above?
Thanks.
Solved by:
C.fill_2d_array((*[16]C.char)(unsafe.Pointer(&dirs)))