メモリの再割り当て

メモリの再割り当て list_29.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <malloc.h>

int main()
{
    int i;
    long *buffer;
    size_t size=sizeof(long)*10;
    char buf[256];
    memset(buf,NULL,sizeof(char)*256);

    /* test of realloc */
    printf("**test of realloc\n");
    if( (buffer = (long *)malloc(size)) == NULL )
        exit( 1 );
    for(i=0;i<10;i++) buffer[i]=i;
    
    /* 再割り当てしてサイズを表示します。 */

    if( (buffer = realloc(buffer, size + (10 * sizeof( long )) )) 
        ==  NULL )
        exit( 1 );
    for(i=10;i<20;i++) buffer[i]=i;

    for(i=0;i<20;i++) printf("%d ",buffer[i]);
    printf("\n");
    
    free(buffer);
    return 0;
}
実行結果
Gami[1487]% list_29
**test of realloc
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Gami[1488]%
inserted by FC2 system