美好的一天!我正确使用malloc吗?
我需要建立一个学生名单系统使用malloc ....为了提高效率,我们的教授要求我们把它用在一个结构,所以我创建了一个结构如下:
struct student {
char studentID[6];
char name[31];
char course [6];
};
struct student *array[30];
每当我添加一个记录,那就是当我使用malloc ...
array[recordCtr]=(struct student*)malloc(sizeof(struct student));
recordCtr++;
然后我释放它像这样。
for(i = 0; i < recordCtr; i++){
free(array[i]);
}
我是否正确使用malloc?如果我像这样释放它而不是上面的循环,会有什么效果。
free(array);
在此先感谢。您的意见将不胜感激。
是的。它是一个全局变量 – newbie 2010-12-03 08:44:00