【问题标题】:C++ VALGRIND Uninitialised value was created by a heap allocationC++ VALGRIND 未初始化的值是由堆分配创建的
【发布时间】:2013-06-05 13:34:07
【问题描述】:

我有问题。当我编译程序时,我没有任何错误,但是当我使用 valgrind 时: 未初始化的值是由堆分配创建的(与 new 一致) 条件跳转或移动取决于未初始化的值(带删除的行) 我在论坛中搜索,但没有找到太多可以帮助我的信息。 非常感谢您的提示。

我的程序

#include <cstdlib>
#include <stdint.h>
#include <cstdio>
#include <iostream>
#include <string>
#include <istream>
#include <cstring>
#include <fstream>
#include <sstream>
#include <cctype>



using namespace std;
using std::cout;
using std::endl;

int dlugosc,miejsce;
ifstream file;


class channel
{
public:
  int start;
  double length;
  int bytespix;
  int resolution;
  channel(double g) : start(g),
                        length(0),
                        bytespix(0),
                        resolution(0)
    {
    }  

};
int fileopen() // opens the file and returns its size
{
   file.open ("0_dlc.000", ios::in|ios::binary);

  if( file.good() == true )
    {
      cout << "Uzyskano dostep do pliku!" << endl;
    }
  else 
    {
      cout<< "File cannot open" <<endl;
    }

  file.seekg(0, file.end);
  dlugosc = file.tellg();

  return dlugosc;
}

int findword(const char* slowo,int startplace) 
{
  int m;
  int c=0;
  int cur=0;
  unsigned int equal=0;
  char element=0;

  file.seekg (startplace, file.beg);

  for(m=0;m<dlugosc;m++)
  {

  file.get(element); 

   if(element==slowo[cur])
   {
    equal++;
    cur++;
   }
   else
   {
     equal=0;
     cur=0;
     if(element==slowo[cur])
     {
       equal++;
       cur++;

     }
   }
   if(equal==strlen(slowo))
   {
     return m+startplace; 
   } 

  }
  return 0;  


}

int findvalue(const char* wartosc,int startpoint)
{
    int p;
    int g;
    char element=0;
    char* buffer = new char[9];

    miejsce = findword(wartosc,startpoint); // miejsce to global variable
    file.seekg (miejsce+1, file.beg);
    for(p=0;(int)element<58;p++)
    {
      file.get(element);
       if((int)element>58 || (int)element<48)
      break;
      else
      buffer[p] = element;  
    }  
    buffer[p]='\0';

   g = atoi(buffer);

    delete [] buffer;
    return g;    
}

int main()
{
    int a,h=0,channels,start=0,length=0,resolution=0,bytespix=0,m=0;
    const char* slowko="Data offset: ";

    dlugosc=fileopen();

    channel** kanaly=0;
    kanaly = new channel*[9];
    miejsce=0;

    for(a=0;a<9;a++)
    {
        kanaly[a] = new channel(4);
        start = findvalue("Data offset: ",miejsce+20);  
        kanaly[a]->start=start;  
    }

    for(m=0;m<9;m++)
    {
        delete kanaly[m];
    }
    delete []kanaly;

    file.close();
}

【问题讨论】:

  • @BЈовић 似乎已经解决了这个问题,但为了将来参考,学习构造一个minimal complete example 很重要。
  • @djf - 我做了,没有任何改变。

标签: c++ heap-memory valgrind


【解决方案1】:

问题出在channel 的构造函数中。初始化所有成员变量,问题就解决了:

class channel
{
public:
    double start;
    double length;
    int bytespix;
    int resolution;
    channel(double g) : start(g),
                        length(0),
                        bytespix(0),
                        resolution(0)
    {
    } 
};

【讨论】:

  • OP 发布的代码中是否存在依赖于这些未初始化值的行?还是您假设问题出在我们尚未显示的代码中,并且是由于channel 的ctor 中缺少初始化引起的? (不质疑这是解决方案。只是好奇,因为我在发布的代码中看不到任何问题。)
  • @simonc 您不需要使用这些值来获得该 valgrind 警告。不,问题出在显示的代码中。他真的没有初始化那些成员变量。
  • 好的,我按照你写的做了,不幸的是 valgrind 错误是一样的。
  • @BЈовић 我知道他没有初始化变量并同意初始化它们是正确的。我怀疑这是错误的原因。根据我的经验,您确实必须使用变量 - 例如if (channel.length == 0) - 得到那个警告。 Valgrind 不会(不能)警告分配的内存,从不初始化和从不取消引用。
  • @user2455862 很可能来自您未显示的代码。你能举出 vagrind 错误的完整例子吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-11
  • 1970-01-01
  • 2021-06-14
相关资源
最近更新 更多