注册 登录
电子工程世界-论坛 返回首页 EEWORLD首页 频道 EE大学堂 下载中心 Datasheet 专题
土豆爱吃地瓜的个人空间 https://home.eeworld.com.cn/space-uid-547051.html [收藏] [复制] [分享] [RSS]
日志

关于内存的小代码

已有 523 次阅读2014-11-25 09:00 |个人分类:每日代码| memory

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

void GetMemory(char **p,int num)
{
    *p=(char*)malloc(num);
}
int main (void)
{
    char *str=NULL;
    GetMemory(&str,100);
    strcpy(str,"hello");
    printf("%s\n",str);
}
//二级指针可以带出申请的内存

int main()
{
    char *str=(char *)malloc(100);
    strcpy(str,"hello");
    free(str);
    if(str!=NULL)
    {
        strcpy(str,"world");
        printf("%s\n",str);
    }

}
//申请的内存释放后要指空,否则会变成野指针。输出world
char *GetMemory()
{
    char p[]="hello world";
    return p;
}
int main()
{
    char *str=NULL;
    str=GetMemory();
    printf("%s\n",str);

}
//返回局部变量的值,乱码
评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册

热门文章