2-16 malloc(1)

本文介绍C语言中的内存管理,重点讲解了malloc函数的工作原理及其内部如何利用brk进行内存分配。此外,还讨论了内存对齐的问题以及free函数的作用。

动态分配内存
memory alloc malloc
1.malloc C库函数 内部封装了brk。
2.int brk(void *addr); linux;

结构:
app
库 lib
API
OS
hard


由于对齐,可能分配内存会取4的倍数
int *p;
p=malloc(4); 4字节
void free(void*ptr);//释放,和malloc对应 ptr为申请内存块的地址,不能知道执行成不成功。

#include<stdio.h>
#include<stdlib.h>
int main()
{
int*p;
char buf[32];
p=(int*)malloc(sizeof(int));
...
free(p);
return 0;
}

free一定要释放heap的内存。

内存结构:
stack 局部变量 函数调用 自顶向下 10M
heap 自底向上 malloc分配的空间
bss 没有初始化的全局变量
data 全局变量
redata 常量
text 二进制

递归 stack smash(产生过多栈针)

当进程结束,动态分配才会还给系统。
free只是autorelease

当申请n个字节空间时,不只n个空间,前面有个管理结构(4字节)

malloc 8字节对齐
1000 8
10000 16
11000 24
后三位都为0
所以得出4字节的管理机制的后三位为0
最后一位说明内存是否可用。0表示不可用 1表示可用

int main(int argc,char*argv[])
{
int *p,*q;
int n;
n=atoi(argv[1]); //string->int
p=(int*)malloc(n);
q=(int*)malloc(8);
printf("%x,%d/n",p[-1],(unsigned)q-(unsigned)p); //p[-1]为管理结构
free(p);
free(q);
}

如果分配16个字节,则需要分配20个字节,但是不能被8整除,所以4字节填充。

ref_info.Nsamples is 968546369------------------------ -----------------ref_info.Nsamples is 0------------------------ ------pesq_measure-------------- ref_info->Nsamples is 0 -------------------- Reading reference file /eap/voip通话质量/pesq/pesq_source_2/voice/30s_rtp_cai123.wav...---malloc 10--- --------max_size is 1002308---------- ---malloc 11---size is 1002308---malloc 12---size is 31002 and 31002------load_src-------------- ref_info->Nsamples is 248017 -------------------- done. Reading degraded file /eap/voip通话质量/pesq/pesq_source_2/voice/output.wav...---malloc 10--- --------max_size is 1226236---------- ---malloc 11---size is 1226236---malloc 12---size is 37999 and 37999done. ====ref_info->Nsamples is 248017 ref_info.Nsamples is 248017 ---malloc 13---size is 1226236 Level normalization... -------------------- ref_info->Nsamples is 248017 -----fix_power_level and info-> Nsamples is 248017 ---malloc 1---size is 1002308 there is fix_power_level on apply_filter malloc 20 size is 1048584,maxNsamples is 248017 and n is 245777 malloc 20 maxNsamples is 248017,Downsample is 32 and Fs is 8000,pow_of_2 is 262144 --------max_size is 2097152---------- -----fix_power_level and info-> Nsamples is 303999 ---malloc 1---size is 1226236 there is fix_power_level on apply_filter --------max_size is 2097160---------- malloc 20 size is 2097160,maxNsamples is 303999 and n is 301759 malloc 20 maxNsamples is 303999,Downsample is 32 and Fs is 8000,pow_of_2 is 524288 --------max_size is 4194304---------- IRS filtering... malloc 20 size is 1048584,maxNsamples is 248017 and n is 245777 malloc 20 maxNsamples is 248017,Downsample is 32 and Fs is 8000,pow_of_2 is 262144 malloc 20 size is 2097160,maxNsamples is 303999 and n is 301759 malloc 20 maxNsamples is 303999,Downsample is 32 and Fs is 8000,pow_of_2 is 524288 ---malloc 2---size is 1226236 Variable delay compensation... ---malloc 3---size is 1226236 Acoustic model processing... ---malloc 15------malloc 16------malloc 17------malloc 14---size is 32776 ---malloc 17------malloc 14---size is 32776 ---malloc 17------malloc 14---size is 32776 ---malloc 17------malloc 14---size is 32776 ---malloc 17------malloc 14---size is 32776 ---malloc 17------malloc 14---size is 32776 ---malloc 18--- -------------------------------------------------------------- ---------------Prediction : PESQ_MOS = 2.594----------------- -------------------------------------------------------------
最新发布
10-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值