| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 623 人关注过本帖
标题:内存不能申请问题
只看楼主 加入收藏
wswwang
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2007-1-5
收藏
 问题点数:0 回复次数:2 
内存不能申请问题

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct listnode *position;

struct listnode
{
char a[10] ;
int group;
position next;
} ;

typedef position list;
struct hashtbl
{
int tablesize;
list *thelists;
};
typedef struct hashtbl *hashtable;
hashtable InitializeTable(int tablesize)
{
hashtable H;
int i;
H=malloc(sizeof(struct hashtbl));
if(H==NULL)
{
printf("out of space1!!") ;

}


H->tablesize=tablesize;
H->thelists=malloc(sizeof(list)*H->tablesize);
if(H->thelists==NULL)
{ printf("out of space2!!");

}

for(i=0;i<H->tablesize;i++)
{
H->thelists[i]=malloc(sizeof(struct listnode));
if(H->thelists[i]==NULL)
{
printf("out of space3!!");

}
else
H->thelists[i]->next=NULL;
}


return H;
}
void insert(char *s,hashtable H,int g)
{
position p, newcell;
list l;
??? newcell=malloc(sizeof(struct listnode));
if(newcell==NULL)
{ printf("out of space4!!");
}?????

else
{
l=H->thelists[s[0]-'A'];
p=l->next;
while(p!=NULL)
p=p->next;
p->next=newcell;
newcell->next=NULL;
strcpy(newcell->a,s);
newcell->group=g;
}
}
int find(char *s,hashtable H)
{
position p;
list l;
l=H->thelists[s[0]-'A'];
p=l->next;
while(p!=NULL&&strcmp(p->a,s)!=0)
p=p->next;
return p->group;
}

void ENQUEUE(char *s,int group,list l)
{ position p,temp,newcell;
p=l->next;
temp=l;
while(p!=NULL&&p->group>group)
{
temp=p;
p=p->next;
}
newcell=malloc(sizeof(struct listnode));
if(newcell==NULL)printf("out of space!!");
else
{
strcmp(newcell->a,s);
newcell->group=group;
temp->next=newcell;
newcell->next=p;
}
}

void DEQUEUE(list l)
{ position temp;
temp=l;
l=l->next;

free(temp);
}


main()
{
hashtable h;
list queue;
FILE *input,*output;
char s[10];
int group;

int k,a[1000];
int i,j;
if((input=fopen("input.txt","r"))==NULL)
{ printf("can't open the file1\n");
exit(0);
}
if((output=fopen("output.txt","w"))==NULL)
{ printf("can't open the file2\n");
exit(0);
}
while(1)
{ queue=malloc(sizeof(struct listnode));
queue->next=NULL;
fscanf(input,"%d",&k);
if(k==0)break;
h=InitializeTable(26);

for(i=0;i<k;i++)
{
fscanf(input,"%d",&a[i]);

for(j=0;j<a[i];j++)
{
fscanf(input,"%s",s);
/*insert(s,h,i); */

fprintf(output,"%s\n",s);
}
fprintf(output,"\n");
}
while(fscanf(input,"%s",s))
{
if(strcmp("stop",s)==0)break;
else if(strcmp("ENQUEUE",s)==0)
{
fscanf(input,"%s",s);
group=find(s,h);
ENQUEUE(s,group,queue);
}
else if(strcmp("DEQUEUE",s)==0)
{ fprintf(output,"%s\n",s);
DEQUEUE(queue);
}

}

}
fclose(input);
fclose(output);
}
打问号的地方,不能申请内存,部知道什么原因,请个位帮帮忙。
我用的是c-free编译的

搜索更多相关主题的帖子: 内存 struct int position typedef 
2007-10-11 22:54
永夜的极光
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:2721
专家分:1
注 册:2007-10-9
收藏
得分:0 
newcell=(position)malloc(sizeof(struct listnode));试试看

从BFS(Breadth First Study)到DFS(Depth First Study)
2007-10-12 08:36
cobby
Rank: 1
等 级:新手上路
威 望:1
帖 子:565
专家分:0
注 册:2007-7-11
收藏
得分:0 
你的position不是指针,已经有了内存空间,所以不能分配。定义时把position定义成*position就OK了

努力成为菜鸟!
2007-10-12 10:23
快速回复:内存不能申请问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011522 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved