void *malloc(size_t size);
功能:申请堆区空间
参数:size:申请堆区空间的大小
返回值:返回获得的空间的首地址,失败返回NULL
void free(void *ptr);
功能:释放堆区空间
注意:
1.free只能释放堆区空间
2. 一个空间只能被free一次,对此free程序会崩溃
3. malloc需要和free搭配使用,如果只有malloc没有free会内存泄露
练习:要求申请堆区空间,将“hello world”存放到堆区空间,完成打印
#include
#include
#include
int main(void)
{
char *p = NULL;
p = malloc(16);
if(p == NULL)
{
printf("failed\n");
return -1;
}
strcpy(p, "Hello World");
printf("%s\n",p);
free(p);
return 0;
}
内存溢出也称为内存越界
程序中malloc的空间没有被释放
由于频繁申请和释放,导致连续的空间分散成一些小的碎片空间,当malloc超过碎片空间时,则无法获得该空间(空间不连续),将这样的空间称为内存碎片
typedef int DataType;
typedef struct node
{
DataType Data;
struct node *pNext;
}LinkNode;
LinkNode *CreateLinkList(void)
{
LinkNode *pTmpNode = NULL;
pTmpNode = malloc(sizeof(LinkNode));
if(pTmpNode == NULL)
{
return NULL;
}
pTmpNode->pNext = NULL;
return pTmpNode;
}
int InsertHeadLinkList(LinkNode * phead, DataType TmpData)
{
LinkNode *pTmpNode = NULL;
pTmpNode = malloc(sizeof(LinkNode));
if(pTmpNode == NULL)
{
return -1;
}
pTmpNode->Data = TmpData;
pTmpNode->pNext = phead->pNext;
phead->pNext = pTmpNode;
return 0;
}
int ShowLinkList(LinkNode *phead)
{
LinkNode *pTmpNode = NULL;
pTmpNode = phead->pNext;
while(pTmpNode != NULL)
{
printf("%d ",pTmpNode->Data);
pTmpNode = pTmpNode->pNext;
}
printf("\n");
return 0;
}
int ReplaceData(LinkNode *phead, int OldData, int NewData)
{
LinkNode *pTmpNode = NULL;
pTmpNode = phead->pNext;
while(pTmpNode != NULL)
{
if(pTmpNode->Data == OldData)
{
pTmpNode->Data = NewData;
}
pTmpNode = pTmpNode->pNext;
}
return 0;
}
int DeleteLinkList(LinkNode *phead, DataType DeleteData)
{
LinkNode *pPreNode = NULL;
LinkNode *pTmpNode = NULL;
pTmpNode = phead->pNext;
pPreNode = phead;
while(pTmpNode != NULL)
{
if(pTmpNode->Data == DeleteData)
{
pPreNode->pNext = pTmpNode->pNext;
free(pTmpNode);
pTmpNode = pPreNode->pNext;
}
else
{
pTmpNode = pTmpNode->pNext;
pPreNode = pPreNode->pNext;
}
}
}
int DestoryLinkList(LinkNode **pphead)
{
LinkNode *pTmpNode = NULL;
LinkNode *pFreeNode = NULL;
pTmpNode = pFreeNode = *pphead;
while(pTmpNode != NULL)
{
pTmpNode = pTmpNode->pNext;
free(pFreeNode);
pFreeNode = pTmpNode;
}
*pphead = NULL;
return 0;
}
1. 封装函数在链表中实现尾插法
#include
#include
typedef int DataType;
typedef struct node
{
DataType Data;
struct node *pNext;
}LinkList;
LinkList *CreatLinkList()
{
LinkList *pTmpNode = NULL;
pTmpNode = malloc(sizeof(LinkList));
if(pTmpNode == NULL)
{
return NULL;
}
pTmpNode->pNext = NULL;
return pTmpNode;
}
int InsertTailLinkList(LinkList *pHead, DataType NewData)
{
LinkList *pTmpNode = NULL;
LinkList *pPotNode = NULL;
pPotNode = pHead;
pTmpNode = malloc(sizeof(LinkList));
if(pTmpNode == NULL)
{
return -1;
}
pTmpNode->Data = NewData;
pTmpNode->pNext = NULL;
while(pPotNode->pNext != NULL)
{
pPotNode = pPotNode->pNext;
}
pPotNode->pNext = pTmpNode;
return 0;
}
int ShowLinkList(LinkList *pHead)
{
LinkList *pTmpNode = NULL;
pTmpNode = pHead->pNext;
while(pTmpNode != NULL)
{
printf("%d ", pTmpNode->Data);
pTmpNode = pTmpNode->pNext;
}
printf("\n");
return 0;
}
int DestoryLinkList(LinkList **pphead)
{
LinkList *pTmpNode = NULL;
LinkList *pFreeNode = NULL;
pTmpNode = pFreeNode = *pphead;
while(pTmpNode != NULL)
{
pTmpNode = pTmpNode->pNext;
free(pFreeNode);
pFreeNode = pTmpNode;
}
*pphead = NULL;
return 0;
}
int main(void)
{
LinkList *linklist = NULL;
linklist = CreatLinkList();
InsertTailLinkList(linklist, 1);
InsertTailLinkList(linklist, 2);
InsertTailLinkList(linklist, 3);
InsertTailLinkList(linklist, 4);
InsertTailLinkList(linklist, 5);
ShowLinkList(linklist);
DestoryLinkList(&linklist);
}
2. 从终端接收一个字符,将字符串倒置后输出(用指针的方法):
“how are you” > "you are how"
#include
int Reverse(char *pstr)
{
char *phead = NULL;
char *ptail = NULL;
char tmp = 0;
phead = ptail = pstr;
while(*ptail != '\0')
{
ptail++;
}
ptail--;
while(phead < ptail)
{
tmp = *phead;
*phead = *ptail;
*ptail = tmp;
phead++;
ptail--;
}
return 0;
}
int ReverseStr(char *pstr)
{
char *phead = NULL;
char *ptail = NULL;
char *tmp1 = NULL;
char *tmp2 = NULL;
char tmp = 0;
Reverse(pstr);
phead = ptail = pstr;
while(*pstr != '\0')
{
while(*ptail != ' ' && *ptail != '\0')
{
ptail++;
}
ptail--;
tmp1 = phead;
tmp2 = ptail;
while(tmp1 < tmp2)
{
tmp = *tmp1;
*tmp1 = *tmp2;
*tmp2 = tmp;
tmp1++;
tmp2--;
}
if (*ptail != '\0')
{
ptail = ptail + 2;
phead = ptail;
}
else
{
break;
}
}
}
int main(void)
{
char str[32] = "how are you";
ReverseStr(str);
printf("%s\n",str);
return 0;
}