cichaqiu4015
cichaqiu4015
采纳率0%
2017-03-13 03:33

关于C++ 指针new和delete的问题。

图片说明

创建了四个结构体,并为其分配了内存空间,删除delete的代码后算法可以正常运行,加入delete后运行终止,出现上图的报错,希望有人能帮我看一下,非常感谢大家。

 #define _CRT_SECURE_NO_WARNINGS
#include <iostream> 
#include <iomanip> 
#include <math.h> 
#include <ctime> 
using namespace std;

struct POINT3D //顶点的数据结构
{
    float x;
    float y;
    float z;
};
struct TRIANGLE //三角面片
{
    POINT3D *p_pnt[3];

};
struct PART //部件实时数据结构/添加
{
    char* file;
    char* name;
    int tri_num;
    int p_num;
    float T[4][4];
    TRIANGLE *p_tri[3000];
};

struct   PART_0 //部件初始数据结构/添加
{
    POINT3D   *p_pnt_0[25100];
};
PART Tool[10];
PART_0 Tool_0[10];

bool ifbin(const char *fname) //判断是否二进制文件 
{
    bool bin = false; //默认文本文件
    FILE *fp;
    fp = fopen(fname, "r");
    if (fp == NULL)
    {
        return false;
    }
    char s[256];
    fgets(s, 256, fp);//读一行
    char *q = "solid binary";
    int i;
    for (i = 0; i<12; i++)
    {
        if (s[i] != q[i])   break;
    }
    if (i<5 || i == 12)
    {
        bin = true; //是二进制文件
    }
    else
        bin = false;
    fclose(fp);
    return bin;
}

bool compare(const char *s, const char *q, int n)
{
    int i = 0;
    while (i<n)
    {
        if (q[i++] != s[i++])
            return false;
    }
    if (i = n && (s[n] - ' ' == 0 || s[n] - '\n' == 0 || s[n] == NULL))
        return true;
    else
        return false;
}

int Triangle_num(char* fname)
{
    FILE *fp;
    if ((fp = fopen(fname, "r ")) == NULL)
    {
        printf("Can't open file");
        exit(1);
    }

    int npoint = 0; //点个数
    int count = 0; //三角面片个数

    while (!feof(fp))     //feof()函数为判断文件是否结束,C语言
    {
        char f[256];
        fgets(f, 256, fp);
        if (compare(strtok(f, "   "), "vertex ", 6))
        {
            if (++npoint == 3)
            {
                count++;
                npoint = 0;
            }
        }
    }
    fclose(fp);
    return   count;
}

void read_stl(int t, const char *filename, int n_triangle)//部件,文件名,三角面片个数 

{
    bool bin;
    int n_T = 0, n_P = 0;
    bin = ifbin(filename);
    if (bin == false) //如果是文本文件
    {
        //n_triangle = Triangle_num(char* filename); //计算三角面片数
        //t_p3d *t_p = new t_p3d[n_triangle * 3];//t_p指向点面结构 
        FILE  *fp;
        if ((fp = fopen(filename, "r")) == NULL)
        {
            printf("Can 't   open   file ");
            exit(1);
        }
        float me[3];//存储顶点的三个坐标值 
                    //int count = 0;//三角面片个数    
        int count = 0;
        while (!feof(fp))
        {
            char f[256];
            fgets(f, 256, fp);//读一行 

            if (compare(strtok(f, "   "), "vertex ", 6))//以点开始,才能读点 
            {
                for (int h = 0; h<3; h++)
                {
                    me[h] = atof(strtok(NULL, "   "));
                }
                (*(*Tool[t].p_tri[n_T]).p_pnt[n_P]).x = (double)me[0];
                (*(*Tool[t].p_tri[n_T]).p_pnt[n_P]).y = (double)me[1];
                (*(*Tool[t].p_tri[n_T]).p_pnt[n_P]).z = (double)me[2];
                (*Tool_0[t].p_pnt_0[count]).x = (double)me[0];
                (*Tool_0[t].p_pnt_0[count]).y = (double)me[1];
                (*Tool_0[t].p_pnt_0[count]).z = (double)me[2];
                count++;
                if (n_P == 2) //满三个点,三角面片号加1
                {
                    n_T++;
                    n_P = 0;
                }
                else n_P++;
            }
        }

        fclose(fp);
        //check(n_pnt, p_pnt, p_pnt0,n_triangle, p_TRIANGLE, t_p,pt1,pt0);//去除冗余点 
        //delete[] t_p;
    }
}

