C 语言中堆栈的理解

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *Temp(const char *src,int len)   //数组是在栈中分配,返回时,帧栈中的内容已经释放,会产生不可预料的结果
{
    char temp[15];
    memcpy(temp,src,len);
    return temp;
}

char *TempM(const char *src,int len) //在函数里面申请动态内存,很容易忘记释放内存,导致内存泄漏
{
    int a=3,b=4;
    char *temp = (char*)malloc(len);
    memcpy(temp,src,len);
    return temp;
}

void main()
{
    char *temp = "hello world";
    char *dest = Temp(temp,15);
    char *forever = TempM(temp,15);
    printf("%s\n",dest);               //出现乱码
    printf("%s\n",forever);
}
原文地址:https://www.cnblogs.com/hzhida/p/2805925.html