C++ VALGRIND 未初始化的值是由堆分配创建的

标签 c++ heap-memory valgrind

我有一个问题。当我编译程序时没有任何错误,但是当我使用 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();
}

最佳答案

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

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

关于C++ VALGRIND 未初始化的值是由堆分配创建的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16941368/

相关文章:

c - 错误信息 : *** glibc detected *** ./cube : double free or corruption (! prev): 0x0a4c4420 ***

c++ - C++初始化后的数组赋值

c - Valgrind 对结构 + union 的行为

c++ - 如何为此代码创建支票?

java.lang.OutOfMemory错误: Java heap space in allocating array size

c++ - glibc malloc() 总字节数

c++ - 从函数返回 vector 而不破坏它

c++ - Valgrind C++ 内存泄漏

c++ - 如何从我的 Visual Studio 静态代码分析中排除库 header ?

c++ - 无符号字符串