void main()
{
    for (int i = 0; i < 10; i++)
    {
        Tool[i].file = new char[1024];
        Tool[i].name = new char[1024];
        for (int j = 0; j < 3000; j++)
        {
            Tool[i].p_tri[j] = new TRIANGLE;
            for (int k = 0; k < 3; k++)
            {
                Tool[i].p_tri[j]->p_pnt[k] = new POINT3D;
            }
        }

    }
    for (int p = 0; p < 10; p++)//分配Tool_0内存
    {
        for (int q = 0; q < 25100; q++)
        {
            Tool_0[p].p_pnt_0[q] = new POINT3D;
        }
    }
    Tool[0].file = "C:\\Users\\Huan\\Desktop\\stl\\12workpiece.STL";
    Tool[0].name = "GJ";
    Tool[1].file = "C:\\Users\\Huan\\Desktop\\stl\\M1.STL";
    Tool[1].name = "M1";
    Tool[2].file = "C:\\Users\\Huan\\Desktop\\stl\\M2.STL";
    Tool[2].name = "M2";
    Tool[3].file = "C:\\Users\\Huan\\Desktop\\stl\\11tool.STL";
    Tool[3].name = "M3";
    Tool[4].file = "C:\\Users\\Huan\\Desktop\\stl\\D1.STL";
    Tool[4].name = "D1";
    Tool[5].file = "C:\\Users\\Huan\\Desktop\\stl\\D2.STL";
    Tool[5].name = "D2";
    Tool[6].file = "C:\\Users\\Huan\\Desktop\\stl\\F1.STL";
    Tool[6].name = "F1";
    Tool[7].file = "C:\\Users\\Huan\\Desktop\\stl\\F2.STL";
    Tool[7].name = "F2";
    Tool[8].file = "C:\\Users\\Huan\\Desktop\\stl\\S1.STL";
    Tool[8].name = "S1";
    Tool[9].file = "C:\\Users\\Huan\\Desktop\\stl\\S2.STL";
    Tool[9].name = "S2";

    for (int k = 0; k < 10; k++)//计算三角面片数和点数
    {
        Tool[k].tri_num = Triangle_num(Tool[k].file);
        Tool[k].p_num = Tool[k].tri_num * 3;
    }

    for (int i = 0; i < 10; i++)//读取STL文件
    {
        read_stl(i, Tool[i].file, Tool[i].tri_num);//部件,文件名,三角面片个数 
    }


    for (int p = 0; p < 10; p++)//删除内存,加入这一段代码之后程序终止
    {
        for (int q = 0; q < 25100; q++)
        {
            delete Tool_0[p].p_pnt_0[q];
        }
    }

    for (int i = 0; i < 10; i++)
    {
        for (int j = 0; j < 3000; j++)
        {
            for (int k = 0; k < 3; k++)
            {
                delete Tool[i].p_tri[j]->p_pnt[k];
            }
            delete Tool[i].p_tri[j];
        }
        delete[] Tool[i].file;
        delete[] Tool[i].name;
    }



    system("pause");
}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • IssueNo IssueNo 4年前

    Tool[0].file = "C:\Users\Huan\Desktop\stl\12workpiece.STL";
    Tool[0].name = "GJ";
    //类似的字符串赋值语句是不对的。改成strcpy(Tool[0],file,"C:\Users\Huan\Desktop\stl\12workpiece.STL")或者strncpy(Tool[0],file,"C:\Users\Huan\Desktop\stl\12workpiece.STL",1024);
    //你写的是指针变量赋值,不是字符串赋值

    点赞 评论 复制链接分享