日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C語言?動態內存開辟常見問題解決與分析流程_C 語言

作者:K穩重 ? 更新時間: 2022-05-18 編程語言

前言

當我們用動態內存分配函數來編寫程序時,在編寫的過程中常常會產生一些不易被察覺,被發現的錯誤,例如對NULL指針的解引用操作,對動態開辟空間的越界訪問,使用free釋放非動態開辟的空間,使用free釋放動態內存中的一部分,對同一塊動態開辟的空間,多次釋放,動態開辟空間忘記釋放。下面我們挨個來分析,刨析一下這些個常見的動態內存開辟的問題。

一、動態內存錯誤

1.對NULL指針的解引用操作

代碼如下(示例):

錯誤示例:
//動態內存開辟
int main()
{
 
	int* p = malloc(100000000000);
	//沒有對mollac函數的返回值做判空處理
		int i = 0;
	for (i = 0; i < 10; i++)
	{
		*(p + i) = 5;
	}
	
	return 0;
}
 
正確示例:
//動態內存開辟
int main()
{
 
	int* p = malloc(100000000000);
	if (p == NULL)
	{
		return 1;
	}
		int i = 0;
	for (i = 0; i < 10; i++)
	{
		*(p + i) = 5;
	}
	for (i = 0; i < 10; i++)
	{
		printf("%d ", p[i]);
	}
	free(p);
	p = NULL;
	return 0;
}

2.對動態開辟空間的越界訪問

代碼如下(示例):

錯誤示例:
//動態內存開辟
int main()
{
 
	int* p = (int*)malloc(10*sizeof(int));
	if (p == NULL)
	{
		return 1;
	}
		int i = 0;
		//越界訪問
	for (i = 0; i < 40; i++)//malloc函數只是開辟了十個整型的空間,這里卻要訪問四十個元素。
	{
		*(p + i) = 5;
	}
	for (i = 0; i < 40; i++)
	{
		printf("%d ", p[i]);
	}
	free(p);
	p = NULL;
	return 0;
}
 
正確示例:
//動態內存開辟
int main()
{
 
	int* p = (int*)malloc(10*sizeof(int));
	if (p == NULL)
	{
		return 1;
	}
		int i = 0;
		
	for (i = 0; i < 10; i++)
		*(p + i) = 5;
	}
	for (i = 0; i < 10; i++)
	{
		printf("%d ", p[i]);
	}
	free(p);
	p = NULL;
	return 0;
}

3.使用free釋放非動態開辟的空間

代碼如下(示例):

//動態內存開辟
int main()
{
	int arr[10] = { 0 };//棧區
	int* p = arr;
	free(p);//使用free釋放非動態開辟的空間
	p = NULL;
	return 0;
}

4.使用free釋放動態內存中的一部分

代碼如下(示例):

 
//動態內存開辟
int main()
{
	int* p = malloc(10 * sizeof(int));
	if (p == NULL)
	{
		return 1;
	}
	int i = 0;
	for (i = 0; i < 5; i++)
	{
		*p++ = i;//1:p一直往后走之后沒人知道起始空間的位置在哪,2:p釋放的只是后面空間的一部分,前面的空間并沒有得到釋放。
	}
	free(p);
	p = NULL;
	return 0;
}

5.對同一塊動態內存動態開辟的空間多次釋放

代碼如下(示例):

//動態內存開辟
int main()
{
	int* p = malloc(10 * sizeof(int));
	//使用
	//釋放
	free(p);
	//再次釋放
	free(p);//free要是傳的是空指針什么事都不會發生。
	p = NULL;
	return 0;
}

6.動態開辟的空間忘記釋放(容易造成內存泄露,比較嚴重)

代碼如下(示例):

void test()
{
	int* p = malloc(10 * sizeof(int));
	if (p == NULL)
	{
		return 1;
	}
	//使用
	//忘記釋放
}
 
//動態內存開辟
int main()
{
	test();
	return 0;
}

二、動態內存錯誤面試題分析

1.NULL指針傳參不取地址傳的也是一份臨時拷貝

例題分析:
 
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;
}

程序運行結果:

拷貝不成功,程序直接掛掉。

原因分析:

str傳給GetMemory函數的時候是值傳遞,所以GetMemory函數的形參p是str的一份臨時拷貝。
在GetMemory函數內部動態申請空間的地址,存放在P中,不會影響外面str,所以當GetMemory函數返回
之后,str任然是NULL指針,所以strcpy會失敗。
當GetMemory函數返回之后,形參p銷毀,使得動態開辟的100個字節存在內存泄漏。

?正確代碼:

//第一種改法:
 
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;
}
 
 
//第二種改法:
 
char* GetMemory(char** p)
{
	*p = (char*)malloc(100);
}
void test(void)
{
	char* str = NULL;
	GetMemory(&str);
	strcpy(str, "hello world");
	printf(str);
	free(str);
	str = NULL;
}
 
int main()
{
	test();
	return 0;
}

2.局部變量和形式參數存在于棧上

代碼如下(示例):

//例題分析:
 
char* GetMemory(void)
{
	char p[] = "hello world";
	return p;
}
void Test(void)
{
	char* str = NULL;
	str = GetMemory();
	printf(str);
}
 
int main()
{
	test();
	return 0;
}

程序運行結果:

打印不成功,打印的都是隨機值

原因分析:

GetMemory函數內部創建的數組是在棧區上創建的
出了函數,p的數組的空間就還給了操作系統
返回的地址是沒有實際意義的,如果通過返回的地址,去訪問內存就是非法訪問內存。

?正確代碼:

char* GetMemory(void)
{
	static char p[] = "hello world";
	return p;
}
void test(void)
{
	char* str = NULL;
	str = GetMemory();
	printf(str);
}
 
int main()
{
	test();
	return 0;
}

3.動態內存開的空間記得free釋放掉

代碼如下(示例)

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;
}

錯誤分析:

申請的動態內存空間使用完之后沒有及時free釋放掉。

正確代碼:

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;
}

4.非法訪問內存

代碼如下(示例)

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;
}

錯誤分析:

申請的空間已經free釋放還給操作系統了,及時str還記得這塊空間的起始地址,但是也不能訪問,屬于非法訪問內存空間。
free完之后要及時把str置成NULL指針。

正確代碼:

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;
}

總結:

上述給大家簡單介紹了動態內存開辟常見的幾種問題,也分析了往年的幾道面試題里面的錯誤,讓我們加深了對這一章的理解,后續自己使用的時候可以有效的規避掉這些問題。相信大家都學會了。如果上述文章有任何問題?,歡迎大佬們提出質疑,我會虛心學習和改正,最重要的是能共同進步,共同成長,學習好編程。

原文鏈接:https://blog.csdn.net/m0_64397675/article/details/122942990

欄目分類
最近更新