zoukankan      html  css  js  c++  java
  • 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);
    }
  • 相关阅读:
    团队冲刺第十天
    每日学习
    每日学习
    团队冲刺第九天
    每日学习
    2021.4.26
    2021.4.23
    2021.4.22
    2021.4.21
    2021.4.20
  • 原文地址:https://www.cnblogs.com/hzhida/p/2805925.html
Copyright © 2011-2022 走看看