zoukankan      html  css  js  c++  java
  • c程序设计语言_习题8-6_利用malloc()函数,重新实现c语言的库函数calloc()

    The standard library function calloc(n,size) returns a pointer to n objects of size size , with the storage initialized to zero. Write calloc , by calling malloc or by modifying it.

    /*
        Exercise 8.6. The standard library function calloc(n, size) returns a pointer to n objects
              of size size, with the storage initialised to zero. Write calloc, by calling 
              malloc or by modifying it.
    
        Author: Bryan Williams
    
    */
    
    #include <stdlib.h>
    #include <string.h>
    
    /*
      Decided to re-use malloc for this because :
        1) If the implementation of malloc and the memory management layer changes, this will be ok.
        2) Code re-use is great.
    
    */
    void *mycalloc(size_t nmemb, size_t size)
    {
      void *Result = NULL;
    
      /* use malloc to get the memory */
      Result = malloc(nmemb * size);
    
      /* and clear the memory on successful allocation */
      if(NULL != Result)
      {
        memset(Result, 0x00, nmemb * size);
      }
    
      /* and return the result */
      return Result;
    }
    
    /* simple test driver, by RJH */
    
    #include <stdio.h>
    
    int main(void)
    {
      int *p = NULL;
      int i = 0;
    
      p = mycalloc(100, sizeof *p);
      if(NULL == p)
      {
        printf("mycalloc returned NULL.
    ");
      }
      else
      {
        for(i = 0; i < 100; i++)
        {
          printf("%08X ", p[i]);
          if(i % 8 == 7)
          {
        printf("
    ");
          }
        }
        printf("
    ");
        free(p);
      }
    
      return 0;
    }
  • 相关阅读:
    定时器
    SysTick
    13.看门狗
    12.FSMC
    11.中断
    ##HTML行内块元素好迷啊!!
    ##HTML基础
    ##CS3动画效果
    ##管家婆项目(tools层)
    ##管家婆项目(app层)
  • 原文地址:https://www.cnblogs.com/haore147/p/3648301.html
Copyright © 2011-2022 走看看