I am trying to allocate memory to linking list dynamically.
When I call the free function, the memory is not actually freed.
The consumed memory profile are as follows:
Before free
root 9031 2.6
5.2 99432 40592 pts/2 S+ 12:16 0:00 ./a.out
After free
root 9031 2.6
5.2 99432 40592 pts/2 S+ 12:16 0:00 ./a.out
However, if I exeucted the above codes on Windows 2000, the memory will be actually freed.
Would you guys have any comment?
my source code is as follows:
Code:
//----------------------------------
#include <stdio.h>
#include <stdlib.h>
typedef struct _node
{
int i;
char *parray;
struct _node *next;
}Node;
Node* newNode(int i);
void freeNode(Node *n);
void freeNodeLoop(Node *n);
int main()
{
int j;
// allocate 10000 nodes
Node *head = newNode(10000);
while(1)
{
int i;
printf("push 1 to free memory, 2 to exit\n");
scanf("%d", &i);
if( i==1 )
{
printf("free node\n");
freeNode(head);
head = NULL;
}
else if ( i == 2)
break;
}
return 0;
}
Node* newNode(int i)
{
Node *head;
Node *pre;
Node *cur;
int k;
head = NULL;
pre = NULL;
for(k=0; k<i; k++)
{
cur = (Node*)malloc(sizeof(Node));
cur->parray = (char*)malloc(sizeof(char)*10000);
cur->i = k;
cur->next = NULL;
if( head == NULL )
{
head = cur;
}
else
{
pre->next = cur;
}
pre = cur;
}
return head;
}
void freeNode(Node *n)
{
if( n == NULL)
{
printf("Node is NULL\n");
return;
}
if(n->next != NULL)
freeNode(n->next);
free(n->parray);
free(n);
}
//---------------------------------