摘要:如果開辟失敗,則返回一個(gè)指針,因此的返回值一定要做檢查。函數(shù)用來釋放動態(tài)開辟的內(nèi)存。
- 堆區(qū)
 - malloc calloc realloc free
 
在堆區(qū)上申請size_t大小的空間 返回這塊空間的起始位置
void* malloc(size_t t);
這個(gè)函數(shù)向內(nèi)存申請一塊連續(xù)可用的空間,并返回指向這塊空間的指針。
- 如果開辟成功,則返回一個(gè)指向開辟好空間的指針。
 - 如果開辟失敗,則返回一個(gè)NULL指針,因此malloc的返回值一定要做檢查。
 - 返回值的類型是 void* ,所以malloc函數(shù)并不知道開辟空間的類型,具體在使用的時(shí)候使用者自己來決定。
 - 如果參數(shù) size 為0,malloc的行為是標(biāo)準(zhǔn)是未定義的,取決于編譯器。
 
free函數(shù)用來釋放動態(tài)開辟的內(nèi)存。
- 如果參數(shù) ptr 指向的空間不是動態(tài)開辟的,那free函數(shù)的行為是未定義的。
 - 如果參數(shù) ptr 是NULL指針,則函數(shù)什么事都不做。
 
#include  #include  int main(){	// 1. 開辟空間	int* p = (int*)malloc(40); // 申請40大小空間 把起始地址強(qiáng)轉(zhuǎn)為int* 賦給p	if (p == NULL)	{		return -1;	}	// 開辟成功了 可以使用	int i = 0;	for (i = 0; i < 10; i++)	{		*(p + i) = i;	}	// 2. 釋放空間	free(p);	p = NULL; // 	return 0;} void* calloc (size_t num, size_t size);
malloc函數(shù)只負(fù)責(zé)在堆區(qū)申請空間,并且返回起始地址,不初始化空間
 calloc函數(shù)在堆區(qū)上申請空間,并且在返回起始地址之前把申請的每個(gè)字節(jié)初始化為0
#include  #include  #include  int main(){	int* p = (int*)calloc(10, sizeof(int));	if (p == NULL)	{		printf("%s/n", strerror(errno));		return -1;	}	// 申請成功	int i = 0;	for (i = 0; i < 10; i++)	{		printf("%d ", *(p + i));	}	// 釋放空間	free(p);	p = NULL;	return 0;} 讓動態(tài)內(nèi)存管理更加靈活
void* realloc (void* ptr, size_t size);
ptr 是要調(diào)整的內(nèi)存地址
size 調(diào)整之后新大小
返回值為調(diào)整之后的內(nèi)存起始位置。
這個(gè)函數(shù)調(diào)整原內(nèi)存空間大小的基礎(chǔ)上,還會將原來內(nèi)存中的數(shù)據(jù)移動到 新 的空間
兩種情況:
#include  #include  #include  int main(){	int* p = (int*)calloc(10, sizeof(int));	if (p == NULL)	{		printf("%s/n", strerror(errno));		return -1;	}	int i = 0;	for (i = 0; i < 10; i++)	{		*(p + i) = i;	}	// 空間不夠大,增加空間至20int	int* ptr = (int*)realloc(p, 20 * sizeof(int));	if (ptr != NULL)	{		p = ptr;	}	else	{		return -1;	}	// 增加成功,使用	for (i = 10; i < 20; i++)	{		*(p + i) = i;	}	for (i = 0; i < 20; i++)	{		printf("%d ", *(p + i));	}	free(p);	p = NULL;	return 0;} int* p = (int*)malloc(20);*p = 0; // 有風(fēng)險(xiǎn) #include  #include  int main(){	int* p = (int*)malloc(20);	if (p == NULL)	{		return -1;	}	*p = 0;	return 0;} #include  #include  int main(){	int* p = (int*)malloc(200);	if (p == NULL)	{		return -1;	}	int i = 0;	for (i = 0; i < 80; i++)	{		*(p + i) = 1;	}		for (i = 0; i < 80; i++)	{		printf("%d ", *(p + i));	}	free(p);	p = NULL;	return 0;} int main(){	int a = 10;	int* p = &a;	free(p); // err	p = NULL;	return 0;} 改變了p 不再指向起始位置 此時(shí)釋放的不在 起始位置
#include  #include  int main(){	int* p = (int*)malloc(10 * sizeof(int));	if (p == NULL)	{		return -1;	}	int i = 0;	for (i = 0; i < 10; i++)	{		*p++ = 1;	}	free(p);	p = NULL;	return 0;} int main(){	int* p = (int*)malloc(40);	if (p == NULL)	{		return -1;	}	free(p);	free(p); // err}int main(){	int* p = (int*)malloc(40);	if (p == NULL)	{		return -1;	}	free(p);	p = NULL;	free(p); // ok} 在堆區(qū)上申請空間,有2種回收方式,
- free
 - 程序退出時(shí),申請的空間回收
 
