【问题标题】:Initializing dynamic pointer to multidimensional array初始化指向多维数组的动态指针
【发布时间】:2016-08-16 18:47:13
【问题描述】:

我是编程新手,正在尝试在 C++ 上实现星搜索算法。我有分段错误:11 因为没有初始化我的指针。我尝试了几种不同的方法都无济于事。 我仍然对整个指针和动态内存分配概念感到困惑。

谁能帮我弄清楚?谢谢。

    #include <iostream>
    #include <vector>
    #include <fstream>
    #include <math.h>
    #include <stdio.h>
    #include <string>
    #include <vector>
    #include <iostream>
    #include <fstream>

    using namespace std;


    // Definition of the heuristic. The heuristic in this problem is the distance between
    // two coordinates
    double heuristic(double x1, double y1, double x2, double y2) {
        double dx, dy;
        dx = x1 - x2;
        dy = y1 - y2;
        return sqrt(dx*dx - dy*dy);
        //return sqrt(pow((x1 - x2), 2) + pow((y1 - y2), 2));
    }

    // ----- A Star Search Algorithm (f = g + h)----
    double** a_star_search(double points[][2]) {
        int count = 1;
        double** points1 = NULL;
    //  points1[10][2];
        double x1 = points[0][0];
        double y1 = points[0][1];
        points1[count - 1][0] = x1;
        points1[count - 1][1] = y1;
        while (count <= 10) {
            double tempx1;
            double tempy1;
            double distance = 10000000;
            for (int i = 0; i < 10; i++) {
                if (points[i][0] != 0 && points[i][1] != 0) {
                    double distance2 = heuristic(x1, y1, points[i][0], points[i][1]);
                    if (distance2 < distance) {
                        tempx1 = points[i][0];
                        tempy1 = points[i][1];
                        distance = distance2;
                    }
                }
            }
            x1 = tempx1;
            y1 = tempy1;
            count++;
            points1[count - 1][0] = x1;
            points1[count - 1][1] = y1;
        }
        return points1;
    }

    int main() {
        double points[7][2];
        int counter = 0;
        ifstream infile("waypoints.txt");
        int a, b;
        while (infile >> a >> b)
        {
            points[counter][0] = a;
            points[counter][1] = b;
            counter++;
        }
        points[6][0] = points[0][0];
        points[6][1] = points[0][1];
        double** points1 = a_star_search(points);

        cout << "Initial Sequence: ";
        for (int i = 0;i < 7;i++) {
            cout << "(" <<points[i][0] << " , " << points[i][1] << "), ";
        }
        cout << "\n\nOptimized Sequence: ";
        for (int i = 0;i < 7;i++) {
            cout << "(" << points1[i][0] << " , " << points1[i][1] << "), ";
        }
        cout << "\n\nTotal Distance after A* search: ";
        double totaldistance = 0;
        for (int i = 0;i < 6;i++) {
            double dis = heuristic(points1[i][0], points1[i][1], points1[i + 1][0], points1[i + 1][1]);
            cout << dis << "+";
            totaldistance = totaldistance + dis;
        }
        cout<< "=" << totaldistance <<endl;
    }

【问题讨论】:

  • 你尝试了多少种方法?分配动态数组?如果你看一下,你会发现如何做成千上万的地方,但不要打扰。试试std::vector,而不是动态数组。为您省去很多麻烦。

标签: c++ pointers multidimensional-array dynamic


【解决方案1】:

在 a_star_search 函数中将 double** points1 变量设置为 NULL 后,您没有为它动态分配内存。正如@user4581301 所指出的,请使用std::vector。这将显着简化您的代码,值得花时间学习 STL 容器。

【讨论】:

    猜你喜欢
    • 2013-08-18
    • 1970-01-01
    • 2010-10-11
    • 1970-01-01
    • 2015-04-28
    • 1970-01-01
    • 2015-12-31
    • 1970-01-01
    • 2020-04-07
    相关资源
    最近更新 更多