Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

freeing allocated memory...

Status
Not open for further replies.

netcomander

Programmer
Jul 20, 2001
68
0
0
DE
hi all,

I've got a problem with a programm, which deals with linked lists.
The main task of the programm is reading datas out of several database-tables.
Because I do not know how many fields will be found in a table, I store the datas in
a linked list with one element per field. After I'm done with the tables data I free the
allocated memory for the list in a loop like
while(listelement->next){
bffr = listelement;
listelement = listelement->next;
free((void *) bffr);
}...
then I go over to the next table, and I do again build my linked list.

Ok, now to the problem. The system (linux redhat 7.0) is not going to use the once
allocated memory twice. It seems to refuse to pick up the former allocated memory
location, which was given free by me by "free((void *) bffr);". Instead it is allocating
new memory, as if the freed memory was still occupied.
Yes, and this leads to the situation that my server takes up to 500MB Memory and
starts swapping.

I was glad by getting an advice that leads me to the light ;)

greetings markus
 
If this is the case then there shud b some bug in the free syscall coz u'll b able to use the same memory again if u r freeing it. Now the solution is that allocate a big memory chunk and handle that as per ur requirements.Means write a small memory manager for ur application.
 
Hi,
Well maybe listelement->next is NULL to start because you forgot to initialize it to the HEAD of the list before you started the While loop?

Therefore the While loop is never executed?

Put a print statement inside the loop and make sure it is being executed.

 
When it come to UNIX and I would imagine LINUX (since it was built from UNIX), memory management and allocation can be tricky. Since UNIX constantly spawns processes and those processes spawn processes and so on... an issue arrises where, depending on the system usage, UNIX starts paging and swapping. Memory that was once freely available can become allocated for unrelated processes in a matter of nanoseconds.

Assuming that your system has enough Physical Memory (RAM) and that your system administrator had tuned the kernel correctly. I think that your difficulty is a result of system processes or daemons re-allocating the memory your program was accessing. My advice would be to run a memory monitoring command like vmstat and execute your program all the while monitoring the output of the vmstat command. That way you can monitor memory usage as well as paging, swapping, and faults. Hope this helps.
aithosn8
aithosn8@lycos.com
 
Hi,
your code does not free the memory of the last node.
This happens bcoz you are checking the next of a node for the NULL value. When the control reaches on the last node, "while loop" condition will fail and it will not enter the loop.
Try the following code:
listelement = start
while(listelement){
bffr = listelement;
listelement = listelement->next;
free((void *) bffr);
}

VinKish
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top