标题:动态分配内存问题求分析
取消只看楼主
飞天大烧卖
Rank: 2
等 级:论坛游民
帖 子:45
专家分:27
注 册:2020-3-26
结帖率:100%
已结贴  问题点数:20 回复次数:0 
动态分配内存问题求分析

#include <stdio.h>
#include <stdlib.h>
struct xue
{
    int age;
    char *name;
};
int main(void)
{
    struct xue t,t1;
    t.age=10;
    if((t.name=(char *)malloc(sizeof(char)))==NULL)//不在堆区分配也行,我就是想研究一下malloc
    {
        printf("错误");
        return 0;
    }
    t.name="huzli";
    t1.age=20;
    if((t1.name=(char *)malloc(sizeof(char)))==NULL)
    {
        printf("错误");
        return 0;
    }
    t1.name="sjl";
    printf("%3d,%3s,%3d,%3s",t.age,t.name,t1.age,t1.name);
    free(t1.name);//这两行free()不加就不会报错,为啥?
    free(t.name);
    return 0;
}
搜索更多相关主题的帖子: 内存 name age char 动态分配 
2020-04-09 16:29



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-500961-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.659157 second(s), 9 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved