2017年1月4日 星期三

[C][Concept] malloc

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

int main()
{
    char *log;

    log = malloc(256);

    strcpy(log, "happy");
    printf("log = %s\n",log);
}

Result

log = happy

Compare

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

int main()
{
    char *log;

    strcpy(log, "happy");
    printf("log = %s\n",log);
}

Result

Segmentation fault (core dumped)

0 意見:

張貼留言