user-0 提问: 动态申请的结构体指针属性不能释放

回复:
user: user-0
content: 引用 6 楼 starytx 的回复:[quote=引用 5 楼 asdfg48_ 的回复:] 非常感谢您的回答。在结构体中不能这样干啊，比如 typedef struct{  char *RecordData_01[Max_Record_Number][Max_Attribute_Number]; //训练数据  int Record_Number_01;       /*训练数据的行数*/ int  Attribute_Number_01;    /*训练数据的列数*/ }DataNode;   	DataNode  *PreprocessedTrain_Data; 	PreprocessedTrain_Data=(DataNode **)malloc(sizeof(DataNode*));	 	DataPreprocessing(Train_Data,Attribute_Information,PreprocessedTrain_Data,Tree); 编译不通过，在普通变量中二级指针确实无敌于天下，可是在这真不行就是需要传入main中定义的PreprocessedTrain_Data的地址才可以，否则无法改变该指针指向的内容。这样改 void DataPreprocessing(DataNode *Train_Data,Attribute_Node  *Attribute_Information[],DataNode **PreprocessedTrain_Data,DecisionTree &Tree)//训练数据预处理 把函数内部的PreprocessedTrain_Data替换成(*PreprocessedTrain_Data) 调用时DataPreprocessing(Train_Data,Attribute_Information,&PreprocessedTrain_Data,Tree);/*预处理训练数据*/ [/quote]  谢谢版主的回复，我试试

user: user-1
content: 引用 5 楼 asdfg48_ 的回复:非常感谢您的回答。在结构体中不能这样干啊，比如 typedef struct{  char *RecordData_01[Max_Record_Number][Max_Attribute_Number]; //训练数据  int Record_Number_01;       /*训练数据的行数*/ int  Attribute_Number_01;    /*训练数据的列数*/ }DataNode;   	DataNode  *PreprocessedTrain_Data; 	PreprocessedTrain_Data=(DataNode **)malloc(sizeof(DataNode*));	 	DataPreprocessing(Train_Data,Attribute_Information,PreprocessedTrain_Data,Tree); 编译不通过，在普通变量中二级指针确实无敌于天下，可是在这真不行就是需要传入main中定义的PreprocessedTrain_Data的地址才可以，否则无法改变该指针指向的内容。这样改 void DataPreprocessing(DataNode *Train_Data,Attribute_Node  *Attribute_Information[],DataNode **PreprocessedTrain_Data,DecisionTree &Tree)//训练数据预处理 把函数内部的PreprocessedTrain_Data替换成(*PreprocessedTrain_Data) 调用时DataPreprocessing(Train_Data,Attribute_Information,&PreprocessedTrain_Data,Tree);/*预处理训练数据*/

user: user-0
content: 引用 2 楼 u010030181 的回复:改成void DataPreprocessing(DataNode *Train_Data,Attribute_Node  *Attribute_Information[],DataNode **PreprocessedTrain_Data,DecisionTree &Tree)//训练数据预处理 比如说你要改变int a的值，你需要用func(int* temp),你用func(int temp)是改变不了的，你要改变int* a的值，你就需要func(int **temp);  非常感谢您的回答。在结构体中不能这样干啊，比如 typedef struct{  char *RecordData_01[Max_Record_Number][Max_Attribute_Number]; //训练数据  int Record_Number_01;       /*训练数据的行数*/ int  Attribute_Number_01;    /*训练数据的列数*/ }DataNode;   	DataNode  *PreprocessedTrain_Data; 	PreprocessedTrain_Data=(DataNode **)malloc(sizeof(DataNode*));	 	DataPreprocessing(Train_Data,Attribute_Information,PreprocessedTrain_Data,Tree); 编译不通过，在普通变量中二级指针确实无敌于天下，可是在这真不行

user: user-0
content: 引用 1 楼 user-4 的回复:Train_Data在哪定义?具体报错是什么。。  整体代码是封装的DLL，下面是我的结构体定义的部分以及部分代码，基本上动态申请的空间double用红色标注出来了，现在报错的是for(int j=0;j<Train_Data->Attribute_Number;j++){ 		for(int i=0;i<Train_Data->Record_Number;i++){ 			PreprocessedTrain_Data->RecordData[i][j]=(char *)malloc(strlen(Train_Data->RecordData[i][j]) + 1);  提示错误为内存读取受保护，不能读写。但是我觉得错误应该是我在void C4_5_main( )中空间释放的不对，您帮我看看应该怎么改，谢谢 typedef struct Attribute_Node{ 	char  Name[Max_Attribute_Name_Length];             //属性名 	char  kind[Max_Attribute_Name_Length];             //属性类型，如离散型、连续性属性等 	int   Value_Number; 	char  *Value_Name[Max_Record_Number]; 	double Value_Name_Count[Max_Record_Number]; 	double Rntropy; 	double GainRatio; }Attribute_Node; typedef struct{	 	char *RecordData[Max_Record_Number][Max_Attribute_Number]; //训练数据 	int Record_Number;       /*训练数据的行数*/ 	int Attribute_Number;    /*训练数据的列数*/ }DataNode; typedef struct{ 	 char *RecordData_01[Max_Record_Number][Max_Attribute_Number]; //训练数据 	 int Record_Number_01;       /*训练数据的行数*/ 	int  Attribute_Number_01;    /*训练数据的列数*/ }DataNode_01;  typedef	struct DecisionTreeNode{ 	int  keynum;	     char SelectedAttributeName[Max_Attribute_Name_Length];  	char AttributeValue[Max_Attribute_Name_Length];  	int  Record_Marker[Max_Record_Number]; 	int  Attribute_Marker[Max_Attribute_Number]; 	struct DecisionTreeNode  *ptr[Max_Discrete_Value_Number]; }DecisionTreeNode, *DecisionTree;  typedef	struct TreeNode{     char *AttributeName[Max_Attribute_Number];  	char *AttributeValue[Max_Attribute_Number];  	int Top; }TreeNode;  extern"C"_declspec(dllexport)void DataPreprocessing(DataNode *Train_Data,Attribute_Node  *Attribute_Information[],DataNode *PreprocessedTrain_Data,DecisionTree &Tree)//训练数据预处理 { 	char *AttributeValueName[Max_Record_Number]; 	for(int j=0;j<Train_Data->Attribute_Number;j++){ 		for(int i=0;i<Train_Data->Record_Number;i++){ 			PreprocessedTrain_Data->RecordData[i][j]=(char *)malloc(strlen(Train_Data->RecordData[i][j]) + 1); 			strcpy(PreprocessedTrain_Data->RecordData[i][j],Train_Data->RecordData[i][j]); 		} 	}//创建与处理后的训练数据 	PreprocessedTrain_Data->Attribute_Number=Train_Data->Attribute_Number; 	PreprocessedTrain_Data->Record_Number=Train_Data->Record_Number; 	StatisticAttribute_Value(PreprocessedTrain_Data,PreprocessedTrain_Data->Attribute_Number-1,Attribute_Information,Tree->Record_Marker);//统计第Num列各属性取值频率。 	CalculateRntropy(PreprocessedTrain_Data,PreprocessedTrain_Data->Attribute_Number-1,Attribute_Information,Tree->Record_Marker); 	for(int j=0;j<Train_Data->Attribute_Number;j++){ 		if(!strcmp(Attribute_Information[j]->kind,"连续")&&Tree->Attribute_Marker[j]){			 			char *TempChar; 			TempChar=(char *) malloc(sizeof(char)); 			StatisticAttribute_Value(PreprocessedTrain_Data,j,Attribute_Information,Tree->Record_Marker);//统计第Num列各属性取值频率。 			for(int i=0;i<Attribute_Information[j]->Value_Number-1;i++){ 				for(int k=i+1;k<Attribute_Information[j]->Value_Number;k++){ 					if(atof(Attribute_Information[j]->Value_Name[i])>atof(Attribute_Information[j]->Value_Name[k])){ 						strcpy(TempChar,Attribute_Information[j]->Value_Name[i]); 						strcpy(Attribute_Information[j]->Value_Name[i],Attribute_Information[j]->Value_Name[k]); 						strcpy(Attribute_Information[j]->Value_Name[k],TempChar); 					} 				} 			} 			for(int i=0;i<Attribute_Information[j]->Value_Number;i++){ 				AttributeValueName[i]=(char *) malloc(sizeof(char)); 				strcpy(AttributeValueName[i],Attribute_Information[j]->Value_Name[i]); 			} 			int TempNumber=Attribute_Information[j]->Value_Number; 			double GainRatio;int Number; 			for(int  i=0;i<TempNumber-1;i++){				 				for(int k=0;k<Train_Data->Record_Number;k++){ 					if(Tree->Record_Marker[k]){ 						if(atof(Train_Data->RecordData[k][j])>atof(AttributeValueName[i])){ 							strcpy(TempChar,">"); 						} 						else{ 							strcpy(TempChar,"<="); 						} 						strcat(TempChar,Attribute_Information[j]->Value_Name[i]); 						strcpy(PreprocessedTrain_Data->RecordData[k][j],TempChar); 					} 				} 				StatisticAttribute_Value(PreprocessedTrain_Data,j,Attribute_Information,Tree->Record_Marker);//统计第Num列各属性取值频率。 				CalculateRntropy(PreprocessedTrain_Data,j,Attribute_Information,Tree->Record_Marker); 				CalculateGainRatio(PreprocessedTrain_Data,j,Attribute_Information,Tree->Record_Marker); 				if(0==i){ 					Number=i; 					GainRatio=Attribute_Information[j]->GainRatio ; 				} 				else if(i>0&&GainRatio<Attribute_Information[j]->GainRatio){ 					Number=i; 					GainRatio=Attribute_Information[j]->GainRatio; 				} 			} 			for(int k=0;k<Train_Data->Record_Number;k++){ 				if(Tree->Record_Marker[k]){ 					if(atof(Train_Data->RecordData[k][j])>atof(AttributeValueName[Number])){ 						strcpy(TempChar,">"); 					} 					else{ 						strcpy(TempChar,"<="); 					} 					strcat(TempChar,AttributeValueName[Number]); 					strcpy(PreprocessedTrain_Data->RecordData[k][j],TempChar); 				} 			} 			StatisticAttribute_Value(PreprocessedTrain_Data,j,Attribute_Information,Tree->Record_Marker);//统计第Num列各属性取值频率。 			CalculateRntropy(PreprocessedTrain_Data,j,Attribute_Information,Tree->Record_Marker); 			CalculateGainRatio(PreprocessedTrain_Data,j,Attribute_Information,Tree->Record_Marker); 		} 	}//连续属性离散化 }    extern"C"_declspec(dllexport)void C4_5_main(char *A[][6],int s,int p,int *Y)  //DLL入口 { 	Attribute_Node  *Attribute_Information[Max_Attribute_Number]; 	DataNode  *Train_Data; 	Train_Data=(DataNode *)malloc(sizeof(DataNode));	 	FILE *stream1,*stream2;	 	stream1=fopen("C:\\Users\\Cwq\\Documents\\Visual Studio 2012\\Projects\\C4.5_DLL\\C4.5_DLL\\Atribution.txt", "r" ); 	Train_Data->Attribute_Number=0; 	while (!feof(stream1)){ 		Attribute_Information[Train_Data->Attribute_Number]=(Attribute_Node *)malloc(sizeof(Attribute_Node));		 		fscanf(stream1,"%s %s\n",Attribute_Information[Train_Data->Attribute_Number]->Name,Attribute_Information[Train_Data->Attribute_Number]->kind); 		Train_Data->Attribute_Number++; 	} 	fclose(stream1);	 	//Dispay_Attribute_Information(Train_Data,Attribute_Information);//输出属性信息 	stream2=fopen("C:\\Users\\Cwq\\Documents\\Visual Studio 2012\\Projects\\C4.5_DLL\\C4.5_DLL\\trainData01.txt", "r" ); 	Train_Data->Record_Number=0; 	while (!feof(stream2)){ 		for(int i=0;i<Train_Data->Attribute_Number;i++){ 			Train_Data->RecordData[Train_Data->Record_Number][i]=(char *)malloc(sizeof(char)); 			fscanf(stream2,"%s\n",Train_Data->RecordData[Train_Data->Record_Number][i]); 		} 		Train_Data->Record_Number++; 	} 	fclose(stream2);	 	/** 	获取训练数据 	**/ 	DataNode_01  *train_data_01; 	train_data_01=(DataNode_01 *)malloc(sizeof(DataNode_01));  	 for(int i=0;i<s;i++) 	 { 		 for(int j=0;j<p;j++) 		 { 			//PreprocessedTrain_Data->RecordData[i][j]=(char *)malloc(strlen(Train_Data->RecordData[i][j]) + 1); 			//strcpy(PreprocessedTrain_Data->RecordData[i][j],Train_Data->RecordData[i][j]); 			//train_data_01->RecordData_01[i][j]=(char *)malloc(sizeof(char)*10); 			train_data_01->RecordData_01[i][j]=(char *)malloc(strlen(A[i][j])); 		    train_data_01->RecordData_01[i][j]=A[i][j]; 		 } 	 } 	 train_data_01->Attribute_Number_01=p; 	 train_data_01->Record_Number_01=s; 	 //测试训练数据获取完毕  	DecisionTree Tree; 	Tree=(DecisionTree)malloc(sizeof(DecisionTreeNode)); 	for(int i=0;i<Train_Data->Attribute_Number;i++){ 		Tree->Attribute_Marker[i]=1; 	} 	for(int i=0;i<Train_Data->Record_Number;i++){ 		Tree->Record_Marker[i]=1; 	} 	//DispayTrain_Data(Train_Data,Attribute_Information,"输出训练数据",Tree);//输出训练数据 	DataNode  *PreprocessedTrain_Data; 	PreprocessedTrain_Data=(DataNode *)malloc(sizeof(DataNode));	 	DataPreprocessing(Train_Data,Attribute_Information,PreprocessedTrain_Data,Tree);/*预处理训练数据*/ 	//DispayTrain_Data(PreprocessedTrain_Data,Attribute_Information,"输出预处理后的训练数据",Tree);//输出预处理后的训练数据 	char *MaxClass; 	MaxClass=(char *)malloc(sizeof(char)); 	CreateDecisionTree(Tree,PreprocessedTrain_Data,Attribute_Information,MaxClass); 	//printf("决策树预测:\n"); 	StatisticAttribute_Value(Train_Data,Train_Data->Attribute_Number-1,Attribute_Information,Tree->Record_Marker);//统计第Num列各属性取值频率。 	for(int i=0;i<train_data_01->Record_Number_01;i++){ 		ClassSample(Tree,train_data_01,Attribute_Information,i,Y);	 	}  	for(int i=0;i<Train_Data->Attribute_Number;i++)     //释放空间 		free(Attribute_Information[i]);  	free(PreprocessedTrain_Data->RecordData); 	PreprocessedTrain_Data=NULL; 	free(Train_Data); 	Train_Data=NULL;      free(train_data_01->RecordData_01); 	//free(train_data_01); 	train_data_01=NULL;  	free(Tree); 	Tree=NULL; 	free(MaxClass); 	MaxClass=NULL; }

user: user-2
content: 楼主能不能贴一个详细一点的，别人可以编译的例子啊

user: user-3
content: 改成void DataPreprocessing(DataNode *Train_Data,Attribute_Node  *Attribute_Information[],DataNode **PreprocessedTrain_Data,DecisionTree &Tree)//训练数据预处理 比如说你要改变int a的值，你需要用func(int* temp),你用func(int temp)是改变不了的，你要改变int* a的值，你就需要func(int **temp);

user: user-4
content: Train_Data在哪定义?具体报错是什么。。