一个奇怪的free的应用

来源:互联网 发布:汤姆索亚历险记 知乎 编辑:程序博客网 时间:2024/05/01 12:56
#include <stdio.h> #include <malloc.h>#include <string.h>#include <stdarg.h>struct str{char *name;int age;char *like;struct str *next;};   int dcli_free_single_link_list_L1(const int pointer_num, void *first_elem, /*void *next_ptr,*/ ...){va_list ap;int _ptr_num = (int)pointer_num;unsigned int *distance = NULL;distance = malloc((pointer_num + 1) * sizeof(unsigned int));if (distance == NULL){return -1;}void *_pre_elem = NULL;void *_next_elem = NULL;memset(distance, 0, sizeof(unsigned int) * (pointer_num + 1));va_start(ap, first_elem);int len = 0;_pre_elem = va_arg(ap, void *);len = (_pre_elem > first_elem) ? (_pre_elem - first_elem) : (first_elem - _pre_elem);//memcpy(&(distance[pointer_num - _ptr_num]), &len, 4);distance[pointer_num-_ptr_num] = len;_ptr_num--;while (_ptr_num >= 0){_pre_elem = va_arg(ap, void *);len = _pre_elem - first_elem;if (len < 0)len = -len;//memcpy(/*ptr_each_elem*/&(distance[pointer_num - _ptr_num]), &len, 4);distance[pointer_num-_ptr_num] = len;len  = 0;_ptr_num--;}va_end(ap);_pre_elem = _next_elem = first_elem;while (_pre_elem != NULL){if ((((void *)_pre_elem) + distance[0]) != NULL)_next_elem = *(int *)(((void *)_pre_elem) + distance[0]);_ptr_num = (int)pointer_num;while (_ptr_num > 0){if (((void *)_pre_elem + distance[pointer_num + 1 - _ptr_num]) != NULL){typeof(_pre_elem + distance[pointer_num + 1 - _ptr_num]) ptr_tmp;//ptr_tmp = (typeof(_pre_elem + distance[pointer_num + 1 - _ptr_num]))(_pre_elem + distance[pointer_num + 1 - _ptr_num]);//free((typeof(_pre_elem + distance[pointer_num + 1 - _ptr_num]))(*((int *)ptr_tmp)));ptr_tmp = (typeof(ptr_tmp))(_pre_elem + distance[pointer_num+1-_ptr_num]);printf("%s\n", (typeof(ptr_tmp))(*((int *)ptr_tmp)));free((typeof(ptr_tmp))((*((int *)ptr_tmp))));//free((void *)_pre_elem + distance[pointer_num + 1 - _ptr_num]);}_ptr_num--;}free(_pre_elem);_pre_elem = _next_elem;}free(distance);return pointer_num;}void create_link_list(struct str **first_elem){char c = 0;struct str *ele = NULL;while(1){printf("Do you want to continue?(y/n):");c = getchar();getchar();if (c == 'n')break;ele = (struct str *)malloc(sizeof(struct str));if (ele == NULL)break;printf("\tplease input the name: ");ele->name = (char *)malloc(256);memset(ele->name, 0, 256);gets(ele->name);printf("\tplease input the age: ");scanf("%d", &(ele->age));getchar();printf("\tplease input it's like: ");ele->like = (char *)malloc(256);memset(ele->like, 0, 256);gets(ele->like);ele->next = NULL;if (*first_elem == NULL)*first_elem = ele;else{ele->next = (*first_elem);(*first_elem) = ele;}ele = NULL;}ele = *first_elem;while (ele != NULL){printf("----------\n");printf("\tname is: %s\n", ele->name);printf("\tage is: %d\n", ele->age);printf("\tit's like is: %s\n", ele->like);printf("----------\n");if (ele->next != NULL){ele = ele->next;}else break;}}int main(){struct str *student = NULL;create_link_list(&student);dcli_free_single_link_list_L1(2, student, &(student->next), &(student->name), &(student->like));}


	
				
		
原创粉丝点击