c++ 精髓

C语言内存管理

c语言开发无非内存越界与内存泄漏两大问题;排查也有规律,得此篇...可弃WinDbg。

#include <fstream>

typedef struct Field
{
	~Field()
	{
		if (key != NULL) { free(key); key = NULL; }
		if (value != NULL) { free(value); value = NULL; }
	}

	char*		key;
	char*		value;
}Field;

//二级指针计数
int ptrCount(void **papszStrList)
{
	int items = 0;
	if (papszStrList)//papszStrList
	{
		while (*papszStrList != NULL)
		{
			items++;
			papszStrList++;
		}
	}

	return items;
}

int freeObj(Field** &items)
{
	if (items == NULL)
		return 1;

	int i = 0;
	for (; items[i] != NULL; i++)
	{
		delete[] items[i]->key; items[i]->key = NULL;
		delete[] items[i]->value; items[i]->value = NULL;
		//delete[] items[i]; items[i] = NULL;
	}

	if (items != NULL)
	{
		delete[] items; items = NULL;
	}

	return 0;
}

int freeLength(Field**items)
{
	if (items == NULL)
		return 1;

	int i = 0;
	for (; items[i] != NULL; i++)
	{
		delete items[i]; items[i] = NULL;
	}

	if (items != NULL)
	{
		items = NULL;
	}

	return 0;
}

int freeArray(void** &items)
{
	if (items == NULL)
		return 1;

	int i = 0;
	for (; items[i] != NULL; i++)
	{
		delete[] items[i]; items[i] = NULL;
	}

	if (items != NULL)
	{
		delete[] items; items = NULL;
	}

	return 0;
}

int frees(void** &items)
{
	if (items == NULL)
		return 1;

	int i = 0;
	for (; items[i] != NULL; i++)
	{
		free(items[i]); items[i] = NULL;
	}

	if (items != NULL)
	{
		free(items); items = NULL;
	}

	return 0;
}

void assignment(Field**&ptr)
{
	int len = ptrCount((void**)ptr);
	for (size_t i = 0; i < len; i++)
	{
		strcpy_s(ptr[i]->key, 64, "123");
		strcpy_s(ptr[i]->value, 64, "abc");
	}
}

void assignments(char**&ptr)
{
	int len = ptrCount((void**)ptr);
	for (size_t i = 0; i < len; i++)
	{
		strcpy_s(ptr[i], 64, "123");
	}
}

int main()
{

	//---------------------------char类型----------------------------//
	char** test1 = new char*[4];
	char** test2 = (char**)calloc(4, sizeof(char*));
	//---------------------------自定义类型----------------------------//
	Field* test6[4];
	Field* test5 = new Field[3];
	//---------------------------自定义指针类型----------------------------//
	Field** test3 = new Field*[4];
	Field** test4 = (Field**)calloc(4, sizeof(Field*));

	size_t i = 0;
	for (; i < 3; i++)
	{
		test1[i] = new char[64];
		*(test2 + i) = (char*)calloc(64, 1);
		//---------------------------自定义类型----------------------------//
		test3[i] = new Field;
		test3[i]->key = new char[64];
		test3[i]->value = new char[64];
		//--------------------语法*(test4 + i)==test4[i]----------------//
		*(test4 + i) = (Field*)calloc(1, sizeof(Field));
		(*(test4 + i))->key = (char*)calloc(64,sizeof(char));
		test4[i]->value = (char*)calloc(64,sizeof(char));

		test5[i].key = new char[64];
		test5[i].value = new char[64];

		test6[i] = new Field;
		test6[i]->key = new char[1024 * 1024];
		test6[i]->value = new char[1024 * 1024];
	}
	test1[i] = NULL;
	test2[i] = NULL;

	test3[i] = NULL;
	i[test4] = NULL;//语法i[test4]==test4[i]

	test6[i] = NULL;

	assignments(test1);
	freeArray((void**&)test1);
	assignments(test2);
	frees((void**&)test2);

	assignment(test3);
	freeObj(test3);
	assignment(test4);
	freeObj(test4);

	delete[]test5;//调用析构。
	freeLength(test6);//调用析构。
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

资深码农

让我们和小姐姐唠嗑可以肢愣起来

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值