单链表 free问题

今天编程序时碰到的问题,就是输入 5 5,运行到free(p);时就终止递归,直接显示控制台了,请各位帮忙看看。谢谢
#if 1
#include <iostream>
#include <cstdio>
using namespace std;

typedef struct combination
{
int num;
struct combination *next;
}node;

node* push_in(node *head,int num)
{
node *p = NULL;

p = (node*)malloc(sizeof(node*));
if(p)
{
p->num = num;
if(head)
p->next = head;
else
p->next = NULL;
}
return p;
}

node* pop_back(node *head)
{
node *p = head;

head = head->next;
free(p); return head;
}

void print(node* head)
{
node* p = head;

cout << endl << "the list could be:";

{
cout << (p)->num << ' ';
//free(p++);
} while((p++)->next)
return;
}
void fillPaggage(int x, int score, node* p)
{
if(0 == score)
{
print(p);
return;
}

else if(((x)*(x+1)/2) < score)
{
return;
}

else{
for(int i = x; i >= 1; i--)
{
score -= i;
p = push_in(p,i);
fillPaggage(x-1,score,p);
p = pop_back(p);
score += i;
}
}
}
int main()
{
int n,m;
node *p = NULL;

cout << "pls input n and m:";
cin >> n >> m;
fillPaggage(n,m,p);
getchar();
getchar();
return 0;
}
#endif

作者: leoaran   发布时间: 2011-06-15

node* push_in(node *head,int num)
{
node *p = NULL;

p = (node*)malloc(sizeof(node*)); //p = (node *)malloc(sizeof(node));
if(p)

作者: woaifen3344   发布时间: 2011-06-15

p = (node *)malloc(sizeof(node));
申请的空间应为一个node所占空间,而不是指针所占空间

作者: woweiwokuang0000   发布时间: 2011-06-15

你的p又不是malloc分配的空间,使用free当然出错了,free要配合malloc使用

作者: bdmh   发布时间: 2011-06-15