修复expand函数中无法malloc的bug:

原因是free的时候没有消除指针悬挂
This commit is contained in:
Qiea
2024-11-11 17:34:50 +08:00
parent f940758d87
commit a6adf831b3
3 changed files with 38 additions and 14 deletions

26
cnn.c
View File

@@ -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]);
}
}

View File

@@ -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;
}

24
main.c
View File

@@ -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结束");
}
}