内存空间为啥没分配

C++语言 码拜 7年前 (2017-04-19) 912次浏览
#include”iostream”
#include”string.h”
using namespace std;
const int MAXSIZE=100;
typedef struct{
char *top;
char *base;
int stacksize;
}Sqstack;
int init(Sqstack &s){
s.base=new char[MAXSIZE];
if(!s.base) exit(-1);
s.top=s.base;
s.stacksize=MAXSIZE;
return 0;
}
int push(Sqstack &s,char e){
if(s.top-s.base==s.stacksize)
return -1;
*s.top++=e;
return 0;
}
int pop(Sqstack &s,char &e)
{
if(s.top==s.base)return 0;
e=*–s.top;
return 0;
}
void main(){
char a[10],m;
int l,i,tag=1;
Sqstack s;
gets(a);
l=strlen(a);
for(i=0;i<10;i++)
cout<<a[i];
for(i=0;i<=(l/2);i++)
push(s,a[i]);
if(l%2==0)
{
for(i=((l/2)+1);i<l&&tag;i++)
{pop(s,m);
if(m!=a[i])
tag=0;
}}
if(l%2!=0)
{
for(i=(l/2+2);i<l&&tag;i++)
{pop(s,m);
if(m!=a[i])
tag=0;
}}
if(tag==1)
cout<<“该数是回文数”<<endl;
else
cout<<“错误”;
}
解决方案

10

int init(Sqstack &s)这个函数都没调用,哪来的内存?

40

1.main函数需要调用init(s);
2.你main函数里cout为什么是循环10次?应该是循环l次吧
3.你的程序还有别的地方也不对,本人再改改

20

你都没有调用init当然没有分配内存了

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明内存空间为啥没分配
喜欢 (0)
[1034331897@qq.com]
分享 (0)