欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 时评 > [C语言基础]13.动态内存管理

[C语言基础]13.动态内存管理

2025/3/15 16:52:39 来源:https://blog.csdn.net/weixin_45752674/article/details/146269584  浏览:    关键词:[C语言基础]13.动态内存管理

动态内存管理

  • 1. 动态内存分配
  • 2. 动态内存函数的介绍
    • 2.1 malloc
    • 2.2 free
    • 2.3 calloc
    • 2.4 realloc
  • 3. 动态内存错误
    • 3.1 NULL指针解引用
    • 3.2 动态开辟空间越界访问
    • 3.3 非动态开辟内存使用free释放
    • 3.4 free释放动态开辟内存的一部分
    • 3.5 同一块动态内存多次释放
    • 3.6 动态开辟内存未释放(内存泄漏)

1. 动态内存分配

int val = 20;//在栈空间上开辟四个字节  
char arr[10] = {0};//在栈空间上开辟10个字节的连续空间
  1. 空间开辟大小是固定的。
  2. 数组在申明时,必须指定数组长度,所需内存在编译时分配。

2. 动态内存函数的介绍

2.1 malloc

void* malloc (size_t size);

动态内存开辟函数:

  • 向内存申请一块连续可用的空间,返回指向这块空间的指针。
  • 开辟成功,则返回一个指向开辟好空间的指针。
  • 开辟失败,则返回一个NULL指针,因此malloc的返回值一定要做检查。
    返回类型是 void*,malloc函数并不知道开辟空间的类型,具体在使用的时候使用者自己来决定。
    malloc申请到空间后,直接返回空间起始地址,不会初始化空间。
#include <stdlib.h>
int main() {int* p = (int*)malloc(40);if (p == NULL) {//检测malloc返回指针perror("malloc");return 1;}int i = 0;for (i = 0; i < 10; i++) {printf("%d\n", *(p + i));}free(p);//释放空间p = NULL;//p变成野指针,要置为空指针return 0;
}

2.2 free

void free (void* ptr);

malloc申请内存空间,不会主动释放内存,需要free释放。程序退出时会还给系统。
如果参数 ptr 是NULL指针,则函数什么事都不做。

2.3 calloc

void* calloc (size_t num, size_t size);
  • 为 num 个大小 为 size 的元素开辟一块空间,并且把空间的每个字节初始化为0。
  • calloc 会在返回地址前,把申请的空间的每个字节初始化为全0。
  • malloc申请到空间后,直接返回空间起始地址,不会初始化空间。
#include <stdlib.h>
int main() {int* p = (int*)calloc(10, sizeof(int));if (p == NULL) {//检测calloc返回指针perror("calloc");return 1;}int i = 0;for (i = 0; i < 10; i++) {printf("%d ", p[i]);}free(p);//释放空间p = NULL;//p置为空指针return 0;
}

0 0 0 0 0 0 0 0 0 0

2.4 realloc

void* realloc (void* ptr, size_t size);
  • realloc 可以调整动态开辟内存大小,返回调整后的内存位置
  • ptr:需要调整的内存地址;size:调整后的新大小
    调整空间大小的情况:
    1.原有空间后有空间
  • 原有内存后直接追加空间,原来空间的数据不变
    2.原有空间后没空间
  • 在堆空间上另找一个合适大小的连续空间来使用
  • 会将原来内存中的数据移动新的空间
  • 释放旧空间,返回新空间内存地址
int main() {int* p = (int*)malloc(40);if (p == NULL) {perror("calloc");return 1;}//初始化int i = 0;for (i = 0; i < 10; i++) {p[i] = i + 1;}//增加空间int* ptr = (int*)realloc(p, 80);if (ptr != NULL) {p = ptr;//如果扩展失败了,p被置为NULL,故不能直接使用p接受新指针,否则会内存泄漏}else {perror("realloc");return 1;}//打印数据for (i = 0; i < 20; i++) {printf("%d ", p[i]);}free(p);//释放空间p = NULL;//p置为空指针return 0;
}

1 2 3 4 5 6 7 8 9 10 -842150451 -842150451 -842150451 -842150451 -842150451 -842150451 -842150451 -842150451 -842150451 -842150451

3. 动态内存错误

3.1 NULL指针解引用

void test()  {  int *p = (int *)malloc(INT_MAX/4);  *p = 20;//如果p的值是NULL,就会有问题  free(p);  
}

3.2 动态开辟空间越界访问

int main() {int* p = (int*)malloc(40);//10个整形if (NULL == p){perror("malloc");return 1;}int i = 0;for (i = 0; i <= 20; i++){p[i] = i;//越界访问}free(p);p = NULL;return 0;
}

3.3 非动态开辟内存使用free释放

int main() {int a = 10;int* p = &a;printf("%d\n", *p);//a不是动态开辟的free(p);p = NULL;return 0;
}

3.4 free释放动态开辟内存的一部分

int main() {int* p = (int*)malloc(40);if (NULL == p){perror("malloc");return 1;}int i = 0;for (i = 0; i < 5; i++) {*p = i;p++;}free(p);//p已经不指向起始位置p = NULL;return 0;
}

3.5 同一块动态内存多次释放

int main() {int* p = (int*)malloc(40);if (NULL == p) {perror("malloc");return 1;}free(p);free(p);//p多次释放p = NULL;return 0;
}

每次释放完p置空

free(p);
p = NULL;

3.6 动态开辟内存未释放(内存泄漏)

void test(){int* p = (int*)malloc(100);if (NULL != p){*p = 20;}
}
int main(){test();while (1);
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词