1. The address book into a dynamic version 2. Exercise Dynamic Memory opens malloc calloc realloc

1. The address book into a dynamic version of
the header file

#ifndef _CONTACT_H_
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
enum Option
{
	EXIT,
	ADD,
	SEAR,
	DEL,
	SHOW,
	CLEAR,
	SORT,
	DES
};
#define MAX_NAME 20
#define MAX_TEL 11
#define MAX_ADDR 15
#define MAX_SEX 3
#define MAX_NUMPERSON 1000
#define DEFAULT_SIZE 2;
#define MAX_STR 20
typedef struct Personlnfo
{
	char name[MAX_NAME];
	short age;
	char tele[MAX_TEL];
	char addr[MAX_ADDR];
	char sex[MAX_SEX];
}Personlnfo;
//通讯录
typedef struct Contact
{
	//Personlnfo per[MAX_NUMPERSON];
	Personlnfo *per;
	int usedSize;//被使用的个数
	int capticty;
}Contact;
void InitContact(Contact *pCon);
void AddContact(Contact *pCon);
int SearchContact(Contact *pCon);
void DelContact(Contact *pCon);
void ShowContact(Contact *pCon);
void ClearContact(Contact *pCon);
void DestoryContact(Contact *pCon);
#endif

contact .c file

#define _CRT_SECURE_NO_WARNINGS 1
#include"Contact.h"
void InitContact(Contact *pCon)
{
	assert(pCon != NULL);
	//pCon->usedSize = 0;
	//对数组进行初始化
	//memset(pCon->per, 0, sizeof(pCon->per));
	pCon->capticty = DEFAULT_SIZE;
	pCon->usedSize = 0;
	pCon->per = (Personlnfo *)malloc(sizeof(Personlnfo)* pCon->capticty);
	assert(pCon->per != NULL);
	memset(pCon->per, 0, sizeof(Personlnfo)* pCon->capticty);

}
//添加一个人物到通讯录
static int CheckFull(Contact *pCon)
{
	if (pCon->usedSize == pCon->capticty)
	{
		Personlnfo *ptr = (Personlnfo*)realloc(pCon->per, pCon->capticty*sizeof(Personlnfo)* 2);
		if (ptr != NULL)
		{
			pCon->per = ptr;
			pCon->capticty *= 2;
			printf("增容成功\n");
			return 1;
		}
		else
		{
			return 0;
		}
	}
	return 1;
}
void AddContact(Contact *pCon)
{
	/*if (pCon->usedSize == MAX_NUMPERSON)
	{
		printf("不好意思,通讯录满了\n");
		return;
	}*/
	int ret = CheckFull(pCon);
	if (ret == 0)
	{
		printf("扩容失败\n");
		return;
	}
	printf("请输入姓名;");
	scanf("%s", pCon->per[pCon->usedSize].name);
	printf("请输入性别;");
	scanf("%s", pCon->per[pCon->usedSize].sex);
	printf("请输入年龄;");
	scanf("%d", &(pCon->per[pCon->usedSize].age));
	printf("请输入电话;");
	scanf("%s", pCon->per[pCon->usedSize].tele);
	printf("请输入地址;");
	scanf("%s", pCon->per[pCon->usedSize].addr);
	pCon->usedSize++;
	printf("添加成功\n");
}
int SearchContact(Contact *pCon)
{
	int i = 0;
	char name[MAX_NAME] = { 0 };
	assert(pCon != NULL);
	if (pCon->usedSize == 0)
	{
		printf("通讯录为空");
		return -1;
	}
	printf("请输入姓名\n");
	scanf("%s", name);
	for (i = 0; i < pCon->usedSize; i++)
	{
		if (strcmp(pCon->per[i].name, name) == 0)
			return 1;
		else
			return -1;
	}
}
void DelContact(Contact *pCon)
{
	int index = SearchContact(pCon);
	int i = 0;
	assert(pCon != NULL);
	if (index == -1)
	{
		printf("查无此人\n");
		return;
	}
	for (i = index; i <pCon->usedSize - 1; i++)
	{
		pCon->per[i] = pCon->per[i + 1];
	}
	pCon->usedSize--;
	printf("删除成功\n");
}
void ShowContact(Contact *pCon)
{
	int i = 0;
	assert(pCon != NULL);
	printf("%-20s %-3s %-10s %-11s %-15s\n", "姓名", "性别", "年龄", "电话", "地址");
	for (i = 0; i < pCon->usedSize; i++)
	{
		printf("%-20s %-3s %-10d %-11s %-15s\n", pCon->per[i].name, 
			pCon->per[i].sex, pCon->per[i].age, pCon->per[i].tele, pCon->per[i].addr);
	}
}
void ClearContact(Contact *pCon)
{
	//pCon->usedSize = 0;
	InitContact(pCon);
}
void DestoryContact(Contact *pCon)
{
	assert(pCon != NULL);
	free(pCon->per);
	pCon->per = NULL;
	pCon->capticty = 0;
	pCon->usedSize = 0;
}

