代码如下:
void main(){
int i=0;
int intStepNo=0;
char *chrInput=new char(sizeof(char)*1024);for(i=0;i<1024;i++)
{
  *(chrInput+i)=0;
}
cout<<121<<endl; //此处不通过
}每次程序运行到cout处即出现内存非法访问,cout无法输出除了char和string以外的类型数值,
希望各位高人赐教~。谢谢

解决方案 »

  1.   

    char *chrInput=new char(sizeof(char)*1024);for(i=0;i<1024;i++)
    {
      *(chrInput+i)=0;
    }
    不关cout的事,
    char *chrInput=new char(sizeof(char)*1024);
    这里很大问题哦
    你是没有开数组空间 而是new 了一char 然后初始化他我sizeof(char)*1024
    而下面
    for(i=0;i<1024;i++)
    {
      *(chrInput+i)=0;
    }
    在未开辟空间的地方负值,所以出错
    改为
    char *chrInput=new char[sizeof(char)*1024];
      

  2.   

    输不出结果是因为
    cout<<121<<endl; //没有这个用法
      

  3.   

    这么用:
    std::cout<<to_string<int>(121, std::hex)<<std::endl;
      

  4.   

    写错了,上面是输出16进制度的,
    用这个
    std::cout<<to_string<long>(121, std::dec)<<std::endl;
      

  5.   

    HunterForPig(留着口水的猪)----正解!
      

  6.   

    cout能直接输出数字的,现在都忘了这个了。
    Sorry....:(
      

  7.   

    new int(100)与new int[100]是有区别的
    前者开辟空间为一个int,初始化为100
    后者开辟连续的大小为100个int的空间
      

  8.   

    char *chrInput=new char[sizeof(char)*1024];

    char *chrInput=new char(sizeof(char)*1024);区别何在?
      

  9.   

    char *chrInput=new char(sizeof(char)*1024);与char *chrInput=new char[sizeof(char)*1024];有何区别?