diff options
author | haturatu <taro@eyes4you.org> | 2024-12-12 18:53:25 +0900 |
---|---|---|
committer | haturatu <taro@eyes4you.org> | 2024-12-12 18:53:25 +0900 |
commit | 2eb858c51eaf50a780ee47227963fcb36aac4102 (patch) | |
tree | 24691dc5a92e1eedee512e98a4d140b836b656d8 /malloc.c |
first commit
Diffstat (limited to 'malloc.c')
-rw-r--r-- | malloc.c | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/malloc.c b/malloc.c new file mode 100644 index 0000000..8bb81a1 --- /dev/null +++ b/malloc.c @@ -0,0 +1,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); +} |