C语言动态内存管理

学c++方向,指针、结构体、动态内存开辟这三个直接关系到数据结构的学习,非常重要(学java就还好)

栈区:局部变量,形式参数
堆区:malloc,calloc,realloc,free申请的空间

malloc



int main()
{
	
	int arr[10] = { 0 };  //栈区申请空间
	//动态内存开辟
	int* p = (int*)malloc(40); //堆区申请空间
	if (p == NULL) //如果开辟失败
	{  
		printf("%s\n", strerror(errno));
		return 1; //1表示异常返回
	}

	//使用内存
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		*(p + i) = i;
	}
	for (i = 0; i < 10; i++)
	{
		printf("%d ", *(p + i));
	}
	
	//没有free,内存泄露  但并不是说内存空间就不回收了
	//当程序退出的时候,系统会自动回收内存空间
	free(p);
	p = NULL; //防止p是个野指针

	//malloc跟free最好成对出现,防止内存泄露
	//过多内存泄露会导致电脑卡死,因为内存干爆了
	
	
	return 0;
}

calloc

calloc = malloc + memset
想初始化用calloc,不想用malloc
num : Number of elements
size :Length in bytes of each element





int main()
{
	//开辟10个整型的空间
	int* p = calloc(10, sizeof(int));
	if (p == NULL)
	{
		printf("%s\n", strerror(errno));
	}
	//打印
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		printf("%d ", *(p + i));  //0 0 0 0 0 0 0 0 0 0
	}
	free(p);
	p = NULL;


	return 0;
}

realloc

int main()
{
	int* p= (int*)malloc(40);
	if (NULL == p)
	{
		printf("%s\n", strerror(errno));
		return 1;
	}
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		*(p + i) = i + 1;
	}

	//扩容
	int*ptr = realloc(p, 80); //追加上40个字节,总共80个字节  
	//realloc会在堆区找到80字节的空间,再把数据拷贝过去,释放掉旧的空间,最后返回新的地址
	if (ptr != NULL) //如果扩容失败,返回空指针
	{
		p = ptr; //因此直接赋值给p很危险
	}

	for (i = 0; i < 10; i++)
	{
		printf("%d ", *(p + i));
	}
	free(p);
	p = NULL;
	return 0;
}
realloc(NULL, 40);  等价于  malloc(40);

常见动态内存错误

   int*p=(int*)malloc(INT_MAX/4);
    *p=20; //p是NULL是就有错误

动态开辟空间的越界访问

	//动态开辟空间的越界访问
	int* p = (int*)malloc(40);
	if (p == NULL)
	{
		printf("%s\n", strerror(errno));
		return 1;
	}
	int i = 0;
	for (i = 0; i <= 10; i++)
	{
		p[i] = i;
	}
	free(p);
	p = NULL;

free栈区空间

	int a = 10;
	int* p = &a;

	free(p);

使用free释放动态开辟的一部分

	

	int* p = (int*)malloc(40);
	if (p == NULL)
	{
		printf("%s\n", strerror(errno));
		return 1;
	}
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		*p=i;
		p++;
	}
	free(p);  //p不再指向空间的起始位置
	p = NULL;

对同一块空间的多次释放
下面这个不行

	free(p);
	//....
	free(p);

这个可以

	free(p);
	p = NULL;
	//....
	free(p);

动态内存开辟忘记释放(内存泄露)

	int* p = (int*)malloc(40);
	if (p == NULL)
	{
		printf("%s\n", strerror(errno));
		return 1;
	}
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		if (i == 3) 
			return;  //free没有机会实现
	}
	free(p);
	p = NULL;
int* test()
{
	int* p = (int*)malloc(100);
	if (p == NULL)
	{
		printf("%s\n", strerror(errno));
		return p;
	}
	return p;
}
int main()
{

	int* ret = test();
	
	//忘记释放了

	return 0;
}

几个经典的笔试题

void GetMemory(char* p) 
{
    p = (char*)malloc(100);  //p是形参,出函数后被销毁
}

void Test(void) 
{
    char* str = NULL;
    GetMemory(str);  //str仍是空指针
    strcpy(str, "hello world"); //str解引用时,程序会崩溃
    printf(str);  //打印不出来
}

int main()
{
    Test();
	return 0;
}

修改后


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

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

3



void Test(void)
{
	char* str = (char*)malloc(100);
	strcpy(str, "hello");
	free(str);
	if (str != NULL)
	{
		strcpy(str, "world");  //str还记得地址,但是空间已经不属于str了
		//因此free完后一定要把指针置为空
		printf(str);
	}
}
int main()
{
	Test();
	return 0;
}
  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值