callocプロジェクト


※上記の広告は60日以上更新のないWIKIに表示されています。更新することで広告が下部へ移動します。

calloc.c

#include <stdio.h>
#include <stdlib.h>
 
int main(void){
 
  int *p_malloc;
  int *p_calloc;
  int i;
 
  p_malloc = NULL;
  p_calloc = NULL;
 
  printf("malloc example.\n");
 
  printf("first call malloc.\n");
 
  p_malloc = (int *)malloc(sizeof(int) * 5);
 
  if (p_malloc != NULL){
    for (i = 0; i < 5; i++){
      (*(p_malloc + i)) = i;
      printf("p_malloc[%d] = %d(input now)\n", i, (*(p_malloc + i)));
    }
  }
 
  printf("first call free.\n");
 
  free(p_malloc);
  p_malloc = NULL;
 
  printf("second call malloc.\n");
 
  p_malloc = (int *)malloc(sizeof(int) * 5);
 
  if (p_malloc != NULL){
    for (i = 0; i < 5; i++){
      printf("p_malloc[%d] = %d(no input)\n", i, (*(p_malloc + i)));
    }
  }
 
  printf("second call free.\n");
 
  free(p_malloc);
  p_malloc = NULL;
 
  printf("calloc example.\n");
 
  printf("first call calloc.\n");
 
  p_calloc = (int *)calloc(5, sizeof(int));
 
  if (p_calloc != NULL){
    for (i = 0; i < 5; i++){
      (*(p_calloc + i)) = i;
      printf("p_calloc[%d] = %d(input now)\n", i, (*(p_calloc + i)));
    }
  }
 
  printf("first call free.\n");
 
  free(p_calloc);
  p_calloc = NULL;
 
  printf("second call calloc.\n");
 
  p_calloc = (int *)calloc(5, sizeof(int));
 
  if (p_calloc != NULL){
    for (i = 0; i < 5; i++){
      printf("p_calloc[%d] = %d(no input)\n", i, (*(p_calloc + i)));
    }
  }
 
  printf("second call free.\n");
 
  free(p_calloc);
  p_calloc = NULL;
 
  return 0;
 
}