c语言中判断申请的动态内存是否被释放了

动态申请内存创建链表,读入一组数据,判断释放与不释放前后的指针所指空间的数据是否一致,一致就是没有释放, 不一致表示已被释放,变为垃圾数据。
代码:没有释放内存时

//头文件区
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"func.h"

typedef struct ListNode
{
    
    
	float val;
	struct ListNode* next;
} Node;

void test_node() {
    
    
	printf("input a height(m):\n");
	float v = 0.0f;
	scanf_s("%f", &v);

	Node* head = (Node*)malloc(sizeof(Node));
	head->val = 0.0f;
	head->next = NULL;
	Node* tail = head;
	Node* cur = head;

	while (v != 0)
	{
    
    
		cur = (Node*)malloc(sizeof(Node));
		cur->val = v;
		cur->next = NULL;//尾巴指针指向空
		tail->next = cur;
		tail = tail->next;
		//printf("cur.val=%f,tail.val=%f",cur->val,tail->val);
		scanf_s("%f", &v);
	}
	cur = NULL;
	printf("现在的tail=%p,*tail = %f\n", tail, tail->val);
	Node* r = head;
	head = head->next;
	printf("the list is:\n");
	while (head != NULL)
	{
    
    
		printf("%.2f\n", head->val);
		head = head->next;
	}

	//Node* tmp;
	//删除链表,释放内存
	printf("现在的r=%p\n", r);
	//while (r) {
    
    
	//	if (r->next == NULL) {
    
    
	//		//还有一个头结点
	//		free(r);
	//		r = NULL;
	//		cur = NULL;
	//	}
	//	else
	//	{
    
    
	//		cur = r->next;//保存下一个结点
	//		free(r);//释放结点
	//		r = cur;//令r等于下一个结点
	//	}
	//}
	printf("现在的r=%p\n", r);
	printf("现在的head=%p\n", head);
	printf("现在的cur=%p\n", cur);
	printf("现在的tail=%p,*tail = %f\n", tail, tail->val);
}

//======================================主函数  用于测试各个函数的功能=================================//
/*******************************************************************************************/
/* 函数名称:main
* 输入参数:无
* 返回参数:无
* 函数功能:主函数*/
/*******************************************************************************************/

int main() {
    
    	
	test_node();
	system("pause");
	return 0;
}
//主函数结束==========================================================================================//

运行结果:
在这里插入图片描述

释放内存的代码,如下:

//头文件区
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"func.h"

typedef struct ListNode
{
    
    
	float val;
	struct ListNode* next;
} Node;

void test_node() {
    
    
	printf("input a height(m):\n");
	float v = 0.0f;
	scanf_s("%f", &v);

	Node* head = (Node*)malloc(sizeof(Node));
	head->val = 0.0f;
	head->next = NULL;
	Node* tail = head;
	Node* cur = head;

	while (v != 0)
	{
    
    
		cur = (Node*)malloc(sizeof(Node));
		cur->val = v;
		cur->next = NULL;//尾巴指针指向空
		tail->next = cur;
		tail = tail->next;
		//printf("cur.val=%f,tail.val=%f",cur->val,tail->val);
		scanf_s("%f", &v);
	}
	cur = NULL;
	printf("现在的tail=%p,*tail = %f\n", tail, tail->val);
	Node* r = head;
	head = head->next;
	printf("the list is:\n");
	while (head != NULL)
	{
    
    
		printf("%.2f\n", head->val);
		head = head->next;
	}

	//Node* tmp;
	//删除链表

	printf("现在的r=%p\n", r);
	printf("删除\n");
	while (r) {
    
    
		if (r->next == NULL) {
    
    
			//还有一个头结点
			free(r);
			r = NULL;
			cur = NULL;
		}
		else
		{
    
    
			cur = r->next;//保存下一个结点
			free(r);//释放结点
			r = cur;//令r等于下一个结点
		}
	}
	printf("现在的r=%p\n", r);
	printf("现在的head=%p\n", head);
	printf("现在的cur=%p\n", cur);
	printf("现在的tail=%p,*tail = %f\n", tail, tail->val);
}

//======================================主函数  用于测试各个函数的功能=================================//
/*******************************************************************************************/
/* 函数名称:main
* 输入参数:无
* 返回参数:无
* 函数功能:主函数*/
/*******************************************************************************************/

int main() {
    
    
	
	//process_grades();
	//process_grades_with_name();
	//process_grades_w_name();
	//test_quick_sort();
	//process_book_ISBN();
	//process_book_ISBN2();

	/*  chpter 2  */
	//extra_qq_num();
	//extra_qq_num_with_struct();
	//test_jiemi();
	//test_huiwen_str();
	//judge_kuohao();
	//judge_kuohao_use_dynamic_mem_stack();
	//card_game_cat_get_fish();
	//create_list_and_show_data();
	//insert_data_with_list(3);
	//insert_data_with_list_pro(3);
	//simulate_list_use_two_arrays();
	
	test_node();
	system("pause");
	return 0;
}
//主函数结束==========================================================================================//

运行结果如下:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/ericanxuanxuan/article/details/112573465