指针释放之结构体

阅读更多
typedef struct{
        uint len;
        uchar *s;
}nint;

typedef struct{
	xtime_t	log_time[16];
	uchar	log_uchar[16];
	ushort	log_ushort[16];
	uint	log_uint[16];
	uint	log_addr[16];
	ulong	log_ulong[16];
	nint	log_nint[32];
}LOG;

typedef struct{
	int code;
	LOG *log;
	long total;
	char type;
}QueryChat;


queryChat->log = (LOG *)malloc(sizeof(LOG)*gettotal);
memset(queryChat->log,0,sizeof(LOG)*gettotal);


for (j = 0; j < 32; j++){
	if (fread(&((queryChat->log+i)->log_nint[j].len), sizeof(int), 1, fp) != 1){
		goto _FAIL_;
	}
	if ((queryChat->log+i)->log_nint[j].len){
		if (((queryChat->log+i)->log_nint[j].s = (u_char*)malloc((queryChat->log+i)->log_nint[j].len + 1)) != NULL){
			if (fread((queryChat->log+i)->log_nint[j].s, (queryChat->log+i)->log_nint[j].len, 1, fp) != 1){
				goto _FAIL_;
			} else {
				(queryChat->log+i)->log_nint[j].s[(queryChat->log+i)->log_nint[j].len] = 0;
			}
		}
	}
}

int j=0,k=getnum;
if(NULL != queryChat){	
	for(i=0;ilog+i){
			for(j=0;j<32;j++){
				if((queryChat->log+i)->log_nint[j].s){
					free((queryChat->log+i)->log_nint[j].s);
					(queryChat->log+i)->log_nint[j].s = NULL;
				}
			}

		}
	free(queryChat->log);	
	free(queryChat);
	queryChat = NULL;			
	}
}

你可能感兴趣的:(c)