向量初始化重写结构指针

时间:2019-03-28 11:11:13

标签: c++ swift

代码执行过程中的错误: 线程2:EXC_BAD_ACCESS

在调试器中:

Before vector init:
*durationMatrix const int64_t *const    0x102ce5650 0x0000000102ce5650
After:
*durationMatrix const int64_t *const    NULL    0x0000000000000000

slv.hpp

struct RoutingData {
    int locationsCount;
    int64_t const * const * distanceMatrix;
    int64_t const * const * durationMatrix;
};

main.cpp

void Solve(const struct RoutingData *routingData) {
std::vector<std::vector<int64>> durations (routingData->locationsCount, std::vector<int64>(routingData->locationsCount, 0LL));
    for (std::size_t fromNode = 0; fromNode <  routingData->locationsCount; fromNode++) {
        for (std::size_t toNode = 0; toNode <  routingData->locationsCount; toNode++) {
            durations[fromNode][toNode] = routingData->durationMatrix[fromNode][toNode];
        }
    }

}

void Solve(const struct RoutingData *routingData) {
    std::vector<std::vector<int64>> durations (routingData->locationsCount, std::vector<int64>(routingData->locationsCount, 0LL));
    printf("%lld\n", routingData->durationMatrix[0][0]);
}

但是此代码可以正常工作:

main.cpp

void Solve(const struct RoutingData *routingData) {
    printf("%lld\n", routingData->durationMatrix[0][0]);
}

从Swift调用的解决函数:

var s = RoutingData()
        s.locationsCount = Int32(self.distanceResponse.distances.count)
        s.distanceMatrix = UnsafePointer(self.distanceResponse.distances.map{UnsafePointer($0.map{Int64($0)})})
        s.durationMatrix = UnsafePointer(self.distanceResponse.durations.map{UnsafePointer($0.map{Int64($0)})})
        Solve(&s)

0 个答案:

没有答案