c语言动态内存申请的作用,C语言 动态内存分配的详解及实例

1. 动态内存分配的意义

(1)C 语言中的一切操作都是基于内存的。

(2)变量和数组都是内存的别名。

①内存分配由编译器在编译期间决定

②定义数组的时候必须指定数组长度

③数组长度是在编译期就必须确定的

(3)但是程序运行的过程中,可能需要使用一些额外的内存空间

2. malloc 和 free 函数

(1)malloc 和 free 用于执行动态内存分配的释放

(2)malloc 所分配的是一块连续的内存

(3)malloc 以字节为单位,并且返回值不带任何的类型信息:void* malloc(size_t size);

(4)free 用于将动态内存归还系统:void free(void* pointer);

(5)_msize(void* pointer)可以获取 malloc 出来的内存空间大小

3. 使用 malloc 和 free 需要注意的地方

(1)malloc 和 free 是库函数,而不是系统调用

(2)malloc 实际分配的内存可能有会比请求的多,但不能依赖于不同平台下的 malloc 行为。

(3)当请求的动态内存无法满足时,malloc 返回 NULL

(4)当 free 的参数为 NULL 时,函数直接返回

malloc(0)返回什么?

#include

#include

int main()

{

int i=10;

int* p= NULL;

for(i=0;i<100;i++)

{

//注意,malloc(0)会返回一个有效的内存地址,大小为1

//但我们不能依赖编译器的这种行为来使用这个字节的空间!

p = (int*)malloc(i);

printf("%d ",_msize(p));//返回malloc出来的内存空间大小

free(p);

}

return 0;

}

内存泄漏检测模块

mleak.h

#ifndef _MLEAK_H_

#define _MLEAK_H_

#include

#include

#define MALLOC(n) mallocEx(n, __FILE__, __LINE__)

#define FREE(p) freeEx(p)

void* mallocEx(size_t n, const char* file, const line);

void freeEx(void* p);

void PRINT_LEAK_INFO();

#endif

mleak.c

复制代码

#include "mleak.h"

#define SIZE 256

//动态内存申请参数结构体

typedef struct

{

void* pointer;//申请到的内存地址

int size; //内存块大小

const char* file; //文件名

int line; //文件行号

}MItem;

static MItem g_record[SIZE]; //记录每个动态内存申请的操作

void* mallocEx(size_t n, const char* file, const line)

{

int i = 0;

void* ret = malloc(n);//动态内存申请

if(ret != NULL)

{

//申请成功,则记录下来

//遍历全局数组,记录此次操作

for(i = 0; i< SIZE; i++)

{

//查找位置

if(g_record[i].pointer == NULL)

{

g_record[i].pointer = ret;

g_record[i].size = n;

g_record[i].file = file;

g_record[i].line = line;

break;

}

}

}

return ret;

}

void freeEx(void* p)

{

if(p != NULL)

{

int i = 0;

//遍历全局数组,释放内存空间,并清除操作记录

for(i = 0; i< SIZE; i++)

{

if(g_record[i].pointer == p)

{

g_record[i].pointer = NULL;

g_record[i].size = 0;

g_record[i].file = NULL;

g_record[i].line = 0;

free(p);

break;

}

}

}

}

void PRINT_LEAK_INFO()

{

int i = 0;

printf("Potenital Memory Leak Info:\n");

//遍历全局数组,打印未释放的空间的申请记录

for(i = 0; i< SIZE; i++)

{

//查找位置

if(g_record[i].pointer != NULL)

{

printf("Address:%p, size:%d, Location:%s:%d\n",

g_record[i].pointer,

g_record[i].size,

g_record[i].file,

g_record[i].line);

}

}

}

testc.

#include

#include "mleak.h"

void f()

{

//没释放,会造成内存泄漏!

MALLOC(100);

}

int main()

{

int* p = (int*)MALLOC(3 * sizeof(int));

f();

p[0] = 1;

p[1] = 2;

p[2] = 3;

FREE(p);

PRINT_LEAK_INFO();

return 0;

}

/*

输出结果:

E:\Study>gcc test.c mleak.c

E:\Study>a.exe

Potenital Memory Leak Info:

Address:00602ED8, size:100, Location:38-1.c:7

*/

4. calloc 和 realloc 函数

(1)malloc 的同胞兄弟:

void* calloc(size_t num, size_t size);

void* realloc(void* pointer,size_t new_size);

(2)calloc 参数表示要返回 num 个某种类型(如 sizeof(int))大小的内存空间。calloc 能以类型大小为单位申请内存并初始化为 0.

(3)realloc 用于修改一个原先己经分配的内存块大小。当第一个参数 pointer 为 NUL 时,等价于 malloc。

calloc 和 realloc 的使用

#include

#include

#define SIZE 5

int main()

{

int i = 0;

int* pI = (int*)malloc(SIZE * sizeof(int)); //malloc内存没有初始化

short* pS = (short*)calloc(SIZE, sizeof(short));//内存初始化为0

for (i = 0; i < SIZE;i++)

{

printf("pI[%d] = %d, pS[%d] = %d\n", i, pI[i], i, pS[i]);

}

printf("Before: pI = %p\n", pI); //重置内存大小之前的pI指针

pI = (int*)realloc(pI, 2 * SIZE * sizeof(int)); //内存未初始化的

printf("After: pI = %p\n", pI);

for (i = 0; i < 10;i++)

{

printf("pI[%d] = %d\n", i, pI[i]);

}

free(pI);

free(pS);

return 0;

}

通过此文希望大家对C语言的动态内存分配了解掌握,谢谢大家对本站的支持!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值