鱼C论坛

 找回密码
 立即注册
查看: 3745|回复: 4

缓冲区溢出

[复制链接]
发表于 2014-12-2 19:13:57 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
下面是的代码目的是从一个TEXT文件中读取未知大小的矩阵,并将矩阵存放在一个动态分配的空间中。我在用VS2013调试的时候,在红色代码处出现了缓冲区溢出,谁能帮我解释一下为什么码?

  1. // 从文件读入矩阵
  2. Matrix Matrix::MatValueFileIn(const char *infilename="matrixin.txt")
  3. {
  4.     ifstream inFile;
  5.     inFile.open(infilename,ios::in);
  6.     int row=0;      //矩阵行
  7.     int col=0;        //矩阵列
  8.     string temp;
  9.     double datatemp=0.0;
  10.     vector<double> veMatData;                                           //用来保存输入的临时数据
  11.     if(inFile.bad())
  12.     {
  13.        cout<<"文件不存在"<<endl;
  14.        exit(1);

  15.     }
  16.     if(inFile.is_open())
  17.     {
  18.         cout<<"文件打开成功"<<endl;
  19.         while(getline(inFile,temp,'\n'))
  20.         {
  21.             ++row;                                  //获得矩阵行数
  22.         }
  23.         temp.clear();
  24.         inFile.clear();
  25.         inFile.seekg(0);                         //重新定位到文件头

  26.         {
  27.             double m;                            //获得矩阵列
  28.             char c;
  29.             while(inFile>>m)
  30.             {
  31.                  ++col;
  32.                 c=inFile.peek();
  33.                 if(c=='\n')
  34.                 {
  35.                     break;
  36.                 }
  37.             }
  38.         }
  39.         inFile.clear();
  40.         inFile.seekg(0);
  41.         this->matRow=row;
  42.         this->matCol=col;
  43.         this->pMatValue=new double[row*col];
  44.                
  45.         while(inFile.good() && !inFile.eof())
  46.         {
  47.                        
  48.                          inFile>>datatemp;               
  49.                          veMatData.push_back(datatemp);                        //将矩阵数据读入到vector中               
  50.         }
  51.         if(!veMatData.empty())
  52.         {
  53.             for(size_t  i=0; i <(veMatData.size())-1;i++)
  54.             {
  55.                 [color=Red]this->pMatValue[i]=veMatData[i];[/color]
  56.             }
  57.         }
  58.         inFile.clear();
  59.         inFile.close();

  60.     }
  61.     else
  62.     {
  63.         cerr<<"文件打开失败"<<endl;
  64.         exit(0);
  65.     }
  66.     return *this;

  67. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2014-12-2 19:15:29 | 显示全部楼层
额,颜色没有改。
this->pMatValue[i]=veMatData[i]; 是57行这句
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-12-2 19:18:01 | 显示全部楼层
GRRRARD 发表于 2014-12-2 19:15
额,颜色没有改。
this->pMatValue=veMatData; 是57行这句

warning C6386: 写入到“this->pMatValue”时缓冲区溢出:    可写大小为“row*col*8”个字节,但可能写入了“16”个字节。
不太明白这句话里面 16个字节是什么意思,怎么会出现个16呢?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-12-14 17:23:30 | 显示全部楼层
GRRRARD 发表于 2014-12-2 19:18
warning C6386: 写入到“this->pMatValue”时缓冲区溢出:    可写大小为“row*col*8”个字节,但可能写入 ...

this->pMatValue 是个啥?贴就贴全了啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-12-15 14:17:30 | 显示全部楼层
编程难 发表于 2014-12-14 17:23
this->pMatValue 是个啥?贴就贴全了啊

是这句  this->pMatValue=veMatData  //pMatValue是Matrix 类中的成员,是个指针,我用这个来存放矩阵元素。veMatData是个 vector,里面存放的是我从文件里读出的矩阵元素,我想把它赋值给pMatValue。因为提前不知道矩阵元素的个数,所以pMatValue是动态分配的,大小是row*col*8,row和col分别是矩阵的行列。
全部的代码有点长,而且和这没有关系所以我就没贴。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-18 12:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表