作者neville123 (nk)
看板C_and_CPP
标题[问题] 记忆体错误
时间Mon Apr 6 20:08:22 2009
这是小弟一份关於stack的code
ptr 是一个型别为struct item 的 array
在第一次执行push 的时候 确定可以顺利将element塞进array里面
只是在第二次执行的时候却发现记忆体读取错误 (会直接终止)
不知道是不是push里面出了点问题
因为小弟对pointer也还不是说非常熟悉 所以也不确定问题到底出在哪
先谢谢大家了~
void push (struct item **ptr, struct item good){
top = top + 1 ;
printf ("top = %d\n",top);
*(ptr[top]) = good ;
}
struct item{
int time;
int index;
int From_stair;
int To_stair;
int weight;
};//item
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 123.204.215.109
1F:→ windincloud:你是不是应该要做copy construct的动作 而不是直接指 04/06 20:32
2F:→ neville123:不好意思 还是有点不太懂@@ 04/06 20:40
3F:推 xrancyma:你是如何CALL push 的? 04/06 21:49
4F:→ neville123:push (&ptr, queue[i]); 04/06 21:51
5F:推 xrancyma:只要把 *(ptr[top])=good; 改成 *(ptr[top])=good; 就OK 04/06 21:56
6F:→ xrancyma:打错,改成 **(ptr[top]) = good; 应该就可以了 04/06 21:57
7F:→ neville123:可是没办法complier 会发生语法错误@@ 04/06 22:02
8F:→ xrancyma:抱歉,是改成 (*ptr)[top] = item 才对。 04/06 22:09