Loose-Info.com
Last Update 2012/07/05
TOP - C言語 - stdlib.h - malloc()

指定したバイト数のメモリを確保して、確保されたメモリを参照するポインタを返します。

 戻り値1  = malloc(  引数1  )

戻り値1 :
void *
成功
確保されたメモリを参照するポインタ
失敗
NULL
引数1 :
size_t
メモリを確保するバイト数

(例)
#include <stdio.h> #include <stdlib.h> int main() { int i; char *pc = (char *)malloc(20); if (pc != NULL) { for (i=0; i<10; i++) { *(pc+i) = i + 0x30; } *(pc+i) = '\0'; for (i=0; i<10; i++) { printf("[%d] : %c\n", i, *(pc+i)); } printf("%s\n", pc); free(pc); } else { printf("確保失敗\n"); } return 0; }

実行結果
[0] : 0 [1] : 1 [2] : 2 [3] : 3 [4] : 4 [5] : 5 [6] : 6 [7] : 7 [8] : 8 [9] : 9 0123456789