Hello All:
I have a linked list of structs each of which contains an integer and a pointer to the next struct. Before populating this struct by a sequence of new commands, I note down the memory used (say Mem_1) by this program in Windows Task Manager under "Mem Usage". The actual linked list creation occurs next. (See void populate(int i) function below). Then, I use a sequence of deletes to try and delete this linked list and hopefully reclaim the memory. After the deletes, I check the memory again in the Task Manager. This time the amount of memory used is, say, Mem_2. I notice that Mem_2 > Mem_1. Should not Mem_2 = Mem_1? Or is there some dangling pointer I am not properly taking care of.
Thanks for your help in advance...(The code is a console application/VS2008/Windows XP platform)
struct item_s{
int value;
item_s* next;
};
struct item_s* item = NULL;
struct item_s* last_item = NULL;
struct item_s* last_accessed = NULL;
void populate(int i){
if(item == NULL){
item = new item_s;
item->value = i;
item->next = NULL;
last_item = item;
}
else{
last_item->next = new item_s;
last_item->next->value = i;
last_item->next->next = NULL;
last_item = last_item->next;
}
}
void main(){
for(i = 1; i <= 10000; i++){
populate(i);
}
last_item = item;
last_accessed = last_item->next;
while(last_item!=NULL){
delete last_item;
last_item = last_accessed;
if(last_item!=NULL){
last_accessed = last_item->next;
}
}
}