c/c++中可以循环使用malloc给指针数组分配内存吗 求解答!!
dozhw 2010-03-18 09:13:58 这是我的程序,调试的时候每次到第二次执行malloc语句时就发生错误!!
本来想做http://acm.scs.bupt.cn/onlinejudge/showproblem.php?problem_id=1503这个题呢 可以前没用过程c/c++
真不知该怎么办了!!
#include<iostream>
//#include<malloc.h>
using namespace std;
/*bool isword(char c){
if(((int)'a')<=(int)c){
return true;
}else if(){
}else if()
}*/
char str[1200];
char *st[1200];
int i,cur=0;
char tem;
int main()
{
scanf("%s",str);
tem=str[0];
for(i=1;tem!='\0';i++){
if('a'<=tem<='z'||'A'<=tem<='Z')
{//deal with words
int j,SIZE=50;
st[cur]=(char*)malloc(SIZE*sizeof(char));
for(j=0;tem!='#';j++){
*(st[cur]+j)=tem;
tem=str[i++];
if(j>=SIZE){
SIZE+=50;
st[cur]=(char*)realloc(st[cur],(SIZE*sizeof(char)));
}
}
free(st[cur]);
cur++;
//printf("the %d times string words: %s",cur,st[cur]);
}
if(tem == '#'){//deal with #
st[cur]=(char*)malloc(2*sizeof(char));
*st[cur++]=tem;
free(st[cur]);
tem=str[i++];
//printf("the%d times string "#" : %s",cur,st[cur]);
}
if(tem!='#'&&('Z'<tem<'a'||tem<'A'||tem>'z')){//deal with non-words
int m,n;
char nw[1200];
for(m=0;tem!='#';m++)
{nw[m]=tem;
tem=str[i++];
}
st[cur++]=(char*)malloc((m+2)*sizeof(char));
for(n=0;n<=m;n++){
*(st[cur]+n)=nw[m-n];
}
//printf("the %d times string non-words: %s",cur,st[cur]);
}
}
int ocur=--cur;
for(;ocur>=0;ocur--){
printf("%s",st[ocur]);//output the string
}
return 0;
}