[C言語] malloc, calloc, realloc, free, strdup

メモリの動的確保系の関数のメモ

malloc, calloc, free

malloc はメモリを動的に確保する。 calloc も同じだけど確保したメモリを 0 で埋める free はメモリを解放する。

  long *m = (long *)malloc(10 * sizeof(long));
  long *c = (long *)calloc(10, sizeof(long));

  if ( ! m ) {
    // メモリ確保失敗
    exit(EXIT_FAILURE);
  }

  if ( ! c ) {
    // メモリ確保失敗
    exit(EXIT_FAILURE);
  }

  for (int i = 0; i < 10; i++)
  {
    printf("%ld,", m[i]); // 値は不定
  }

  puts("");

  for (int i = 0; i < 10; i++)
  {
    printf("%ld,", c[i]); // すべて 0
  }

  free(m);
  free(c);

メモリ確保する関数は失敗時に 0 を返す点に注意すること。

malloc, calloc, realloc したら必ず if ( ptr ) みたいなチェックを入れるのが基本。

free には NULL を渡すことも可能で、その場合は何も動作しない。つまり NULL チェックは必要ないということ

realloc

メモリの再確保を行う関数。

  • realloc はコピーされる部分以外は初期化されない。
  • 第2引数に再確保するメモリのサイズを指定する。元のメモリより小さい値を指定することも可能。
  • 第1引数に NULL を渡すと malloc と同様の動作をする。
  • reallocf なんてものも存在する。これはメモリ確保失敗時であっても渡されたポインタを free する点が異なる。つまりかなり便利じゃん。

    char *m = (char *)malloc(10);
    if (m) {
    memset(m, 'x', 10 - 1);
    m[10 - 1] = '\0';
    } else {
    exit(EXIT_FAILURE);
    }
    
    char *r = (char *)realloc(m, 100);
    if (r) {
    puts(r); // xxxxxxxxx
    
    free(r);
    // free(m) - realloc 成功時は m は既に解放されているのでこの処理は不要
    } else {
    free(m); // realloc に失敗した場合は m は解放する必要がある
    }
    
    char *m = (char *)malloc(100);
    memset(m, 'x', 100);
    m[10] = 0;
    
    if (m)
    {
    printf("%ld \n", malloc_size(m)); // 112
    
    char *r = (char *)realloc(m, 10);
    if (r)
    {
      printf("%ld \n", malloc_size(r)); // 16
      puts(r); // xxxxxxxxxx
    
      free(r);
    }
    else
    {
      free(m);
    }
    }
    

strdup

文字列を新しく確保したメモリにコピーしてポインタを返す関数。

戻り値は free で解放する必要がある。

char *pCopy = strdup("ABCDE");
if (pCopy)
{
  puts(pCopy); // ABCDE
  free(pCopy);
}

ANSI C 標準ではないけどやってることは malloc してコピーしてるだけなので自作するのは容易

メモリ確保関数は指定したサイズぴったりのサイズを確保する訳ではない。

というか 0 を指定してもある程度のサイズで返してくる。

だからといって指定したサイズを超えた部分は触ったらダメらしいけども。。

  void *p;
  long mallocSize = 0;
  for (int i = 0; i < 1000000; i++)
  {
    p = malloc(i);
    if (p)
    {
      long newSize = malloc_size(p);
      if (mallocSize < newSize)
      {
        printf("i = %d, malloc_size = %ld delta = %ld \n",
               i,
               newSize,
               newSize - mallocSize);
        mallocSize = newSize;
      }
      free(p);
    }
  }

結果の一例

i = 0, malloc_size = 16 delta = 16
i = 17, malloc_size = 32 delta = 16
i = 33, malloc_size = 48 delta = 16
i = 49, malloc_size = 64 delta = 16
i = 65, malloc_size = 80 delta = 16
i = 81, malloc_size = 96 delta = 16
i = 97, malloc_size = 112 delta = 16
i = 113, malloc_size = 128 delta = 16
i = 129, malloc_size = 144 delta = 16
i = 145, malloc_size = 160 delta = 16
i = 161, malloc_size = 176 delta = 16
i = 177, malloc_size = 192 delta = 16
i = 193, malloc_size = 208 delta = 16
i = 209, malloc_size = 224 delta = 16
i = 225, malloc_size = 240 delta = 16
i = 241, malloc_size = 256 delta = 16
i = 257, malloc_size = 272 delta = 16
i = 273, malloc_size = 288 delta = 16
i = 289, malloc_size = 304 delta = 16
i = 305, malloc_size = 320 delta = 16
i = 321, malloc_size = 336 delta = 16
i = 337, malloc_size = 352 delta = 16
i = 353, malloc_size = 368 delta = 16
i = 369, malloc_size = 384 delta = 16
i = 385, malloc_size = 400 delta = 16
i = 401, malloc_size = 416 delta = 16
i = 417, malloc_size = 432 delta = 16
i = 433, malloc_size = 448 delta = 16
i = 449, malloc_size = 464 delta = 16
i = 465, malloc_size = 480 delta = 16
i = 481, malloc_size = 496 delta = 16
i = 497, malloc_size = 512 delta = 16
i = 513, malloc_size = 528 delta = 16
i = 529, malloc_size = 544 delta = 16
i = 545, malloc_size = 560 delta = 16
i = 561, malloc_size = 576 delta = 16
i = 577, malloc_size = 592 delta = 16
i = 593, malloc_size = 608 delta = 16
i = 609, malloc_size = 624 delta = 16
i = 625, malloc_size = 640 delta = 16
i = 641, malloc_size = 656 delta = 16
i = 657, malloc_size = 672 delta = 16
i = 673, malloc_size = 688 delta = 16
i = 689, malloc_size = 704 delta = 16
i = 705, malloc_size = 720 delta = 16
i = 721, malloc_size = 736 delta = 16
i = 737, malloc_size = 752 delta = 16
i = 753, malloc_size = 768 delta = 16
i = 769, malloc_size = 784 delta = 16
i = 785, malloc_size = 800 delta = 16
i = 801, malloc_size = 816 delta = 16
i = 817, malloc_size = 832 delta = 16
i = 833, malloc_size = 848 delta = 16
i = 849, malloc_size = 864 delta = 16
i = 865, malloc_size = 880 delta = 16
i = 881, malloc_size = 896 delta = 16
i = 897, malloc_size = 912 delta = 16
i = 913, malloc_size = 928 delta = 16
i = 929, malloc_size = 944 delta = 16
i = 945, malloc_size = 960 delta = 16
i = 961, malloc_size = 976 delta = 16
i = 977, malloc_size = 992 delta = 16
i = 993, malloc_size = 1008 delta = 16
i = 1009, malloc_size = 1024 delta = 16
i = 1025, malloc_size = 1536 delta = 512
i = 1537, malloc_size = 2048 delta = 512
i = 2049, malloc_size = 2560 delta = 512
i = 2561, malloc_size = 3072 delta = 512
i = 3073, malloc_size = 3584 delta = 512
i = 3585, malloc_size = 4096 delta = 512
i = 4097, malloc_size = 4608 delta = 512
i = 4609, malloc_size = 5120 delta = 512
i = 5121, malloc_size = 5632 delta = 512
i = 5633, malloc_size = 6144 delta = 512
i = 6145, malloc_size = 6656 delta = 512
i = 6657, malloc_size = 7168 delta = 512
i = 7169, malloc_size = 7680 delta = 512
i = 7681, malloc_size = 8192 delta = 512
i = 8193, malloc_size = 8704 delta = 512
i = 8705, malloc_size = 9216 delta = 512
i = 9217, malloc_size = 9728 delta = 512
i = 9729, malloc_size = 10240 delta = 512
i = 10241, malloc_size = 10752 delta = 512
i = 10753, malloc_size = 11264 delta = 512
i = 11265, malloc_size = 11776 delta = 512
i = 11777, malloc_size = 12288 delta = 512
i = 12289, malloc_size = 12800 delta = 512
i = 12801, malloc_size = 13312 delta = 512
i = 13313, malloc_size = 13824 delta = 512
i = 13825, malloc_size = 14336 delta = 512
i = 14337, malloc_size = 14848 delta = 512
i = 14849, malloc_size = 15360 delta = 512
i = 15361, malloc_size = 15872 delta = 512
i = 15873, malloc_size = 16384 delta = 512
i = 16385, malloc_size = 16896 delta = 512
i = 16897, malloc_size = 17408 delta = 512
i = 17409, malloc_size = 17920 delta = 512
i = 17921, malloc_size = 18432 delta = 512
i = 18433, malloc_size = 18944 delta = 512
i = 18945, malloc_size = 19456 delta = 512
i = 19457, malloc_size = 19968 delta = 512
i = 19969, malloc_size = 20480 delta = 512
i = 20481, malloc_size = 20992 delta = 512
i = 20993, malloc_size = 21504 delta = 512
i = 21505, malloc_size = 22016 delta = 512
i = 22017, malloc_size = 22528 delta = 512
i = 22529, malloc_size = 23040 delta = 512
i = 23041, malloc_size = 23552 delta = 512
i = 23553, malloc_size = 24064 delta = 512
i = 24065, malloc_size = 24576 delta = 512
i = 24577, malloc_size = 25088 delta = 512
i = 25089, malloc_size = 25600 delta = 512
i = 25601, malloc_size = 26112 delta = 512
i = 26113, malloc_size = 26624 delta = 512
i = 26625, malloc_size = 27136 delta = 512
i = 27137, malloc_size = 27648 delta = 512
i = 27649, malloc_size = 28160 delta = 512
i = 28161, malloc_size = 28672 delta = 512
i = 28673, malloc_size = 29184 delta = 512
i = 29185, malloc_size = 29696 delta = 512
i = 29697, malloc_size = 30208 delta = 512
i = 30209, malloc_size = 30720 delta = 512
i = 30721, malloc_size = 31232 delta = 512
i = 31233, malloc_size = 31744 delta = 512
i = 31745, malloc_size = 32256 delta = 512
i = 32257, malloc_size = 32768 delta = 512
i = 32769, malloc_size = 33280 delta = 512
i = 33281, malloc_size = 33792 delta = 512
i = 33793, malloc_size = 34304 delta = 512
i = 34305, malloc_size = 34816 delta = 512
i = 34817, malloc_size = 35328 delta = 512
i = 35329, malloc_size = 35840 delta = 512
i = 35841, malloc_size = 36352 delta = 512
i = 36353, malloc_size = 36864 delta = 512
i = 36865, malloc_size = 37376 delta = 512
i = 37377, malloc_size = 37888 delta = 512
i = 37889, malloc_size = 38400 delta = 512
i = 38401, malloc_size = 38912 delta = 512
i = 38913, malloc_size = 39424 delta = 512
i = 39425, malloc_size = 39936 delta = 512
i = 39937, malloc_size = 40448 delta = 512
i = 40449, malloc_size = 40960 delta = 512
i = 40961, malloc_size = 41472 delta = 512
i = 41473, malloc_size = 41984 delta = 512
i = 41985, malloc_size = 42496 delta = 512
i = 42497, malloc_size = 43008 delta = 512
i = 43009, malloc_size = 43520 delta = 512
i = 43521, malloc_size = 44032 delta = 512
i = 44033, malloc_size = 44544 delta = 512
i = 44545, malloc_size = 45056 delta = 512
i = 45057, malloc_size = 45568 delta = 512
i = 45569, malloc_size = 46080 delta = 512
i = 46081, malloc_size = 46592 delta = 512
i = 46593, malloc_size = 47104 delta = 512
i = 47105, malloc_size = 47616 delta = 512
i = 47617, malloc_size = 48128 delta = 512
i = 48129, malloc_size = 48640 delta = 512
i = 48641, malloc_size = 49152 delta = 512
i = 49153, malloc_size = 49664 delta = 512
i = 49665, malloc_size = 50176 delta = 512
i = 50177, malloc_size = 50688 delta = 512
i = 50689, malloc_size = 51200 delta = 512
i = 51201, malloc_size = 51712 delta = 512
i = 51713, malloc_size = 52224 delta = 512
i = 52225, malloc_size = 52736 delta = 512
i = 52737, malloc_size = 53248 delta = 512
i = 53249, malloc_size = 53760 delta = 512
i = 53761, malloc_size = 54272 delta = 512
i = 54273, malloc_size = 54784 delta = 512
i = 54785, malloc_size = 55296 delta = 512
i = 55297, malloc_size = 55808 delta = 512
i = 55809, malloc_size = 56320 delta = 512
i = 56321, malloc_size = 56832 delta = 512
i = 56833, malloc_size = 57344 delta = 512
i = 57345, malloc_size = 57856 delta = 512
i = 57857, malloc_size = 58368 delta = 512
i = 58369, malloc_size = 58880 delta = 512
i = 58881, malloc_size = 59392 delta = 512
i = 59393, malloc_size = 59904 delta = 512
i = 59905, malloc_size = 60416 delta = 512
i = 60417, malloc_size = 60928 delta = 512
i = 60929, malloc_size = 61440 delta = 512
i = 61441, malloc_size = 61952 delta = 512
i = 61953, malloc_size = 62464 delta = 512
i = 62465, malloc_size = 62976 delta = 512
i = 62977, malloc_size = 63488 delta = 512
i = 63489, malloc_size = 64000 delta = 512
i = 64001, malloc_size = 64512 delta = 512
i = 64513, malloc_size = 65024 delta = 512
i = 65025, malloc_size = 65536 delta = 512
i = 65537, malloc_size = 66048 delta = 512
i = 66049, malloc_size = 66560 delta = 512
i = 66561, malloc_size = 67072 delta = 512
i = 67073, malloc_size = 67584 delta = 512
i = 67585, malloc_size = 68096 delta = 512
i = 68097, malloc_size = 68608 delta = 512
i = 68609, malloc_size = 69120 delta = 512
i = 69121, malloc_size = 69632 delta = 512
i = 69633, malloc_size = 70144 delta = 512
i = 70145, malloc_size = 70656 delta = 512
i = 70657, malloc_size = 71168 delta = 512
i = 71169, malloc_size = 71680 delta = 512
i = 71681, malloc_size = 72192 delta = 512
i = 72193, malloc_size = 72704 delta = 512
i = 72705, malloc_size = 73216 delta = 512
i = 73217, malloc_size = 73728 delta = 512
i = 73729, malloc_size = 74240 delta = 512
i = 74241, malloc_size = 74752 delta = 512
i = 74753, malloc_size = 75264 delta = 512
i = 75265, malloc_size = 75776 delta = 512
i = 75777, malloc_size = 76288 delta = 512
i = 76289, malloc_size = 76800 delta = 512
i = 76801, malloc_size = 77312 delta = 512
i = 77313, malloc_size = 77824 delta = 512
i = 77825, malloc_size = 78336 delta = 512
i = 78337, malloc_size = 78848 delta = 512
i = 78849, malloc_size = 79360 delta = 512
i = 79361, malloc_size = 79872 delta = 512
i = 79873, malloc_size = 80384 delta = 512
i = 80385, malloc_size = 80896 delta = 512
i = 80897, malloc_size = 81408 delta = 512
i = 81409, malloc_size = 81920 delta = 512
i = 81921, malloc_size = 82432 delta = 512
i = 82433, malloc_size = 82944 delta = 512
i = 82945, malloc_size = 83456 delta = 512
i = 83457, malloc_size = 83968 delta = 512
i = 83969, malloc_size = 84480 delta = 512
i = 84481, malloc_size = 84992 delta = 512
i = 84993, malloc_size = 85504 delta = 512
i = 85505, malloc_size = 86016 delta = 512
i = 86017, malloc_size = 86528 delta = 512
i = 86529, malloc_size = 87040 delta = 512
i = 87041, malloc_size = 87552 delta = 512
i = 87553, malloc_size = 88064 delta = 512
i = 88065, malloc_size = 88576 delta = 512
i = 88577, malloc_size = 89088 delta = 512
i = 89089, malloc_size = 89600 delta = 512
i = 89601, malloc_size = 90112 delta = 512
i = 90113, malloc_size = 90624 delta = 512
i = 90625, malloc_size = 91136 delta = 512
i = 91137, malloc_size = 91648 delta = 512
i = 91649, malloc_size = 92160 delta = 512
i = 92161, malloc_size = 92672 delta = 512
i = 92673, malloc_size = 93184 delta = 512
i = 93185, malloc_size = 93696 delta = 512
i = 93697, malloc_size = 94208 delta = 512
i = 94209, malloc_size = 94720 delta = 512
i = 94721, malloc_size = 95232 delta = 512
i = 95233, malloc_size = 95744 delta = 512
i = 95745, malloc_size = 96256 delta = 512
i = 96257, malloc_size = 96768 delta = 512
i = 96769, malloc_size = 97280 delta = 512
i = 97281, malloc_size = 97792 delta = 512
i = 97793, malloc_size = 98304 delta = 512
i = 98305, malloc_size = 98816 delta = 512
i = 98817, malloc_size = 99328 delta = 512
i = 99329, malloc_size = 99840 delta = 512
i = 99841, malloc_size = 100352 delta = 512
i = 100353, malloc_size = 100864 delta = 512
i = 100865, malloc_size = 101376 delta = 512
i = 101377, malloc_size = 101888 delta = 512
i = 101889, malloc_size = 102400 delta = 512
i = 102401, malloc_size = 102912 delta = 512
i = 102913, malloc_size = 103424 delta = 512
i = 103425, malloc_size = 103936 delta = 512
i = 103937, malloc_size = 104448 delta = 512
i = 104449, malloc_size = 104960 delta = 512
i = 104961, malloc_size = 105472 delta = 512
i = 105473, malloc_size = 105984 delta = 512
i = 105985, malloc_size = 106496 delta = 512
i = 106497, malloc_size = 107008 delta = 512
i = 107009, malloc_size = 107520 delta = 512
i = 107521, malloc_size = 108032 delta = 512
i = 108033, malloc_size = 108544 delta = 512
i = 108545, malloc_size = 109056 delta = 512
i = 109057, malloc_size = 109568 delta = 512
i = 109569, malloc_size = 110080 delta = 512
i = 110081, malloc_size = 110592 delta = 512
i = 110593, malloc_size = 111104 delta = 512
i = 111105, malloc_size = 111616 delta = 512
i = 111617, malloc_size = 112128 delta = 512
i = 112129, malloc_size = 112640 delta = 512
i = 112641, malloc_size = 113152 delta = 512
i = 113153, malloc_size = 113664 delta = 512
i = 113665, malloc_size = 114176 delta = 512
i = 114177, malloc_size = 114688 delta = 512
i = 114689, malloc_size = 115200 delta = 512
i = 115201, malloc_size = 115712 delta = 512
i = 115713, malloc_size = 116224 delta = 512
i = 116225, malloc_size = 116736 delta = 512
i = 116737, malloc_size = 117248 delta = 512
i = 117249, malloc_size = 117760 delta = 512
i = 117761, malloc_size = 118272 delta = 512
i = 118273, malloc_size = 118784 delta = 512
i = 118785, malloc_size = 119296 delta = 512
i = 119297, malloc_size = 119808 delta = 512
i = 119809, malloc_size = 120320 delta = 512
i = 120321, malloc_size = 120832 delta = 512
i = 120833, malloc_size = 121344 delta = 512
i = 121345, malloc_size = 121856 delta = 512
i = 121857, malloc_size = 122368 delta = 512
i = 122369, malloc_size = 122880 delta = 512
i = 122881, malloc_size = 123392 delta = 512
i = 123393, malloc_size = 123904 delta = 512
i = 123905, malloc_size = 124416 delta = 512
i = 124417, malloc_size = 124928 delta = 512
i = 124929, malloc_size = 125440 delta = 512
i = 125441, malloc_size = 125952 delta = 512
i = 125953, malloc_size = 126464 delta = 512
i = 126465, malloc_size = 126976 delta = 512
i = 126977, malloc_size = 127488 delta = 512
i = 127489, malloc_size = 128000 delta = 512
i = 128001, malloc_size = 128512 delta = 512
i = 128513, malloc_size = 129024 delta = 512
i = 129025, malloc_size = 129536 delta = 512
i = 129537, malloc_size = 130048 delta = 512
i = 130049, malloc_size = 131072 delta = 1024
i = 131073, malloc_size = 135168 delta = 4096
Share
関連記事