How to free this memory?

B

bugzilla

hi, there,

Please help me to figure out this problem. In the following code
segment, how to free the momory allocated to "head" in {STULST*
CreateList()} in main() function? thanks

///////////////////////////////////////////

#include <stdio.h>

typedef struct
{
int num;
float score;
}STUDENT;

typedef struct STULST
{
STUDENT *stu;
struct STULST* next;
}STULST;

STULST* CreateList()
{

STULST *head;
head=(STULST *)malloc(sizeof(STULST));
head->stu=NULL;
head->next=NULL;
return head;
}


int main()
{
STULST* head;

head=CreateList();
/// how to free the memory of head;

return 0;


}
 
B

bjrnove

Hi.

One thing first. You must check the result of malloc and return if NULL
if you don't want your program to crash every once in a while!!

The answer on you question is simply:

free(head);

But again, you should check if it's NULL.
 
B

bjrnove

One more thing: If you are allocating memory for each item in the
linklist you should consither making a FreeList function. It could look
something like this:

void FreeList(STULST* head)
{
while(head){
STULST* tmp = head;
if(head->stu) free(head->stu);
head = head->next;
free(tmp);
}
}
 
E

Emmanuel Delahaye

bugzilla wrote on 29/03/05 :
hi, there,

Please help me to figure out this problem. In the following code
segment, how to free the momory allocated to "head" in {STULST*
CreateList()} in main() function? thanks

///////////////////////////////////////////

#include <stdio.h>

typedef struct
{
int num;
float score;
}STUDENT;

typedef struct STULST
{
STUDENT *stu;
struct STULST* next;
}STULST;

STULST* CreateList()
{

STULST *head;
head=(STULST *)malloc(sizeof(STULST));

You don't need this cast, but you need a prototype for malloc().

Best offer: include <stdlib.h>

Warning. malloc() can fail. You must test the value before using it.
NULL means allocation error.

Here is by bundle:


STULST *head = malloc (sizeof *head);

if (head != NULL)
{

head->stu=NULL;
head->next=NULL;

}
return head;
}
int main()
{
STULST* head;

head=CreateList();
/// how to free the memory of head;

return 0;

free (head), head = NULL;

Of course, if you have a list of more than one element, it requires a
little more of brainstorming.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.
 
A

Al Bowers

bugzilla said:
hi, there,

Please help me to figure out this problem. In the following code
segment, how to free the momory allocated to "head" in {STULST*
CreateList()} in main() function? thanks

I think you rethink your function CreateList. Make a function
that expands the linked list as you need to add a student.
See the function AddStudent below. To free the list see
function FreeStudents.
///////////////////////////////////////////

#include <stdio.h>

typedef struct
{
int num;
float score;
}STUDENT;

typedef struct STULST
{
STUDENT *stu;
STUDENT stu;
There is no need for dynamic allocation.
struct STULST* next;
}STULST;

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

typedef struct STUDENT
{
int num;
float score;
}STUDENT;

typedef struct STULST
{
STUDENT stu;
struct STULST* next;
}STULST;

int AddStudent(STULST **head, int num, float score);
void PrintStudents(STULST *p);
void FreeStudents(STULST **p);

int main(void)
{
STULST* head = NULL;

AddStudent(&head, 20, 89.2f);
AddStudent(&head, 30, 79.4f);
AddStudent(&head, 40, 92.8f);
puts("\tThe Student List:");
PrintStudents(head);
FreeStudents(&head);
return 0;
}

int AddStudent(STULST **head, int num, float score)
{
STULST *tmp;

if((tmp = malloc(sizeof *tmp)) == NULL)
return 0;
tmp->stu.num = num;
tmp->stu.score = score;
tmp->next = *head;
*head = tmp;
return 1;
}

void PrintStudents(STULST *p)
{
for( ; p ; p = p->next)
printf("num = %d score = %.1f\n",
p->stu.num, p->stu.score);
return;
}

void FreeStudents(STULST **p)
{
STULST *tmp;

for( ; *p; *p = tmp)
{
tmp = (*p)->next;
free(*p);
}
return;
}
 
D

Default User

bjrnove said:
Hi.

One thing first. You must check the result of malloc and return if NULL
if you don't want your program to crash every once in a while!!

The answer on you question is simply:

free(head);

But again, you should check if it's NULL.


No. Doing a free() on a null pointer is safe, it is a no-op.



Brian
 
M

mike

bjrnove said:
One more thing: If you are allocating memory for each item in the
linklist you should consither making a FreeList function. It could look
something like this:

void FreeList(STULST* head)
{
while(head){
STULST* tmp = head;
if(head->stu) free(head->stu);
head = head->next;
free(tmp);
}
}

regards:

good codes ^_^


thank you
may goodness be with you all
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,427
Latest member
HildredDic

Latest Threads

Top