univers1314
码龄12年
关注
提问 私信
  • 博客:177
    问答:15
    192
    总访问量
  • 1
    原创
  • 210,938
    排名
  • 0
    粉丝
  • 0
    铁粉

个人简介:一个打杂的

IP属地以运营商信息为准,境内显示到省(区、市),境外显示到国家(地区)
IP 属地:广东省
  • 加入CSDN时间: 2012-12-21
博客简介:

univers1314的博客

查看详细资料
  • 原力等级
    领奖
    当前等级
    0
    当前总分
    23
    当月
    0
个人成就
  • 获得1次点赞
  • 内容获得2次评论
  • 获得0次收藏
创作历程
  • 1篇
    2023年
成就勋章
创作活动更多

HarmonyOS开发者社区有奖征文来啦!

用文字记录下您与HarmonyOS的故事。参与活动,还有机会赢奖,快来加入我们吧!

0人参与 去创作
  • 最近
  • 文章
  • 代码仓
  • 资源
  • 问答
  • 帖子
  • 视频
  • 课程
  • 关注/订阅/互动
  • 收藏
搜TA的内容
搜索 取消

C盘回收站已损坏,怎么点击都没反应怎么办

答:

硬盘有坏道了吧!如果有得要资料,还是换一块新盘好一点。

回答问题 2024.10.07

string buf 爆内存的问题

答:

2024.10.07
解决了,是string buf_A[4098]; 这个定量的表是释放不了内存的,导致一直爆增内存,这样写是不正确的。
改好之后是这样,表用向量 vector buf_A; // 使用 vector 来存储数据
临时释放内存用buf_A.clear(); //清空数组释放内存
当程序跑到尾部了,用vector().swap(buf_A); //释放全部占用内存
这样就能正常运行了,跑几天也不会报错了。谢谢大家。

回答问题 2024.10.07

string buf 爆内存的问题

发布问题 2024.10.05 ·
2 回答

爆内存的问题。抛出 bad allocation

答:

代码如下,这些是读数据和判断的。就是在这里会爆内存的。

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <string.h>
#include <time.h>
#include "windows.h"
#include <direct.h>
#include <sys/stat.h>

using namespace std;

//******************************************定义全局变量***********************************************//
string buf_A[2050];                //temp 表缓冲x+2        1024*2+2=2048+2=2050
string buf_B[4098];                //写数据表缓冲x*6        1024*4+2=4096+2=4098    表B大空间可以大大增加命中率
string buf_C[92];                    //返回表缓冲
string buf_cmp = "";                //对比temp
int rev_num = 90;                    //返回表buf_A的数量90左右 少于的话会抛异常 不能有空字符器对比

string a = "访问:        DELETE";    //你要查找的东东
string b = "WriteData";

string e = ".tmp";                    //这三个是程写入生成的 排除掉
string f = ".TMP";
string g = ".bak";

string j = ".smb";
string y = ".DS_Store";
string k = ":com";
string l = ":AFP";

string temp_str = "";                //temp变量
string temp = "";
string line_temp = "";

char i = 0;
int bufB_num = 0;                    //单数总和
int prt = 14;                        //default.ini月份的指针位置是指向14位
long int all_num = 0;                //总数
long int vaild_files = 0;            //有效文件

char* logA_path = "";
bool flag_do = 0;        //有无文件要处理标志位 0代表没有文件 1代表有文件要处理
bool flag_same = 0;        //同名标志位

bool flag_add = 0;        //同名标志位
bool flag_pressin = 0;
//*****************************************************************************************//