test.c file

#define _CRT_SECURE_NO_WARNINGS 1
#include"Contact.h"
void menu()
{
	printf("********1.add**********2.search*********\n");
	printf("********3.del**********4.show***********\n");
	printf("********5.clear********6.sort***********\n");
	printf("********0.exit*********7.Destory********\n");
}
void start()
{
	int input = 0;
	//初始化通讯录  数组 
	Contact con;
	InitContact(&con);
	do
	{
		menu();
		printf("请输入你的操作>>");
		scanf("%d", &input);
		switch (input)
		{
		case ADD:
			AddContact(&con);
			break;
		case SEAR:
			SearchContact(&con);
			break;
		case DEL:
			DelContact(&con);
			break;
		case SHOW:
			ShowContact(&con);
			break;
		case CLEAR:
			ClearContact(&con);
			break;
		case DES:
			DestoryContact(&con);
			break;
		}
	} while (input);
}
int main()
{
	start();
	return 0;
}

2. Exercise dynamic memory opened
malloc

to apply a memory space continuously available, and returns a pointer pointing to this space.
Open success, returns a pointer pointing to open up good space, otherwise it returns a NULL pointer.

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
//void* malloc(size_t size)
int main()
{
	int arr[10] = { 0 };//连续的 栈
	int*p = (int*)malloc(sizeof(int)* 10);//连续的 参数:40个字节 堆
	assert(p != NULL);
	/*if(p==NULL)
	{
		printf("%s\n", strerror(errno));
	}*/
	free(p);//释放所指向的动态内存  不free会内存泄漏
	p = NULL;//野指针
	system("pause");
	return 0;
}

calloc
* function-element is open up a space for the size of num sizes, and the space is initialized to 0. Each byte
* malloc function differs only in that calloc will apply to each word space before the return address section initialized to zero.

#include<stdio.h>
#include<stdlib.h>
//void* calloc(size_t num, size_t size)
int main()
{
	int*p = calloc(10, sizeof(int));
	if(p==NULL)
	{
		printf("%s\n", strerror(errno));
	}
	free(p);
	p = NULL;
	return 0;
}

realloc
relloc memory function can open up resize dynamic, dynamic memory management allows more flexibility.
Original enough space: large expansion memory is added directly after the original memory space;
original space is not large enough: find another suitable size in contiguous space on the heap space is used, the function returns a new memory address.

#include<stdio.h>
#include<stdlib.h>
void* realloc(void* ptr, size_t size)
{
	int*p =(int*) realloc(p, sizeof(int)* 10 * 2);//2倍扩容
	assert(p != NULL);
	free(p);
	p = NULL;
	return 0;
}

free function
to release dynamic memory open
* If the parameter is not dynamic ptr points to open up space, that behavior is undefined free function
* If the parameter is free NULL pointer, the function does not do anything.

Published 60 original articles · won praise 23 · views 3337

Guess you like

Origin blog.csdn.net/weixin_44945537/article/details/94402342