From a6adf831b3db4f698b3edc12b26f86a443076f6c Mon Sep 17 00:00:00 2001 From: Qiea <1310371422@qq.com> Date: Mon, 11 Nov 2024 17:34:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dexpand=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E4=B8=AD=E6=97=A0=E6=B3=95malloc=E7=9A=84bug=EF=BC=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原因是free的时候没有消除指针悬挂 --- cnn.c | 26 +++++++++++++++++--------- cnn_model.c | 2 +- main.c | 24 ++++++++++++++++++++---- 3 files changed, 38 insertions(+), 14 deletions(-) diff --git a/cnn.c b/cnn.c index b2d7a54..3300878 100644 --- a/cnn.c +++ b/cnn.c @@ -17,11 +17,6 @@ void print_rslt(float* rslt, u8 input_matrix_length, u32 length){ // 将原始矩阵复制到填充后的矩阵中央 float* expand(const float* old_matrix, int old_matrix_length, int layer){ float* new_matrix = (float *)malloc(sizeof(float)*layer*(old_matrix_length+2)*(old_matrix_length+2)); - if (new_matrix == NULL) { - // 处理 malloc 失败的情况,例如打印错误并退出 - perror("Memory allocation failed"); - exit(1); // 退出程序 - } memset(new_matrix, 0, sizeof(float)*layer*(old_matrix_length+2)*(old_matrix_length+2)); for(int l=0; l < layer; l++){ for (int i = 0; i < old_matrix_length; i++) { @@ -272,42 +267,55 @@ void cnn_run(){ float value[3] = {0}; calculate_statistics(data,&value[0]); if (check_threshold(data,&value[0])){ - float* _data = generateMatrix(data,&value[0]); -// printf("最大值:%f 平均值:%f 标准差:%f\r\n",value[0],value[1],value[2]); -// print_rslt(_data,100,1*100*100); - //第一层:填充102 * 102 float* expand_matrix_1 = expand(_data, 100, 1); float* conv_rlst_1 = convolution(conv1_weight,conv1_bias,expand_matrix_1, 102); float* pool_rslt_1 = pooling(conv1_weight, conv_rlst_1, 100); free(_data); + _data = NULL; free(expand_matrix_1); + expand_matrix_1 = NULL; free(conv_rlst_1); + conv_rlst_1 = NULL; + //第二层:填充32 * 52 * 52 float* expand_matrix_2 = expand(pool_rslt_1, 50, 32); float* conv_rlst_2 = convolution(conv2_weight,conv2_bias,expand_matrix_2, 52); float* pool_rslt_2 = pooling(conv2_weight, conv_rlst_2, 50); free(pool_rslt_1); + pool_rslt_1 = NULL; free(expand_matrix_2); + expand_matrix_2 = NULL; free(conv_rlst_2); + conv_rlst_2 = NULL; + //第三层:填充 64 * 27 * 27 float* expand_matrix_3 = expand(pool_rslt_2, 25, 64); float* conv_rlst_3 = convolution(conv3_weight,conv3_bias,expand_matrix_3, 27); float* pool_rslt_3 = pooling(conv3_weight, conv_rlst_3, 25); free(pool_rslt_2); + pool_rslt_2 = NULL; free(expand_matrix_3); + expand_matrix_3 = NULL; free(conv_rlst_3); + conv_rlst_3 = NULL; float* affine1_rslt = hidden(pool_rslt_3); float* affine2_rslt = output(affine1_rslt); free(pool_rslt_3); + pool_rslt_3 = NULL; free(affine1_rslt); + affine1_rslt = NULL; free(affine2_rslt); + affine2_rslt = NULL; + + } else{ + printf("未放电!最大值:%f 平均值:%f 标准差:%f\r\n",value[0],value[1],value[2]); } } diff --git a/cnn_model.c b/cnn_model.c index 5bf3e42..df9108b 100644 --- a/cnn_model.c +++ b/cnn_model.c @@ -283,7 +283,7 @@ u8 model_switchdata(char* data_name){ DEBUG_PRINTF("Data数据集[%s]切换失败!!\r\n",data_name); return 0; } - else printf("Data数据集[%s]切换成功!\r\n",data_name); + else DEBUG_PRINTF("Data数据集[%s]切换成功!\r\n",data_name); DEBUG_PRINTF("data_name的长度为:%d\r\n_path的长度为:%d\r\n_path为:%s\r\n",_len,sizeof(_path),_path); return 1; } diff --git a/main.c b/main.c index fe4d15a..86870a5 100644 --- a/main.c +++ b/main.c @@ -6,8 +6,9 @@ #include "cnn.h" + void run_dataset(){ - char* modelusearr[] = { + char* modelusearr_origin[] = { "C1autosave00095_right_new_2", "C1autosave00096_right_new_2", "C1autosave00097_right_new_2", @@ -69,6 +70,23 @@ void run_dataset(){ "filtered_C1autosave00039_right_new", "filtered_C1autosave00062_right_new", }; + char* modelusearr_out[] = { + "split_1", + "split_2", + "split_3", + "split_4", + "split_5", + "split_6", + "split_7", + "split_8", + "split_9", + "split_10", + }; +/* 可用数据集如下 + * modelusearr_origin + * modelusearr_out +*/ + #define modelusearr modelusearr_origin for(int a=0;a<(sizeof(modelusearr) / sizeof(modelusearr[0]));a++){ SDRAM_USED(); model_switchdata(modelusearr[a]); @@ -83,8 +101,6 @@ int main(){ model_write("all"); run_dataset(); -// model_switchdata("C1autosave00095_right_new_2"); -// cnn_run(); DEBUG_PRINTF("\r\nEnd结束"); -} \ No newline at end of file +}