void Exedat()    //查检路径打开文件在不在+处理数据
{
    char* logpath = "C:\\Users\\work\\Documents\\visual studio 2017\\Projects\\ConsoleApplication1\\Debug\\09.log";
    ofstream outfile;
    outfile.open(logpath, ios::out | ios::app);

    cout << "scaning . . . dat please waiting . . . !" << endl;  //成功
    fstream file("D:\\Desktop VS\\新建文件夹\\2.txt", ios::in);
    if (!file)
    {
        cout << "Sorry Failed to open file!" << endl;
        file.close();        //关闭读文件流
        system("pause");
        exit(0);            //直接结束
    }
    string line;
    int countA = 0;
    int add = 0;

    //******************************************清空数组********************************************//
    countA = 0;
    memset(buf_A, 0, sizeof(buf_A)); // 清空数组将数组所有元素赋值为0,需要引入string.h头文件

    bufB_num = 0;
    memset(buf_B, 0, sizeof(buf_B)); // 清空数组将数组所有元素赋值为0,需要引入string.h头文件

    memset(buf_C, 0, sizeof(buf_C)); // 清空数组将数组所有元素赋值为0,需要引入string.h头文件

    all_num = 0;                    //只能在下面清空总数
    vaild_files = 0;

    temp_str = "";                    //temp变量
    buf_cmp = "";
    temp = "";
    line_temp = "";
    line = "";
    //**********************************************************************************************//

    while (getline(file, line))
    {
        //        cout << line << endl;    //显示
        buf_A[countA] = line;
        all_num++;
        countA++;
        //--------------------------------------------------------------------------------------//
        //到这里必须清0 不然会死机抛异常 找了三四天才找到
        if (countA >= 2048)        //表A上限2050    如果超值清空数组
        {
            for (int i = 0; i < rev_num; i++)        //压到缓存buf_C里    这里必须返回100行给buf_A 因为清空之后 空字符对比会抛异常退出
            {
                buf_C[i] = buf_A[countA - rev_num + i];
            }

            countA = 0;
            memset(buf_A, 0, sizeof(buf_A)); // 清空数组将数组所有元素赋值为0,需要引入string.h头文件

            for (int i = 0; i < rev_num; i++)        //压回到缓存buf_A里
            {
                buf_A[i] = buf_C[i];
            }
            countA = rev_num;    //返回数量
            memset(buf_C, 0, sizeof(buf_C)); // 清空数组将数组所有元素赋值为0,需要引入string.h头文件
        }
        //--------------------------------------------------------------------------------------//

        //------------------------------------------------------------//  查检是不是写入
        if (line.find(a) != string::npos)
        {
            flag_add = 1;
        }

        if (flag_add == 1)
        {
            add++;
            if (add == 6)
            {
                flag_add = 0;
                add = 0;
                if (line.find(b) != string::npos)
                {
                    flag_pressin = 0;        //压入至0
                }
                else
                {
                    flag_pressin = 1;        //压入至1
                }
            }
        }
        //------------------------------------------------------------------------------------//
        if (flag_pressin == 1)     //查找你定义的字符串 如果找不到就返回-1 string::npos
        {
            //            cout << "Found at position "<< endl;
            flag_pressin = 0;        //压入至0
            flag_same = 0;
            //*****************************************************//
            temp = buf_A[(countA - 10)];
            //如果是空白字符串就跳出这一次 不然会抛异常退出        捕获异常后执行
            try
            {
                temp.erase(0, 1);                            // 清除0前面x个空格字
                temp.erase(13, 1);                            // 从7个字后清除2个空格字
                buf_cmp = temp;
            }
            catch (exception e)
            {
                // 异常处理区域
                cerr << "Capture Exception or Error : " << e.what() << endl;        //捕获异常,或然后程序结束
                outfile << "Capture Exception or Error ! : " << e.what() << " < " << temp << " >" << endl;        //写入log
                continue;    // 异常处理后,程序会继续执行
            }
            //*****************************************************//
            //查找你定义的字符串 如果找不到就返回-1 string::npos
            if ((buf_cmp.find(e) != string::npos) || (buf_cmp.find(f) != string::npos) || (buf_cmp.find(g) != string::npos) || (buf_cmp.find(j) != string::npos) || (buf_cmp.find(y) != string::npos) || (buf_cmp.find(k) != string::npos) || (buf_cmp.find(l) != string::npos))
            {
                flag_same = 1;    //有同名的下面就不干了 排除这几个字
            }
            else
            {
//-----------------------------------------------------------//
                for (int i = 0; i < bufB_num; i++)        //压到缓存里
                {
                    if (buf_B[i].find(buf_cmp) != string::npos)
                    {
                        flag_same = 1;        //有同名的下面就跳出
                        break;
                    }
                }
//-----------------------------------------------------------//
            }
//************************************************************************************************//
            if (flag_same == 0)     //查找你定义的字符串 如果找不到就返回-1 string::npos
            {
                temp_str = buf_A[(countA - 36)];            // 合并两组字符串
                temp_str.erase(0, 2);                        // 清除前面2个空格字
                temp_str = "<" + temp_str + "> ";            //加在一起

                temp = buf_A[(countA - 16)];
                temp.erase(0, 1);                            // 清除0前面x个空格字
                temp.erase(7, 2);                            // 从7个字后清除2个空格字 汉字是两个字符的
                temp_str = temp_str + "<" + temp + "> ";    //加在一起

                temp = buf_A[(countA - 12)];
                temp.erase(0, 1);                            // 清除0前面x个空格字
                temp.erase(9, 2);                            // 从7个字后清除2个空格字
                temp_str = temp_str + "<" + temp + "> ";    //加在一起

                temp = buf_A[(countA - 10)];
                temp.erase(0, 1);                            // 清除0前面x个空格字
                temp.erase(13, 1);                            // 从7个字后清除2个空格字
                temp_str = temp_str + "<" + temp + "> ";    //加在一起

                temp = buf_A[(countA - 6)];
                temp.erase(0, 1);                            // 清除0前面x个空格字
                temp.erase(5, 2);                            // 从7个字后清除2个空格字
                temp_str = temp_str + "<" + temp + ">";        //加在一起
                                                            //                        cout << temp_str << endl;

                buf_B[bufB_num] = temp_str;                    //压到表buf_B
                bufB_num++;                                    //自加        
                vaild_files++;                                //用效文件

//                ofstream outfile;
//                outfile.open(logpath, ios::out | ios::app);
                outfile << temp_str << endl;        //写入    
//                outfile.close();                    //关闭文件流
//**************************************************************//
                if (bufB_num >= 4096)    //表B上限4098    如果超值清空数组
                {
                    bufB_num = 0;
                    memset(buf_B, 0, sizeof(buf_B)); // 清空数组将数组所有元素赋值为0,需要引入string.h头文件
                }
            }
        }
    }
    outfile.close();    //关闭写文件流
                        //******************************输出并写日志************************************//
    cout << "all line countA: " << all_num << endl;
    cout << "filter file countA: " << vaild_files << endl;
    //*********************清空数组***********************//
    countA = 0;
    memset(buf_A, 0, sizeof(buf_A)); // 清空数组将数组所有元素赋值为0,需要引入string.h头文件

    bufB_num = 0;
    memset(buf_B, 0, sizeof(buf_B)); // 清空数组将数组所有元素赋值为0,需要引入string.h头文件

    all_num = 0;                    //只能在下面清空总数
    vaild_files = 0;

    temp_str = "";                    //temp变量
    temp = "";
    line_temp = "";
    line = "";
    //****************************************************//
    file.close();    //关闭读文件流
}
//*********************************************************//
int main()
{
    while(1)
    {
    Exedat();                //处理数据
    }
}

回答问题 2024.10.04

爆内存的问题。抛出 bad allocation

发布问题 2024.10.04 ·
2 回答

使用标准 Windows 库时编译正常 换成静态库中使用 MFC编译报错 LNK2019 无法解析的外部符号 __imp__SHFileOperationA

LNK2019 无法解析的外部符号 __imp__SHFileOperationA@4,该符号在函数 "int __cdecl SHDeleteFolder(char const *)" (?SHDeleteFolder@@YAHPBD@Z) 中被引用。本人菜鸟刚入门,碰到一个SHDeleteFolder外壳函数,在使用。编译正常,也能调试。
原创
发布博客 2023.12.13 ·
177 阅读 ·
1 点赞 ·
2 评论 ·
0 收藏