结构中的结构,动态内存分配

时间:2013-04-08 00:30:58

标签: c++ struct malloc

我正在制作一个3D应用程序,船只必须通过浮标轨道。我还需要将曲目存储在组或"布局"中。浮标类基本上是一个"浮标布局列表"其中有一个"浮标轨道列表"其中有一个浮标列表。

我检查了局部变量观察器,构造函数中的所有内存分配似乎都有效。稍后当调用calculateCoordinates函数时,它进入for循环。在for循环的第一次迭代中,函数指针被使用并且工作正常,但是在这一行上

ctMain[j+1][1] = 0;

函数指针设置为NULL。我猜它有一些东西与结构没有被正确分配或解决。我不知道该怎么做。也许我不理解malloc是如何工作的。

更新 我用双** main_track替换了M3DVector3d main_track,认为malloc没有正确处理typedef。但是我在以后的calculateCoordinates中尝试访问main_track变量时遇到了同样的错误。

更新 它最终是由于访问行

中的错误指针而导致的内存损坏
rotatePointD(&(cTrack->main_track[j]), rotation);

稍后当我尝试访问它时,它只会导致错误。

// Buoys.h
////////////////////////////////////////////

struct buoy_layout_t;
struct buoy_track_t;
typedef double M3DVector3d[3];

class Buoys {
    public:
        Buoys();
        struct buoy_layout_t ** buoyLayouts;
        int nTrackLayouts;
        int currentLayoutID;
        void calculateCoordinates();
};

struct buoy_track_t {
    int nMain, nYellow, nDistract;
    M3DVector3d * main_track,
              yellow_buoys,
              distraction_buoys; 
    double (*f)(double x);
    double (*fp)(double x);
    double thickness;
    M3DVector3d start, end;
};

struct buoy_layout_t {
    int nTracks;
    buoy_track_t ** tracks;
};


// Buoys.cpp
/////////////////////////////

// polynomial and its derivative, for shape of track
double buoyfun1(double x) {return (1.0/292.0)*x*(x-12.0)*(x-24.0);}
double buoyfun1d(double x) {return (1.0/292.0)*((3.0*pow(x,2))-(72.0*x)+288.0);}
// ... rest of buoy shape functions go here ...

Buoys::Buoys() {
    struct buoy_layout_t * cLayout;
    struct buoy_track_t * cTrack;
    nTrackLayouts = 1;
    buoyLayouts = (buoy_layout_t **) malloc(nTrackLayouts*sizeof(*buoyLayouts));
    for (int i = 0; i < nTrackLayouts; i++) {
        buoyLayouts[i] = (buoy_layout_t *) malloc(sizeof(*(buoyLayouts[0])));
    }
    currentLayoutID = 0;    

    // ** Layout 1 **
    cLayout = buoyLayouts[0];
    cLayout->nTracks = 1;
    cLayout->tracks = (buoy_track_t **) malloc(sizeof(*(cLayout->tracks)));
    for (int i = 0; i < 1; i++) {
        cLayout->tracks[i] = (buoy_track_t *) malloc (sizeof(*(cLayout->tracks)));
    }
    cTrack = cLayout->tracks[0];
    cTrack->main_track = (M3DVector3d *) malloc(30*sizeof(*(cTrack->main_track)));
    cTrack->nMain = 30;  
    cTrack->f = buoyfun1;
    cTrack->fp = buoyfun1d;
    cTrack->thickness = 5.5; 
    cTrack->start[0] = 0; cTrack->start[1] = 0; cTrack->start[2] = 0;
    cTrack->end[0] = 30; cTrack->end[1] = 0; cTrack->end[2] = -19;

    // ... initialize rest of layouts here ...
    // ** Layout 2 **
    // ** Layout 3 **
    // ...
    // ** Layout N **

    calculateCoordinates(); 
}

void Buoys::calculateCoordinates()
{
    int i, j;
    buoy_layout_t * cLayout = buoyLayouts[0];
    for (i = 0; i < (cLayout->nTracks); i++) {
        buoy_track_t * cTrack = cLayout->tracks[i];
        M3DVector3d * ctMain = cTrack->main_track;
        double thickness = cTrack->thickness;
        double rotation = getAngleD(cTrack->start[0], cTrack->start[2], 
                             cTrack->end[0], cTrack->end[2]);
        double full_disp = sqrt(pow((cTrack->end[0] - cTrack->start[0]), 2)
                         + pow((cTrack->end[2] - cTrack->start[2]), 2));

        // nBuoys is nBuoys per side. So one side has nBuoys/2 buoys.
        for (j=0; j < cTrack->nMain; j+=2) {
            double id = j*((full_disp)/(cTrack->nMain));
            double y = (*(cTrack->f))(id);
            double yp = (*(cTrack->fp))(id);
            double normal, normal_a;
            if (yp!=0) {
                normal = -1.0/yp;
            }
            else {
                normal = 999999999;
            }

            if (normal > 0) {
                normal_a = atan(normal);
            }
            else {
                normal_a = atan(normal) + PI;
            }

            ctMain[j][0] = id + ((thickness/2.0)*cos(normal_a));
            ctMain[j][1] = 0;
            ctMain[j][2] = y + ((thickness/2.0)*sin(normal_a));
            ctMain[j+1][0] = id + ((thickness/2.0)*cos(normal_a+PI));
            ctMain[j+1][1] = 0; // function pointers get set to null here
            ctMain[j+1][2] = y + ((thickness/2.0)*sin(normal_a+PI));
        }
        for (j=0; j < cTrack->nMain; j++) {
            rotatePointD(&(cTrack->main_track[j]), rotation);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

除非有学习指针的要求或者你不能使用STL,否则鉴于你使用的是C ++,我强烈建议你使用更多的STL,这是你的朋友。但无论如何......

首先,ctMain的类型是* M3DVector3D。所以你可以安全地访问ctMain [0],但你不能访问ctMain [1],也许你的意思是ctMain的类型是** M3DVector3D,在这种情况下,你写的初始化行是:

cTrack->main_track = (M3DVector3d *) malloc(30*sizeof(*(cTrack->main_track)));

会有意义。

更多备注

为什么要在这里分配30个?

cTrack->main_track = (M3DVector3d *) malloc(30*sizeof(*(cTrack->main_track)));

鉴于main_track的类型,您只需要:

cTrack->main_track = (M3DVector3d *) malloc(sizeof(M3DVector3d));

此外,出于组织目的,在执行sizeof时,您可能希望给实际类型检查sizeof,而不是变量(应该没有区别,只是组织),这两个更改:

buoyLayouts = (buoy_layout_t **) malloc(nTrackLayouts*sizeof(buoy_layout_t*));
for (int i = 0; i < nTrackLayouts; i++) {
buoyLayouts[i] = (buoy_layout_t *) malloc(sizeof(buoy_layout_t));
}

cLayout->tracks = (buoy_track_t **) malloc(clayout->nTracks * sizeof(buoy_track_t*));
for (int i = 0; i < 1; i++) {
cLayout->tracks[i] = (buoy_track_t *) malloc(sizeof(buoy_track_t));
}