C column of Pointer <3> malloc() free()

What a nice day, ok ,let us go on the malloc() and free() chapter, if you have seen the previous chapter "Pointer <2>",you may think"Come on ! you have introduce,why again?", i really want tell you "That just a cup of water vs a river". If you just the first time to see this chapter, please refer to the "C column of Pointer <2> malloc() free()" ( link here  点击打开链接 ).

Ok, why we go on the malloc() and free() , if you try to run previous chapter example, you may be confused by the result , now I rewrite  as below

/*A bad method to allocate and free required block in heap*/
#include <stdlib.h>  
#include <stdio.h>  
int main(void)  
{  
  int *pf,*sp;  
  pf = malloc(4*sizeof(int));  
  if( pf == NULL)  
  {  
     printf("Out of memory!\n");  
     exit(1);  
  }  
  sp = pf;  
  *sp = 100;  
  *++sp = 200;//As operate precedence *++sp = *(++sp)  
  *++sp = 300;  
  sp = pf;  
  printf("The content is %d %d %d\n",*++sp,*++sp,*sp);  
  
  printf("Before free pf = %p\n",pf);  
  /*Do not need the 4*4 bytes*/  
  free(pf);
  printf("After free pf = %p\n",pf);
  printf("sp's content is %d %d\n",*--sp,*sp);//After free the required block, can we handle the pointer variable pf ??  
  return 0; 
} 


Here i add the printf() at the bottom of the program, the result as below shows


From the result we can make a conclusion , although we have freed the required block in memory (heap),but

0) the pointer variable pf is not cleared and 

1) some data still in the memory , not clear or disappear.

That why we should you memset() function to initialize the newly allocated block. Ok , we have found so many hidden problem in our code, just rewrite it as below

/* A perfect method to allocate a requested block then free it in heap 
*/  
  
#include <stdlib.h>  
#include <stdio.h> 
 
int main(void)  
{  
  int *pf,*sp;  
  pf = malloc(4*sizeof(int));  
  if( pf == NULL)  
  {  
     printf("Out of memory!\n");  
     exit(1);  
  } 
  memset(pf,0,4*sizeof(int)); //Initialize to zero
  sp = pf;  
  *sp = 100;  
  *++sp = 200;//As operate precedence *++sp = *(++sp)  
  *++sp = 300;  
  sp = pf;  
  printf("The content is %d %d %d\n",*++sp,*++sp,*sp);  
    
  /*Do not need the 4*4 bytes*/  
  free(pf);
  pf = null;//point to a null
  
  return 0;  
} 

Ok, if we use  malloc() function frequently , and forget free the unused block or all the blocks allocated are in need, then the memory will have a lot of segments .We know that the memory size is constant. The more we allocate a block form memory the smaller the remained size will be. So now, let us code to simulate this process which the malloc() function could not allocate the required block in memory ,because we have used a lot of blocks but not free.

/* This program is used to simulate the allocation but never free in heap
 * and there are a lot of memory fragments in our memory not to be freed.
 * At last ,there are not enough block to be allocated in memory.
 */
#include <stdio.h>
#include <stdlib.h>


int main(void)
{
  int count = 1;

  int *p = malloc(4000000*sizeof(int));//4*4000000 bytes = 15625 K bytes = 15 M bytes

  while(p != NULL)
  {
    printf("Address : %p \n",p);
    count++;
    p = malloc(count*4000000*sizeof(int)); // count times 15M  bytes 
  }
  printf("Have allocated %d times but free none !\n",count);

  return 0;
}

And the result (linux os. and gcc compiler)as follows


In this program ,we have allocated 19 times, but the 20 time allocated failure.So when we use the malloc() and free() functions to manage the memory , we must pay attention to the free. Just like "Press the accelerator easily, but don't forget where is the brake!" 

Next chapter the column of Pointer ,i want to design a  management memory (only with heap) program, although maybe that's a hard task, but i can try. let us keep moving.

These days i'm a little busy , and not enough time to write this blog,  but i 'm sure i will go on to share my experience about the C program in this blog , no matter how hard , how busy ,what a  less time!

Have a nice day,every one !


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值