首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

简单的程序出错,该如何处理

2012-02-07 
简单的程序出错#include iostreamusingnamespacestdvoidf(){int*pinewint(10)int*pianewint[10]whi

简单的程序出错
#include <iostream>
using   namespace   std   ;
 
void   f()
{
int   *pi   =   new   int   (10);
int   *pia   =   new   int   [10];

while(*pi <20)
{
*pia=   *pi;
*pi   =   *pi+1;


                cout < <   *pia++   < <   "     "   ;
}
delete   pi;
delete   []   pia;
cout < <   endl;
}
int   main()
{
f();
return   0;
}
+++++++++
run   error!
--------
debug   assertion   failed!
expression:ctrisvalidpointer(pUserdate)

for   information   on   how   your   program   can   assertion
failure,see   the   visual   c++   documentation   on   asserts.
(press   retry   to   debug   the   application)
问题出在那里?

[解决办法]
主函数不不能有返回值吧
不知道你想做什么
[解决办法]
指针有问题;
cout < < *pia++ < < " " ;
应该是cout < < *pia < < " ";
你pia的值通过指针pi赋值过来的,pi++就可以了,pia为什么要还要自加呢,系统无法判断自加后的pia值向的内存是多少,发生了上面的断言失败。
[解决办法]

你这样 cout < < (*pia)++ < < " " ;

*的优先级没有++高 呵呵 pia让他每次输出后就跑到下一个了
这样你delete [] 的地址就 就不是你 new的地址了
而是你new的地址加上 4*10 当然就会出现段错误
[解决办法]
楼上有理!
#include <iostream>
using namespace std ;

void f()
{
int *pi = new int (10);
int *pia = new int [10];

while(*pi <20)
{
*pia= *pi;
*pi = *pi+1;


cout < < *pia < < " " ;
}
delete pi;
delete [] pia;
cout < < endl;
}
int main()
{
f();
return 0;
}

热点排行