int main(){	int* p = (int*)malloc(40);	if (p == NULL)	{		return -1;	}	// 沒有釋放	return 0;} #include  #include  void GetMemory(char* p){	p = (char*)malloc(100);} void Test(void){	char* str = NULL;	GetMemory(str); 	strcpy(str, "hello world");	printf(str);} int main(){	Test();	return 0;}  程序會崩潰
修改:
 版本1:
#include  #include  #include  void GetMemory(char** p){	*p = (char*)malloc(100);} void Test(void){	char* str = NULL;	GetMemory(&str); // char** 	strcpy(str, "hello world");	printf(str);	// 釋放	free(str);	str = NULL;} int main(){	Test();	return 0;} 版本2:
#include  #include  #include  char* GetMemory(char* p){	p = (char*)malloc(100); 	return p;} void Test(void){	char* str = NULL; 	str = GetMemory(str);	strcpy(str, "hello world"); 	printf(str);		free(str);	str = NULL;} int main(){	Test();	return 0;} #include  char* GetMemory(void){	char p[] = "hello world";	return p;} void Test(void){	char* str = NULL;	str = GetMemory();	printf(str);} int main(){	Test();	return 0;} 【返回棧空間地址問題】
#include  int* test(){	int n = 10;	return &n;} int main(){	int* p = test();	printf("%d/n", *p);	// 如果沒有被覆蓋,有可能輸出10		return 0;} #include  void GetMemory(char** p, int num){	*p = (char*)malloc(num);}void Test(void){	char* str = NULL;	GetMemory(&str, 100);	strcpy(str, "hello");	printf(str);}int main(){	Test();	return 0;} 通過*p 用malloc給str在堆上開辟空間,
 問題:
內(nèi)存泄漏,沒有free
free(str);
str = NULL;
改正:
#include  #include  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;}int main(){	Test();	return 0;} #include  #include  void Test(void){	char* str = (char*)malloc(100);	strcpy(str, "hello");	free(str);	if (str != NULL)	{		strcpy(str, "world");		printf(str);	}}int main(){	Test();	return 0;}  改正:
#include  #include  void Test(void){	char* str = (char*)malloc(100);	strcpy(str, "hello");	free(str);	str = NULL;	if (str != NULL)	{		strcpy(str, "world");		printf(str);	}}int main(){	Test();	return 0;} C99 中,結(jié)構(gòu)中的最后一個(gè)元素允許是未知大小的數(shù)組,這就叫做『柔性數(shù)組』成員。
// 數(shù)組大小不確定,可大可小typedef struct st_type{	int i;	int a[0];//柔性數(shù)組成員}type_a;// 編譯器報(bào)錯(cuò)無法編譯可改成:typedef struct st_type{	int i;	int a[];//柔性數(shù)組成員}type_a; 1. 結(jié)構(gòu)中的柔性數(shù)組成員前面必須至少一個(gè)其他成員	如 int a[] 前有 int i2. sizeof 返回的這種結(jié)構(gòu)大小不包括柔性數(shù)組的內(nèi)存 #include  typedef struct <           
               
                                           
                       
                 文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/121274.html
摘要:釋放不完全導(dǎo)致內(nèi)存泄漏。既然把柔性數(shù)組放在動態(tài)內(nèi)存管理一章,可見二者有必然的聯(lián)系。包含柔性數(shù)組的結(jié)構(gòu)用進(jìn)行動態(tài)內(nèi)存分配,且分配的內(nèi)存應(yīng)大于結(jié)構(gòu)大小,以滿足柔性數(shù)組的預(yù)期。使用含柔性數(shù)組的結(jié)構(gòu)體,需配合以等動態(tài)內(nèi)存分配函數(shù)。 ...
閱讀 1860·2021-11-25 09:43
閱讀 1891·2021-11-24 10:41
閱讀 3212·2021-09-27 13:36
閱讀 866·2019-08-30 15:53
閱讀 3637·2019-08-30 15:44
閱讀 958·2019-08-30 14:03
閱讀 2659·2019-08-29 16:38
閱讀 1060·2019-08-29 13:23