int num = 20;//在栈空间上开辟4个字节
char arr[10] = { 0 };//在栈空间上开辟10个字节的连续空间
我们知道局部变量的创建、数组的使用是在栈空间上开辟的空间;对于这些内存的开辟,可以总结出俩个特点:
但有时候我们需要的空间在程序运行时才能知道,分配的内存空间大小不能是固定的,想要实现按需求灵活可变的分配内存,就需要运用到动态内存的相关知识了!
malloc和free都声明在stdlib.h
头文件中。
C语言提供了一个动态内存开辟的函数:
void* malloc (size_t size);
这个函数向内存申请一块连续可用的空间,并返回指向这块空间的指针。
void*
,所以malloc函数并不知道开辟空间的类型,具体在使用的时候使用者自己来决定。size
为0,malloc的行为是标准是未定义的,取决于编译器。#include
#include
#include
int main()
{
//int arr[10] = { 0 };//向内存申请了40个字节的空间
//void* p = malloc(40);
//这样写有缺点,假如这40个字节里面我想存10个整数,void*的指针无法解引用和加减
//转化成整型指针来维护这个空间
int* p = (int*)malloc(40);
//保存旧地址
int* ptr = p;
//判断申请的空间够不够 不够就返回NULL
if (p == NULL)
{
printf("%s\n", strerror(errno));//报错函数
return 1;
}
//使用
int i = 0;
for (i = 0; i < 10; i++)
{
*ptr = i;
ptr++;
}
//释放
free(p);//释放的时候得把旧地址保存起来
p = NULL;//防止非法访问内存,防止p是野指针
return 0;
}
C语言还提供了一个函数叫 calloc , calloc 函数也用来动态内存分配。原型如下:
void* calloc (size_t num, size_t size);
num
个大小为 size
的元素开辟一块空间,并且把空间的每个字节初始化为0。malloc
的区别只在于calloc
会在返回地址之前把申请的空间的每个字节初始化为全0。#include
#include
#include
int main()
{
int* p = (int*)calloc(10, sizeof(int));
if (p == NULL)
{
perror("calloc");//calloc的时候出现了问题
return 1;
}
int i = 0;
for (i = 0; i < 10; i++)
{
*(p + i) = i;//p+i产生的地址,所以可以直接free(p)
}
free(p);
p = NULL;
return 0;
}
//calloc = malloc + memset
void* realloc (void* ptr, size_t size);
ptr
是要调整的内存地址size
调整之后新大小总结:
堆区
上进行内存空间的开辟,开辟到的是一个连续
的空间malloc
- 不初始化calloc
- 初始化为0realloc
- 调整内存空间的大小/如果第一个参数是NULL
,功能类似malloc
free
- 释放动态内存开辟的内存空间//对NULL指针的解引用操作
#include
#include
#include
int main()
{
int* p = (int*)malloc(INT_MAX);
//*p = 5;//err 如果p是空指针,空指针不能解引用
if (p == NULL)
{
perror("malloc");
return 1;//开辟失败返回1
}
else
{
*p = 5;
}
free(p);//释放的时候得把旧地址保存起来
p = NULL;//防止非法访问内存,防止p是野指针
return 0;
}
int main()
{
int* p = (int*)malloc(40);
if (p == NULL)
{
printf("%s\n", strerror(errno));
return 1;
}
int i = 0;
for (i = 0; i <= 10; i++)
{//i = 10时发生越界访问
p[i] = i;
}
free(p);
p = NULL;
return 0;
}
#include
#include
int main()
{
int a = 10;
int* p = &a;//开辟了静态的空间,不能被free
//.....
//err
free(p);
p = NULL;
return 0;
}
//使用free释放一块动态开辟内存的一部分
#include
#include
#include
int main()
{
int* p = (int*)malloc(40);
if (p == NULL)
{
return 1;
}
//使用
int i = 0;
for (i = 0; i < 5; i++)
{
*p = i;
p++;
}
//释放
free(p);;//p不再指向动态内存的起始位置,应该保存旧地址p
p = NULL;
return 0;
}
//err
void test()
{
int* p = (int*)malloc(100);
free(p);
free(p);//重复释放
}
忘记释放不再使用的动态开辟的空间会造成内存泄漏。
切记:
//动态开辟内存忘记释放(内存泄漏)
#include
#include
int* get_memory()
{
int* p = (int*)malloc(40);
//.....
return p;
}
int main()
{
int* ptr = get_memory();
//....
//忘记释放空间导致内存泄漏
return 0;
}
#include
#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;
}
请问运行Test 函数会有什么样的结果?
#include
#include
#include
void GetMemory(char* p)
{
p = (char*)malloc(100);//没有判断p是不是NULL
}
void Test(void)
{
char* str = NULL;
GetMemory(str);
strcpy(str, "hello world");//err
printf(str);//等价于printf("%s\n",str);
//没有释放空间,会导致内存泄漏
//哪怕加了free函数都无法释放,因为str是传值引用,p是形参,只作用于GetMemory函数中
}
int main()
{
Test();
return 0;
}
//步骤:
//1:将str所指向的内容传到指针p上去,p为NULL,进入GetMemory函数
//2:p开辟了100字节的空间,将起始地址强行转换成char*类型,把起始地址放入p中,然后进入Test函数
//3:此时到达调用GetMemory(str)这一步,str依旧为NULL
//4:根据strcpy的语法特性,str必须解引用,可此时str为NULL,无法解引用
//改法1:
#include
#include
#include
char* GetMemory()
{
char* p = (char*)malloc(100);//通过p找到str,因此对其解引用
if (p == NULL)
{
perror("malloc");
return 1;
}
else
{
return p;
}
}
void Test(void)
{
char* str = NULL;
str = GetMemory();
strcpy(str, "hello world");
printf(str);
//释放
free(str);
str = NULL;
}
int main()
{
Test();
return 0;
}
//改法2:
#include
#include
#include
void GetMemory(char** p)//取str的地址,因此要改成二级指针
{
*p = (char*)malloc(100);//通过p找到str,因此对其解引用
if (*p == NULL)
{
perror("malloc");
}
else
{
}
}
void Test(void)
{
char* str = NULL;
GetMemory(&str);
strcpy(str, "hello world");
printf(str);
//释放
free(str);
str = NULL;
}
int main()
{
Test();
return 0;
}
#include
#include
#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;
}
请问运行Test 函数会有什么样的结果?
#include
#include
#include
char* GetMemory(void)
{
char p[] = "hello world";
return p;
//数组p位于栈空间中,出函数后
//p释放的空间要还给操作系统,这个空间已经不在程序的作用域内
//str变成了野指针
}
void Test(void)
{
char* str = NULL;
str = GetMemory();
printf(str);
}
int main()
{
Test();
return 0;
}
#include
#include
#include
void Test(void)
{
char* str = (char*)malloc(100);
strcpy(str, "hello");
free(str);
if (str != NULL)//此时的str是野指针,非法访问
{
strcpy(str, "world");
printf(str);
}
}
int main()
{
Test();
return 0;
}
请问运行Test 函数会有什么样的结果?
在free§后面加个str=NULL;即可
C/C++程序内存分配的几个区域:
局部变量、函数参数、返回数据、返回地址
等。链表
。其主要存放动态内存管理
。全局变量、静态数据
。程序结束后由系统释放。栈区
分配空间的,栈区的特点是在上面创建的变量出了作用域就销毁。数据段(静态区)
,数据段的特点是在上面创建的变量,直到程序结束才销毁,所以生命周期变长。C99 中,结构中的最后一个元素允许是未知大小的数组,这就叫做『柔性数组』成员。
struct S
{
int n;
float s;
//柔性数组的两种表现形式
1:int arr[0];
2:int arr[];
};
//途径1:
#include
#include
struct S
{
int n;
float s;
//柔性数组的两种表现形式
//1:int arr[0];
//2:
int arr[];
};
int main()
{
//printf("%d\n", sizeof(struct S));//8
struct S* ps = malloc(sizeof(struct S) + sizeof(int) * 4);//malloc返回的是开辟空间的起始地址
if (ps == NULL)
return 1;
//.....
ps->n = 100;//对结构体的n赋值
ps->s = 5.5f;
int i = 0;
for (i = 0; i < 4; i++)
{
scanf("%d", &(ps->arr[i]));
}
printf("%d %f\n", ps->n, ps->s);
for (i = 0; i < 4; i++)
{
printf("%d ", ps->arr[i]);
}
//调整
struct S* ptr = (struct S*)realloc(ps, sizeof(struct S) + 10 * sizeof(int));
if (ptr == NULL)
return 1;
else
ps = ptr;
//释放
free(ps);
ps = NULL;
return 0;
}
//途径2:
#include
#include
#include
struct S
{
int n;
float s;
int* arr;
};
int main()
{
struct S* ps = (struct S*)malloc(sizeof(struct S));
if (ps == NULL)
return 1;
ps->n = 100;
ps->s = 5.5f;
int* ptr = (int*)malloc(4 * sizeof(int));
if (ptr == NULL)
return 1;
else
ps->arr = ptr;
//使用
int i = 0;
for (i = 0; i < 4; i++)
{
scanf("%d", &(ps->arr[i]));
}
//调整
realloc(ps->arr, 10 * sizeof(int));
//打印
printf("%d %f\n", ps->n, ps->s);
for (i = 0; i < 4; i++)
{
printf("%d ", ps->arr[i]);
}
//释放
free(ps->arr);
ps->arr = NULL;
return 0;
}
上述 代码1 和 代码2 可以完成同样的功能,但是 方法1 的实现有两个好处:
第一个好处是:方便内存释放
第二个好处是:这样有利于访问速度.