cichaqiu4015 2017-03-14 08:32 采纳率: 0%
浏览 1494

内存删除出现问题,在delete[] 处程序运行中断,希望大家能帮我看一下问题出在哪里。

图片说明

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

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 main()
{
    char *a, *b;
    a = new char[50];
    b = new char[50];
    a = "C:\\Users\\Huan\\Desktop\\stl\\12workpiece.STL";
    b = a;
    Triangle_num(a);
    system("pause");    

    delete[] b;//增加delete之后出现报错
}
  • 写回答

2条回答

  • shen_wei 2017-03-14 08:53
    关注
     char *a, *b;
        a = new char[50];
        b = new char[50];
        strcpy(a,"C:\\Users\\Huan\\Desktop\\stl\\12workpiece.STL");
    
        b = a;    // a 和 b 指向同一块区域
        //Triangle_num(a);
        //system("pause");  
    
        delete a;  释放其中之一就可以
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果