系列文章目錄
文章目錄
前言
??作者簡介:大家好,我是橘橙黃又青,一個想要與大家共同進步的男人????
??個人主頁:橘橙黃又青_C語言,函數(shù),指針-CSDN博客
目的:學習malloc,free,calloc,realloc函數(shù)的使用。
內存函數(shù)在#include<stdio.h>頭文件里面。
1. 為什么要有動態(tài)內存分配
我們已經(jīng)掌握的內存開辟?式有:
int val = 20;//在??臻g上開辟四個字節(jié)
char arr[10] = {0};//在??臻g上開辟10個字節(jié)的連續(xù)空間
? 空間開辟??是固定的。? 數(shù)組在申明的時候,必須指定數(shù)組的?度,數(shù)組空間?旦確定了??不能調整。

2. malloc和free
2.1 malloc
C語?提供了?個動態(tài)內存開辟的函數(shù):
void* malloc (size_t size);
這個函數(shù)向內存申請?塊連續(xù)可?的空間,并返回指向這塊空間的指針。 ?
? 如果開辟成功,則返回?個指向開辟好空間的指針。? 如果開辟失敗,則返回?個 NULL 指針,因此malloc的返回值?定要做檢查。? 返回值的類型是 void* ,所以malloc函數(shù)并不知道開辟空間的類型,具體在使?的時候使?者? ?來決定。? 如果參數(shù) size 為0,malloc的?為是標準是未定義的,取決于編譯器
代碼演示:
int* p = (int*)malloc(10 * sizeof(int))開辟10個整形空間
malloc(字節(jié))
2.2 free
void free (void* ptr);
? 如果參數(shù) ptr 指向的空間不是動態(tài)開辟的,那free函數(shù)的?為是未定義的。? 如果參數(shù) ptr 是NULL指針,則函數(shù)什么事都不做。malloc和free都聲明在 stdlib.h 頭?件中。
舉個例?:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num = 0;
scanf("%d", &num);
int arr[num] = {0};
int* ptr = NULL;
ptr = (int*)malloc(num*sizeof(int));
if(NULL != ptr)//判斷ptr指針是否為空
{
int i = 0;
for(i=0; i<num; i++){
*(ptr+i) = 0;
}
}
free(ptr);//釋放ptr所指向的動態(tài)內存
ptr = NULL;
return 0;
}
3. calloc和realloc
3.1 calloc
void* calloc (size_t num, size_t size);
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *p = (int*)calloc(10, sizeof(int));
if(NULL != p)
{
int i = 0;
for(i=0; i<10; i++){
printf("%d ", *(p+i));
}
}
free(p);
p = NULL;
return 0;
}
輸出結果:
3.2 realloc
? realloc函數(shù)的出現(xiàn)讓動態(tài)內存管理更加靈活。? 有時會我們發(fā)現(xiàn)過去申請的空間太?了,有時候我們?會覺得申請的空間過?了,那為了合理的時候內存,我們?定會對內存的??做靈活的調整。那 realloc 函數(shù)就可以做到對動態(tài)開辟內存??的調整。
函數(shù)原型如下:
void* realloc (void* ptr, size_t size);
? ptr 是要調整的內存地址? size 調整之后新??? 返回值為調整之后的內存起始位置。? 這個函數(shù)調整原內存空間??的基礎上,還會將原來內存中的數(shù)據(jù)移動到 新 的空間。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr = (int*)malloc(100);
if(ptr != NULL)
{
//業(yè)務處理
}
else
{
return 1;
}
//擴展容量
//代碼1 - 直接將realloc的返回值放到ptr中
ptr = (int*)realloc(ptr, 1000);//這樣可以嗎?(如果申請失敗會如何?)
//代碼2 - 先將realloc函數(shù)的返回值放在p中,不為NULL,在放ptr中
int*p = NULL;
p = realloc(ptr, 1000);
if(p != NULL)//判斷
{
ptr = p;
}
//業(yè)務處理
free(ptr);
return 0;
}
4. 常?的動態(tài)內存的錯誤
4.1 對NULL指針的解引?操作
void test()
{
int *p = (int *)malloc(INT_MAX/4);
*p = 20;//如果p的值是NULL,就會有問題
free(p);
}
所以使用動態(tài)內存函數(shù)是一定要養(yǎng)成習慣判斷是否為NULL
4.2 對動態(tài)開辟空間的越界訪問
void test()
{
int i = 0;
int *p = (int *)malloc(10*sizeof(int));
if(NULL == p)
{
exit(EXIT_FAILURE);//報錯
}
for(i=0; i<=10; i++)//問題就出在i==10
{
*(p+i) = i;//當i是10的時候越界訪問
}
free(p);
}
4.3 對?動態(tài)開辟內存使?free釋放
void test()
{
int a = 10;
int *p = &a;
free(p);//ok?
}
4.4 使?free釋放?塊動態(tài)開辟內存的?部分
void test()
{
int *p = (int *)malloc(100);
p++;
free(p);//p不再指向動態(tài)內存的起始位置
}
4.5 對同?塊動態(tài)內存多次釋放
void test()
{
int *p = (int *)malloc(100);
free(p);
free(p);//重復釋放
}
4.6 動態(tài)開辟內存忘記釋放(內存泄漏)
void test()
{
int *p = (int *)malloc(100);
if(NULL != p)
{
*p = 20;
}
}
int main()
{
test();
while(1);
}
5. 動態(tài)內存經(jīng)典筆試題分析
5.1 題?1:
void GetMemory(char *p)
{
p = (char *)malloc(100);
}
void Test(void)
{
char *str = NULL;
GetMemory(str);
strcpy(str, "hello world");
printf(str);
}
輸出結果是什么?
為什么?
這是因為str 傳過去char*p是相當于值傳遞,是臨時拷貝的str,函數(shù)銷毀后,str還是不變,str還是NULL,所以沒有輸出。
5.2 題?2:
char *GetMemory(void)
{
char p[] = "hello world";
return p;
}
void Test(void)
{
char *str = NULL;
str = GetMemory();
printf(str);
}
輸出結果:
因為:
在這里p的地址的確傳了回去的是地址后面的空間銷毀了,所以才會出現(xiàn)這結果。
5.3 題?3:
void GetMemory(char **p, int num)
{
*p = (char *)malloc(num);
}
void Test(void)
{
char *str = NULL;
GetMemory(&str, 100);
strcpy(str, "hello");
printf(str);
// free(str);
// str = NULL;
}
忘記了釋放
5.4 題?4:
void Test(void)
{
char *str = (char *) malloc(100);
strcpy(str, "hello");
free(str);
if(str != NULL)
{
strcpy(str, "world");
printf(str);
}
}
忘記str = NULL,且釋放空間后又把world放進去,//非法訪問
6. 柔性數(shù)組
typedef struct st_type
{
int i;
int a[0];//柔性數(shù)組成員
}type_a;
typedef struct st_type
{
int i;
int a[];//柔性數(shù)組成員
}type_a;
6.1 柔性數(shù)組的特點: ?
? 結構中的柔性數(shù)組成員前?必須?少?個其他成員 。? sizeof 返回的這種結構??不包括柔性數(shù)組的內存 。? 包含柔性數(shù)組成員的結構?malloc ()函數(shù)進?內存的動態(tài)分配,并且分配的內存應該?于結構的??,以適應柔性數(shù)組的預期??。
例如:?
typedef struct st_type
{
int i;
int a[0];//柔性數(shù)組成員
}type_a;
int main()
{
printf("%d\n", sizeof(type_a));//輸出的是4
return 0;
}
6.2 柔性數(shù)組的使?
代碼1:
#include <stdio.h>
#include <stdlib.h>
struct St
{
char c;
int n;
int arr[0];
};
int main()
{
struct St* ps = (struct St*)malloc(sizeof(struct St) + 10 * sizeof(int));//柔性數(shù)組開辟的空間:10 * sizeof(int)
if (ps == NULL)
{
perror("malloc");
return 1;
}
ps->c = 'w';
ps->n = 100;
int i = 0;
for (i = 0; i < 10; i++)
{
ps->arr[i] = i;
}
//數(shù)組空間不夠
struct St* ptr = realloc(ps, sizeof(struct St) + 15 * sizeof(int));//改變柔性數(shù)組空間
if (ptr != NULL)
{
ps = ptr;
}
else
{
perror("realloc");//報錯
return 1;
}
//...繼續(xù)使用
for (i = 10; i < 15; i++)
{
ps->arr[i] = i;
}
for (i = 0; i < 15; i++)
{
printf("%d ", ps->arr[i]);
}
printf("\n%d\n", ps->n);
printf("%c\n", ps->c);
//釋放
free(ps);
ps = NULL;
return 0;
}
還有另一種方式:
代碼2:
struct St
{
char c;
int n;
int* arr;//使用指針的方式訪問
};
int main()
{
struct St* ps = (struct St*)malloc(sizeof(struct St));
if (ps == NULL)
{
perror("malloc");//報錯
return 1;
}
ps->c = 'w';
ps->n = 100;
ps->arr = (int*)malloc(10 * sizeof(int));//給柔性數(shù)組開辟空間
if (ps->arr == NULL)//判斷
{
perror("malloc-2");//報錯
return 1;
}
//使用
int i = 0;
for (i = 0; i < 10; i++)
{
ps->arr[i] = i;
}
//數(shù)組空間不夠
int* ptr = (int*)realloc(ps->arr, 15 * sizeof(int));//這個可以,重點
if (ptr == NULL)
{
perror("realloc");
return 1;
}
else
{
ps->arr = ptr;
}
//使用
for (i = 10; i < 15; i++)
{
ps->arr[i] = i;
}
for (i = 0; i < 15; i++)
{
printf("%d ", ps->arr[i]);
}
printf("\n%d\n", ps->n);
printf("%c\n", ps->c);
//釋放兩次
free(ps->arr);
ps->arr = NULL;
free(ps);
ps = NULL;
return 0;
}
上述的 type_a 結構也可以設計為下?的結構,也能完成同樣的效果:
//代碼2
#include <stdio.h>
#include <stdlib.h>
typedef struct st_type
{
int i;
int *p_a;
}type_a;
int main()
{
type_a *p = (type_a *)malloc(sizeof(type_a));
p->i = 100;
p->p_a = (int *)malloc(p->i*sizeof(int));
//業(yè)務處理
for(i=0; i<100; i++)
{
p->p_a[i] = i;
}
//釋放空間
free(p->p_a);
p->p_a = NULL;
free(p);
p = NULL;
return 0;
}
這樣就簡化很多了。文章來源:http://www.zghlxwxcb.cn/news/detail-760994.html
6.3 柔性數(shù)組的優(yōu)勢
上述 代碼1 和 代碼2 可以完成同樣的功能,但是 ?法1 的實現(xiàn)有兩個好處:文章來源地址http://www.zghlxwxcb.cn/news/detail-760994.html
到了這里,關于C語言->動態(tài)內存管理的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!