读书人

一个栈的赋值有关问题

发布时间: 2012-03-06 20:47:55 作者: rapoo

一个栈的赋值问题!
构造一个栈,并使栈的第一个值为10,运行时就出错了,
弹出:"0x0040107e"指令引用的"0xcccccccc"内存。该内存不能为"written"。

应该是那条赋值语句出问题,我用VC6,XP的,怎么会这样呢?照着老严的数据结构的了。

#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef int SElemType;

typedef struct
{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;

Status InitStack(SqStack &S)
{
S.base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!S.base)
{
printf("OverFlow! Init error!\n");
exit(ERROR);
}
S.base=S.top;
S.stacksize=STACK_INIT_SIZE;
printf("aaa\n");
*S.top=10; //运行时弹出错误:"0x0040107e"指令引用的"0xcccccccc"内存。该内存不能为"written"。
printf("bbb\n");
S.top++;
return OK;
}

void main()
{
SqStack S;
InitStack(S);
}

[解决办法]
U have not initialized the pointer before you deference it
[解决办法]
S.top使用之前请初始化!!!

读书人网 >C++

热点推荐