小弟有个动态内存free的小疑点请高手指教

小弟有个动态内存free的小问题请高手指教
代码如下:
#include<stdlib.h>
#include <stdio.h>
#include<string.h>

struct STUDENT
{
char *name;
int score;
}*pstu1;
int main()
{
pstu1=(struct STUDENT*)malloc(sizeof(struct STUDENT));
pstu1->name=(char*)malloc(sizeof(char));
strcpy(pstu1->name,"Jimmy");
pstu1->score=99;
printf("%s : %d",pstu1->name,pstu1->score);
free(pstu1->name);
free(pstu1);
return 0;
}
调试中free(pstu1->name);这句总是报错说“堆破坏”,求大神解答
------解决思路----------------------
你只分配了一个字节 sizeof(char), 却往里面写入了 "Jimmy" 这么长一串,当然破坏了
------解决思路----------------------
malloc的时候尽量申请的大小大于要写入的长度
------解决思路----------------------
引用:
代码如下:
#include<stdlib.h>
#include <stdio.h>
#include<string.h>

struct STUDENT
{
char *name;
int score;
}*pstu1;
int main()
{
pstu1=(struct STUDENT*)malloc(sizeof(struct STUDENT));
pstu1->name=(char*)malloc(sizeof(char));   //分配的空间不够
strcpy(pstu1->name,"Jimmy");
pstu1->score=99;
printf("%s : %d",pstu1->name,pstu1->score);
free(pstu1->name);
free(pstu1);
return 0;
}
调试中free(pstu1->name);这句总是报错说“堆破坏”,求大神解答

可以把char* 换成定长的char数组
------解决思路----------------------
引用:
Quote: 引用:

Quote: 引用:

代码如下:
#include<stdlib.h>
#include <stdio.h>
#include<string.h>

struct STUDENT
{
char *name;
int score;
}*pstu1;
int main()
{
pstu1=(struct STUDENT*)malloc(sizeof(struct STUDENT));
pstu1->name=(char*)malloc(sizeof(char));   //分配的空间不够
strcpy(pstu1->name,"Jimmy");
pstu1->score=99;
printf("%s : %d",pstu1->name,pstu1->score);
free(pstu1->name);
free(pstu1);
return 0;
}
调试中free(pstu1->name);这句总是报错说“堆破坏”,求大神解答

可以把char* 换成定长的char数组

请问换成char数组以后,数组内的“Jimmy”字符串储存在那块内存中?是堆中还是常量储存区中?

如果是局部的数组,就是栈里面,否则就是堆立面。
------解决思路----------------------
如上,二选一:
#define MAX_NAME_LENGTH 32

struct STUDENT {
    char *name;
    int score;
} *pstu1;

int main()
{
    pstu1 = (struct STUDENT*)malloc(sizeof(struct STUDENT));
    pstu1->name = (char*)malloc(sizeof(char) * MAX_NAME_LENGTH);
    strncpy(pstu1->name, "Jimmy", MAX_NAME_LENGTH);
    pstu1->score = 99;
    printf("%s: %d\n", pstu1->name, pstu1->score);
    free(pstu1->name);
    free(pstu1);
    return 0;
}
#define MAX_NAME_LENGTH 32

struct STUDENT {
    char name[MAX_NAME_LENGTH];
    int score;
} *pstu1;

int main()
{
    pstu1 = (struct STUDENT*)malloc(sizeof(struct STUDENT));
    strncpy(pstu1->name, "Jimmy", MAX_NAME_LENGTH);
    pstu1->score = 99;
    printf("%s: %d\n", pstu1->name, pstu1->score);
    free(pstu1);
    return 0;
}

------解决思路----------------------
分配足够内存即可:

//pstu1->name = (char*)malloc(sizeof(char));
pstu1->name = (char*)malloc(sizeof(char) * 10);

------解决思路----------------------
开辟的内存太小,
不够赋值。
所以就报错了。
开辟的内存要略大于你所写数据。
------解决思路----------------------
现在计算机程序运行内存实际上为虚拟内存,你用new申请的内存为一个字节,但是你写入的不只一个字节,在linux系统中(其他系统类似),堆其实是个链表,维护的有上一个内存块的位置,下一个内存块的位置,当前块的大小等信息,你越界放入数据以后,存取的时候没有问题,但是你破坏了维护结构,释放的时候就会出问题
------解决思路----------------------
楼主的代码:

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

struct STUDENT
{
char *name;
int score;
}*pstu1;
int main()
{
pstu1=(struct STUDENT*)malloc(sizeof(struct STUDENT));
pstu1->name=(char*)malloc(sizeof(char));
strcpy(pstu1->name,"Jimmy");
pstu1->score=99;
printf("%s : %d",pstu1->name,pstu1->score);
free(pstu1->name);
free(pstu1);
return 0;
}

改后的代码

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

struct STUDENT
{
char *name;
int score;
}*pstu1;
int main()
{
pstu1=(struct STUDENT*)malloc(sizeof(struct STUDENT));
pstu1->name="Jimmy";
pstu1->score=99;
printf("%s : %d",pstu1->name,pstu1->score);
free(pstu1);
return 0;
}

楼主的问题:
pstu1=(struct STUDENT*)malloc(sizeof(struct STUDENT));  //指针变量pstu1分配了内存。
pstu1->name=(char*)malloc(sizeof(char)); // //指针变量pstu1中的name成员  分配内存。可能此不与上步有相同内存部分
free(pstu1->name);是单独释放结构体所占内存的name成员部分。

------解决思路----------------------
pstu1->name指向的是 一个字节的字符地址。你又在这个字节里面放一个字符串,肯定不行啊