依据网路上教的,确实成功释放内存空间,但是另一个地方,为何第一次成功但第二次又是配不到地址?
程序代码:
#include <stdio.h> #include <windows.h> #define Max_Size 400000 #define Max_Size1 256 const char *delim1=","; const char *delim2="|"; int My_Max_x=0; int My_Max_y=0; int My_Min_x=0; int My_Min_y=0; int ErrorCoord=0; int parts_retested1=0; int parts_retested2=0; struct CoodInfo { int x; int y; int sn; int hw; int sw; int reTest; }; //------------------------------------------------------------------------------------------------------------------// char * delN(char * a) { int l; l=strlen(a); if(a[l-1]==10) a[l-1]='\0'; return a; } //------------------------------------------------------------------------------------------------------------------// int spritcount(char *strIn, const char *aaa)//計算字串中有多少分割符號 { int a = 0; unsigned int i=0; for(i=0; i<strlen(strIn); i++) { if (strIn[i] == aaa[0]) a++; } return(a+1); //符號數量+1=段落 } //------------------------------------------------------------------------------------------------------------------// void strSprit(char *strIn, char **strOut, const char *delim, int nSpritCount) { char *temp; int j=0; temp = strtok(strIn, delim); while( j < nSpritCount ) { strOut[j] = temp; j++; temp = strtok(NULL, delim); } } //------------------------------------------------------------------------------------------------------------------// int CheckCood(int ICCount,CoodInfo *Cood1) { int i=0,x_Max=0,y_Max=0,x_Min=0,y_Min=0; int New_x_Max=0,New_y_Max=0,New_x_Min=0,New_y_Min=0; for (i=0;i<ICCount;i++) { if(i==0) { x_Max=Cood1[i].x;x_Min=Cood1[i].x; y_Max=Cood1[i].y;y_Min=Cood1[i].y; } else { if ((Cood1[i].x>x_Max) || (Cood1[i].y>y_Max)) { if (Cood1[i].x>x_Max) x_Max=Cood1[i].x; if (Cood1[i].y>y_Max) y_Max=Cood1[i].y; } else if ((Cood1[i].x<x_Min) || (Cood1[i].y<y_Min)) { if (Cood1[i].x<x_Min) x_Min=Cood1[i].x; if (Cood1[i].y<y_Min) y_Min=Cood1[i].y; } } } New_x_Max=((x_Max/10)+1)*10;New_x_Min=((x_Min/10)-1)*10; New_y_Max=((y_Max/10)+1)*10;New_y_Min=((y_Min/10)-1)*10; My_Max_x=New_x_Max;My_Min_x=New_x_Min; My_Max_y=New_y_Max;My_Min_y=New_y_Min; if ((x_Min<0)||(y_Min<0)||(x_Min>1000)||(y_Min>1000)) return -1; else return 1; } //------------------------------------------------------------------------------------------------------------------// int OutBinMap(char *FileName,char *TargetFile1,char *TargetFile2,int ICCount) { FILE *stream1; char Temp[Max_Size]={0},CodeString[Max_Size]={0}; int i=0,j=0,k=0,ii=0,jj=0,len=0,Title=0,Paragraph=0; CoodInfo *Cood = (CoodInfo*)calloc(ICCount,sizeof(CoodInfo)); if(Cood==NULL) return 0; //------------------------------------------------------------------------------------------收集資料 stream1=fopen(FileName,"r"); while(fgets(Temp, Max_Size, stream1)!=NULL) { ii++; if (ii==1) {} if (ii==2) {} if (ii==3) { char *temp=""; char **part = (char**)malloc(sizeof(char*)*ICCount); if(part==NULL) return 0; for(i=0;i<ICCount;i++) part[i] = (char*)malloc(sizeof(char)*Max_Size1); strcpy(CodeString,delN(Temp)); len=strlen(CodeString); Title = spritcount(CodeString, delim1); //依delim1分段落 temp = strtok(CodeString, delim1); //第一次分割(part[0],part[1]) while( j < ICCount ) { sprintf(part[j] ,"%s", temp); j++; temp = strtok(NULL, delim1); } temp="";j=0; char *part1[5]={0}; for (i=1;i<ICCount;i++) { Paragraph = spritcount(part[i], delim2); //依delim2分段落 strSprit(part[i], part1, delim2, Paragraph); //第二次分割(part[0]....part[Paragraph]) if((part1[0])&&(part1[1])&&(part1[2])) { Cood[i-1].x = atoi(part1[0]); Cood[i-1].y = atoi(part1[1]); Cood[i-1].sn = atoi(part1[2]); } } for(i=0;i<Max_Size1;i++) free(part[i]); free(part); part=NULL; } else if (ii==4) { char *temp=""; char **part = (char**)malloc(sizeof(char*)*ICCount); if(part==NULL) return 0; for(i=0;i<ICCount;i++) part[i] = (char*)malloc(sizeof(char)*Max_Size1); strcpy(CodeString,delN(Temp)); len=strlen(CodeString); Title = spritcount(CodeString, delim1); //依delim1分段落 temp = strtok(CodeString, delim1); //第一次分割(part[0],part[1]) while( j < ICCount ) { sprintf(part[j] ,"%s", temp); j++; temp = strtok(NULL, delim1); } temp="";j=0; char *part1[5]={0}; for (i=1;i<ICCount;i++) { Paragraph = spritcount(part[1], delim2); //依delim2分段落 strSprit(part[i], part1, delim2, Paragraph); //第二次分割(part[0]....part[Paragraph]) if((part1[0])&&(part1[1])) { Cood[i-1].hw = atoi(part1[0]); Cood[i-1].sn = atoi(part1[1]); } } for(i=0;i<Max_Size1;i++) free(part[i]); free(part); part=NULL; } else if (ii>4) break; memset(CodeString,0,sizeof(CodeString)); memset(Temp,0,sizeof(Temp)); } fclose(stream1); //------------------------------------------------------------------------------------------ if(CheckCood(ICCount,Cood)==1) { char ***NewCood = NULL; NewCood = (char***)malloc(sizeof(*NewCood)*My_Max_x); if(NewCood==NULL) return 0; for(i=(My_Max_x-1);i>=0;--i) { NewCood[i] = (char**)malloc(sizeof(*NewCood[i])*My_Max_y); for(j=(My_Max_y-1);j>=0;--j) NewCood[i][j] = (char*)malloc(sizeof(*NewCood[i][j])*3); } for(i=(My_Max_x-1);i>=0;--i) for(j=(My_Max_y-1);j>=0;--j) for(k=(3-1);k>=0;--k) NewCood[i][j][k]='\0'; for(i=0;i<ICCount;i++) { char tmp1[2]={0}; itoa(Cood[i].hw,tmp1,10); if (strlen(NewCood[Cood[i].x][Cood[i].y])==0) Cood[i].reTest = 0; else { Cood[i].reTest = 1; parts_retested1++; } strcpy(NewCood[Cood[i].x][Cood[i].y],tmp1) ; } stream1 = fopen(TargetFile1, "a+" );//HW for(int num0=0;num0<3;num0++) { fprintf(stream1,","); for(int num1=0;num1<My_Max_x;num1++) { if (num0==0) fprintf(stream1,"0,"); else if (num0==1) { int a=num1/10; fprintf(stream1,"%d,",a); } else if (num0==2) { int a=num1%10; fprintf(stream1,"%d,",a); } } fprintf(stream1,"\n"); } char *MyString1=NULL; MyString1 = (char*)malloc(sizeof(*MyString1)*(My_Max_x+5)); if(MyString1==NULL) return 0; for(i=0;i<My_Max_y;i++) { jj=0; memset(MyString1,0,sizeof(MyString1)); for(int ii=0;ii<My_Max_x;ii++) { if(strcmp(NewCood[ii][i],"000")>0) jj+=sprintf(MyString1+jj,"%s,",NewCood[ii][i]); else jj+=sprintf(MyString1+jj,","); } fprintf(stream1,"%d,%s\n",i,MyString1); } fprintf(stream1,",\n,\n,\n"); MyString1=NULL; //ReTest IC for(i=0;i<ICCount;i++) { if (i==0) fprintf(stream1,"%s,%s,%s,%s\n","X","Y","Site","HW"); if (Cood[i].reTest ==1) fprintf(stream1,"%d,%d,%d,%d\n",Cood[i].x,Cood[i].y,Cood[i].sn,Cood[i].hw); } fclose(stream1); for(i=0;My_Max_x>i;++i) { for(j=0;My_Max_y>j;++j) { free(NewCood[i][j]); NewCood[i][j]=NULL; } free(NewCood[i]); NewCood[i]=NULL; } free(NewCood); NewCood = NULL; } else return 0; //------------------------------------------------------------------------------------------ if(CheckCood(ICCount,Cood)==1) { char ***NewCood = NULL; NewCood = (char***)malloc(sizeof(*NewCood)*My_Max_x); if(NewCood==NULL) return 0; for(i=(My_Max_x-1);i>=0;--i) { NewCood[i] = (char**)malloc(sizeof(*NewCood[i])*My_Max_y); for(j=(My_Max_y-1);j>=0;--j) NewCood[i][j] = (char*)malloc(sizeof(*NewCood[i][j])*3); } for(i=(My_Max_x-1);i>=0;--i) for(j=(My_Max_y-1);j>=0;--j) for(k=(3-1);k>=0;--k) NewCood[i][j][k]='\0'; for(i=0;i<ICCount;i++) { char tmp1[2]={0}; itoa(Cood[i].sw,tmp1,10); if (strlen(NewCood[Cood[i].x][Cood[i].y])==0) Cood[i].reTest = 0; else { Cood[i].reTest = 1; parts_retested2++; } strcpy(NewCood[Cood[i].x][Cood[i].y],tmp1) ; } stream1 = fopen(TargetFile2, "a+" );//SW for(int num0=0;num0<3;num0++) { fprintf(stream1,","); for(int num1=0;num1<My_Max_x;num1++) { if (num0==0) fprintf(stream1,"0,"); else if (num0==1) { int a=num1/10; fprintf(stream1,"%d,",a); } else if (num0==2) { int a=num1%10; fprintf(stream1,"%d,",a); } } fprintf(stream1,"\n"); } char *MyString1=NULL;jj=0; MyString1 = (char*)malloc(sizeof(*MyString1)*(My_Max_x+5)); if(MyString1==NULL) return 0; for(i=0;i<My_Max_y;i++) { jj=0; memset(MyString1,0,sizeof(MyString1)); for(int ii=0;ii<My_Max_x;ii++) { if(strcmp(NewCood[ii][i],"000")>0) jj+=sprintf(MyString1+jj,"%s,",NewCood[ii][i]); else jj+=sprintf(MyString1+jj,","); } fprintf(stream1,"%d,%s\n",i,MyString1); } fprintf(stream1,",\n,\n,\n"); MyString1=NULL; //ReTest IC for(i=0;i<ICCount;i++) { if (i==0) fprintf(stream1,"%s,%s,%s,%s\n","X","Y","Site","SW"); if (Cood[i].reTest ==1) fprintf(stream1,"%d,%d,%d,%d\n",Cood[i].x,Cood[i].y,Cood[i].sn,Cood[i].sw); } fclose(stream1); for(i=0;My_Max_x>i;++i) { for(j=0;My_Max_y>j;++j) { free(NewCood[i][j]); NewCood[i][j]=NULL; } free(NewCood[i]); NewCood[i]=NULL; } free(NewCood); NewCood = NULL; } else return 0; free(Cood); Cood=NULL; return 1; } void main(int argc, char* argv[]) { int A=OutBinMap("C:\\TMB\\Transform\\AAAA.csv","C:\\TMB\\Transform\\HW.csv","C:\\TMB\\Transform\\SW.csv",12355); char B[1]={0}; itoa(A,B,10); MessageBox(NULL,B,"Test Result",MB_OK); }
不要選我當版主