C言語 mallocの使い方|動的メモリ確保とfree

C言語 mallocの使い方|動的メモリ確保とfree (c言語 malloc): practical C programming guide with runnable standard C17 source code that you can edit, compile, and test directly in your browser.

1. C言語 mallocの使い方|動的メモリ確保とfree

This guide covers c言語 malloc in standard C (C17) with clear syntax rules and practical compiler patterns. You can run and modify the complete C program below in the online GCC compiler without installing a local IDE.

2. c言語 malloc — Syntax & Best Practices

When working with c言語 malloc, always compile with -Wall -Wextra, initialize variables before reading them, and verify input/boundary conditions to prevent undefined behavior.

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int *arr = malloc(3 * sizeof *arr);
    if (!arr) return 1;
    arr[0] = 10; arr[1] = 20; arr[2] = 30;
    printf("%d %d %d\n", arr[0], arr[1], arr[2]);
    free(arr);
    return 0;
}