Updated: I work on a Golang code that implements a C library through GCO. Between the arguments of the C function there is a *Cchar object. I convert the string to *char with the C.CString () function, but I have problems when I need to implement it with a for loop. Thanks to @peterSON I can move forward. Now, the program runs a little longer until it fails. Now I get results until the third cycle (of five). When starting the fourth, again it indicates "exit status 3221226356"
This is a piece of code:
package main
//#include <stdio.h>
//#include <stdlib.h>
//#cgo CFLAGS: -I C:/Go-Project/src/Astrocal
//#cgo LDFLAGS: -L C:/Go-Project/src/Astrocal72 -lswedll64
//#include "swephexp.h"
import "C"
import (
"fmt"
"unsafe"
)
...
func (P DATA) FixStarUT() {
C.swe_set_ephe_path(C.CString("C:/sweph/ephe")) // connect to library C.
var Cxx [6]C.double
var serr string
var Cserr *C.char = C.CString(serr)
var FixStarCchar *C.char = C.CString("")
var iFLAG = []uint32{33026, 35074, 266, 2314, 98562, 100610, 65802, 67851} // 33026, 35074, 266, 2314, 98562, 100610, 65802, 67851
var fixStarSlice = []string{"aboras", "achernar", "adara", "ain", "vega"}
fmt.Println("Memory Address of FixStarCchar before For Range ->", FixStarCchar)
// For Range 1
for i, valFixStar := range fixStarSlice {
defer C.free(unsafe.Pointer(FixStarCchar))
defer C.free(unsafe.Pointer(Cserr))
FixStarCchar = C.CString(valFixStar)
fmt.Println("\nExternal cycle:", i, "Star (valFixStar):", valFixStar)
fmt.Println("Memory Add. of FixStarCchar ->", FixStarCchar)
// For Range 2
for i2, valSistem := range iFLAG {
// C functions
C.swe_set_topo(C.double(P.LONsite), C.double(P.LATsite), C.double(P.ALTsite))
C.swe_fixstar_ut(FixStarCchar, C.double(P.JulianDay), C.int(valSistem), &Cxx[0], Cserr)
// Exporting results to a slice
var positionFixStar []float64
positionFixStar = append(positionFixStar, float64(Cxx[0]), float64(Cxx[1]), float64(Cxx[2]), float64(Cxx[3]), float64(Cxx[4]), float64(Cxx[5]))
fmt.Println("\tInternal cycle:", i2, "\tSistem:", valSistem, "\tPosition:", positionFixStar)
}
}
C.swe_close() // disconnect library C
}
Output:
PS C:\Go-Project\src\Astrocal72> go run main.go
Memory Address of FixStarCchar before For Range -> 0x1019c80
External cycle: 0 Star (valFixStar): aboras
Memory Add. of FixStarCchar -> 0x1019ca0
Internal cycle: 0 Sistem: 33026 Position: [331.7560238873596 -23.63731225621964 1 0 0 0]
Internal cycle: 1 Sistem: 35074 Position: [343.5555314743071 -32.70632643606941 1 0 0 0]
Internal cycle: 2 Sistem: 266 Position: [331.7521303243937 -23.639006118391627 1 0 0 0]
Internal cycle: 3 Sistem: 2314 Position: [343.5525162650043 -32.709352136426915 1 0 0 0]
Internal cycle: 4 Sistem: 98562 Position: [307.45659891000344 -23.63731225621964 1 0 0 0]
Internal cycle: 5 Sistem: 100610 Position: [319.25465207507114 -32.705260202398776 1 0 0 0]
Internal cycle: 6 Sistem: 65802 Position: [307.4527053470375 -23.63900611839163 1 0 0 0]
Internal cycle: 7 Sistem: 67851 Position: [319.2516367429912 -32.7082857906988 0.9999999999999999 0 0 0]
External cycle: 1 Star (valFixStar): achernar
Memory Add. of FixStarCchar -> 0x101b3a0
Internal cycle: 0 Sistem: 33026 Position: [344.86781077811537 -59.37245156484419 1 0 0 0]
Internal cycle: 1 Sistem: 35074 Position: [24.13636287205359 -57.39092532881141 0.9999999999999999 0 0 0]
Internal cycle: 2 Sistem: 266 Position: [344.86287075464315 -59.37668655703472 1 0 0 0]
Internal cycle: 3 Sistem: 2314 Position: [24.138688732702057 -57.39568950801329 1 0 0 0]
Internal cycle: 4 Sistem: 98562 Position: [320.56838580075924 -59.37245156484419 0.9999999999999998 0 0 0]
Internal cycle: 5 Sistem: 100610 Position: [359.83323867384445 -57.39150066286222 1 0 0 0]
Internal cycle: 6 Sistem: 65802 Position: [320.563445777287 -59.37668655703472 0.9999999999999999 0 0 0]
Internal cycle: 7 Sistem: 67851 Position: [359.835563909282 -57.39626493585631 1 0 0 0]
External cycle: 2 Star (valFixStar): adara
Memory Add. of FixStarCchar -> 0x101ae40
Internal cycle: 0 Sistem: 33026 Position: [110.31533627711894 -51.36455324165249 1 0 0 0]
Internal cycle: 1 Sistem: 35074 Position: [104.3395190395512 -28.925572447433648 1 0 0 0]
Internal cycle: 2 Sistem: 266 Position: [110.32409258962711 -51.363920733883326 0.9999999999999999 0 0 0]
Internal cycle: 3 Sistem: 2314 Position: [104.34580120684954 -28.92581091916995 0.9999999999999999 0 0 0]
Internal cycle: 4 Sistem: 98562 Position: [86.01591129976276 -51.36455324165249 0.9999999999999998 0 0 0]
Internal cycle: 5 Sistem: 100610 Position: [80.0401030111902 -28.927947460228687 0.9999999999999999 0 0 0]
Internal cycle: 6 Sistem: 65802 Position: [86.02466761227095 -51.363920733883326 1 0 0 0]
Internal cycle: 7 Sistem: 67851 Position: [80.04638532334661 -28.92818591289178 1 0 0 0]
External cycle: 3 Star (valFixStar): ain
Memory Add. of FixStarCchar -> 0x101ad70
exit status 3221226356
Hope you can help me confront this problem, thanks anyway.