summaryrefslogtreecommitdiff
path: root/malloc.c
blob: 8bb81a12461067211505497ecc0f46e5c7018040 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
  char *mem_allocation;

  // 動的メモリ確保
  mem_allocation = malloc(20 * sizeof(char));
  if (mem_allocation == NULL) {
    fprintf(stderr, "動的メモリ確保失敗\n");
    return 1;
  } else {
    strlcpy(mem_allocation, "Hi, My sweet hearts", 20);
  }

  printf("格納した文字列 : %s\n", mem_allocation);
  // => "Hi, My sweet hearts"
  free(mem_allocation);
}