package main
/*
#include <stdio.h>
#include <stdlib.h>
struct CType {
int Id;
int Id2;
};
typedef struct CType TheCType;
TheCType *getTheArray(int length){
TheCType *ctype;
ctype = malloc(length*sizeof(TheCType));
return ctype;
}
void Free(TheCType *ctype){
free(ctype);
}
*/
import "C"
import (
//"fmt"
"reflect"
//"time"
"unsafe"
)
var theGoSlice []C.TheCType
func cc() {
length := C.int(500000)
var theCArray = C.getTheArray(length)
C.Free(theCArray)
sliceHeader := (*reflect.SliceHeader)((unsafe.Pointer(&theGoSlice)))
sliceHeader.Cap = int(uintptr(length))
sliceHeader.Len = int(uintptr(length))
sliceHeader.Data = uintptr(unsafe.Pointer(theCArray))
/*
#include <stdio.h>
#include <stdlib.h>
struct CType {
int Id;
int Id2;
};
typedef struct CType TheCType;
TheCType *getTheArray(int length){
TheCType *ctype;
ctype = malloc(length*sizeof(TheCType));
return ctype;
}
void Free(TheCType *ctype){
free(ctype);
}
*/
import "C"
import (
//"fmt"
"reflect"
//"time"
"unsafe"
)
var theGoSlice []C.TheCType
func cc() {
length := C.int(500000)
var theCArray = C.getTheArray(length)
C.Free(theCArray)
sliceHeader := (*reflect.SliceHeader)((unsafe.Pointer(&theGoSlice)))
sliceHeader.Cap = int(uintptr(length))
sliceHeader.Len = int(uintptr(length))
sliceHeader.Data = uintptr(unsafe.Pointer(theCArray))
}
不知道是否正确
有疑问加站长微信联系(非本文作者)