在C ++中的valgrind,prgram运行良好,但我仍然收到syscallParam警告,不确定为什么

时间:2019-12-01 11:40:31

标签: c++ valgrind

我分配了根据LRU算法实现CacheManager的任务 这是我的代码:

#ifndef EX2_CMAKE_BUILD_DEBUG_D_H_
#define EX2_CMAKE_BUILD_DEBUG_D_H_
#include <fstream>
#include <map>
#include <iterator>
#include <string>
#include <iostream>
#include <list>
#include <utility>
#include <fstream>
#include <sstream>
using namespace std;

class double_linked {
  struct node {
    string data;
    node* prev;
    node* next;
    node(string t, node* p, node* n) : data(t), prev(p), next(n) {}
  };
  node* head;
  node* tail;

 public:
  double_linked() : head(NULL), tail(NULL) {}
  void push_front(string T) {
    node *new_node = new node(T, nullptr, nullptr);
    if (head != nullptr) {
      new_node->next = head;
      head -> prev = new_node;
      head = new_node;
    } else {
      new_node->next = nullptr;
      head = new_node;
      tail = head;
    }
  }
  string pop_back() {
    string data = tail->data;
    node *temp = tail;
    tail = tail->prev;
    if (tail != head) {
      tail->next = nullptr;
    }
    delete temp;
    return data;
  }

  int get_number_of_nodes() {
    int i = 0;
    node* temp(head);
    while (temp != nullptr) {
      i++;
      temp = temp->next;
    }
    return i;
  }

  string get_node(int i) {
    node* temp(head);
    while (i > 0) {
      temp = temp->next;
      i--;
    }
    return temp->data;
  }

  void pop(node* temp) {
    node* temp1(temp);
    if (temp != head) {
      temp1 = temp1->prev;
      temp1->next = temp->next;
      temp->next->prev = temp1;
      delete temp;
    }
  }

  void move_node_to_front(string key) {
    node* temp(head);
    int trigger_found_node = 0;
    while (temp != nullptr && trigger_found_node < 1) {
      if (temp->data.compare(key) == 0) {
        trigger_found_node++;
      } else {
        temp = temp->next;
      }
    }
    if (temp != head) {
      if (temp != tail) {
        pop(temp);
      } else {
        pop_back();
      }
      push_front(key);
    }
  }

  virtual ~double_linked() {
    while (head) {
      node* temp(head);
      head = head->next;
      delete temp;
    }
  }
};

template<class T>
class CacheManager {
 private :
  map<string, T> chachedmap;
  int size;
  int cachedCounter;
  double_linked LRUlist;
 public :
  CacheManager(const int& capacity) : size(capacity) , cachedCounter(0) {}
  /*handling object insert\update inside cache*/
  void insert(string key, T obj) {
    if (chachedmap.find(key) == chachedmap.end()) {
      this->cachedCounter = this->cachedCounter + 1;
      chachedmap.insert(std::make_pair(key, obj));
      LRUlist.push_front(key);
      if (this->cachedCounter >
          this->size) {
        typename std::map<string, T>::iterator it;
        string temp = LRUlist.pop_back();
        it = chachedmap.find(temp);
        chachedmap.erase(it);
        this->cachedCounter = this->cachedCounter - 1;
      }
      //  handling object insert\update inside file
      try {
        ofstream myfile{typeid(T).name() + key + ".txt", ios::binary};
        string c = typeid(T).name() + key + ".txt";
        if (myfile.is_open()) {
          myfile.write((char *)&obj, sizeof(obj));
        }
        myfile.close();
      } catch (const char *e) {
        throw "error oppening file";
      }
    } else {
      chachedmap.at(key) = obj;
      LRUlist.move_node_to_front(key);
      try {
        ofstream myfile{typeid(T).name() + key + ".txt", ios::binary};
        string c = typeid(T).name() + key + ".txt";
        if (myfile.is_open()) {
          myfile.write((char *)&obj, sizeof(obj));
        }
        myfile.close();
      } catch (const char *e) {
        throw "error oppening file";
      }
    }
  }
  T get(string key) {
    string line;
    T obj5;
    if (chachedmap.find(key) == chachedmap.end()) {
      try {
        string key_num_to_string;
        ifstream myfile1 {typeid(T).name() + key + ".txt", ios::binary};
        string c = typeid(T).name() + key + ".txt";
        if (!myfile1) {
          throw "an error";
        }
        if (!myfile1.is_open()) {
          throw "cant open file";
        }
        myfile1.read((char *)&obj5, sizeof(obj5));
        myfile1.close();
        this->insert(key, obj5);
      } catch (const char *e) {
        cout << e << endl;
      }
      return obj5;
    } else {
      LRUlist.move_node_to_front(key);
      return chachedmap.at(key);
    }
  }
  void foreach(void (*p_function)(T &)) {
    int j = LRUlist.get_number_of_nodes();
    int i = 0;
    while (i < j) {
      string key = LRUlist.get_node(i);
      auto kv = chachedmap.at(key);
      p_function(kv);
      i++;
    }
  }
  virtual ~CacheManager(){}
};

#endif

现在这是我的主菜单:

#include "ex2.h"
#include <iostream>
#include <string>

using namespace std;
class Teacher
{
  const char* name;
  const char* course;
  int year;
 public:
  static const string class_name; //every class will have a class static name called: class_name
  Teacher(){}; //every class will have a default constructor
  Teacher(const char* _name, const char* _course, int _year): name(_name),
                                                              course(_course), year(_year){};
  void print() {
    cout<<"Teacher Object: "<<name<<", "<<course<<", "<<year<<endl;
  };
  ~Teacher(){};
};

const string Teacher::class_name = "TeacherClass";


int main() {

  CacheManager<Teacher> teacherCache(5);

  try {
    teacherCache.insert("0", Teacher("Reznikov", "LinearAlgebra", 1));
    teacherCache.insert("1", Teacher("Doron", "Infi", 1));
    teacherCache.insert("2", Teacher("Erez", "Algebraic structures", 2));
    teacherCache.insert("3", Teacher("Arye", "Discrete mathematics", 1));
    teacherCache.insert("4", Teacher("Noa", "Introduction to CS", 1));
    teacherCache.insert("5", Teacher("Adi", "LinearAlgebra", 1));
    teacherCache.insert("6", Teacher("Tsvi Kopelowitz", "Algorithms1", 2));
  } catch  (const char * e) {
    cout << e << endl;
  }
  return 0;
}

现在执行很好,但是我在valgrind中收到系统调用警告,有人可以向我解释一下问题是什么。我想教师类成员的存储方式存在填充问题

valgrind: 
SyscallParam
write.c
Syscall param write(buf) points to uninitialised byte(s)
write
/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25
std::basic_filebuf<char, std::char_traits>::_M_convert_to_external(char*, long)
std::basic_filebuf<char, std::char_traits>::overflow(int)
std::basic_filebuf<char, std::char_traits>::_M_terminate_output()
std::basic_filebuf<char, std::char_traits>::close()
std::basic_ofstream<char, std::char_traits>::close()
CacheManager<Teacher>::insert(std::__cxx11::basic_string<char, std::char_traits, std::allocator>, Teacher)
main
Address 0x5b7e014 is 20 bytes inside a block of size 8,192 alloc'd
operator new[](unsigned long)
std::basic_filebuf<char, std::char_traits>::_M_allocate_internal_buffer()
std::basic_filebuf<char, std::char_traits>::open(char const*, std::_Ios_Openmode)
std::basic_ofstream<char, std::char_traits>::basic_ofstream(std::__cxx11::basic_string<char, std::char_traits, std::allocator> const&, std::_Ios_Openmode)
CacheManager<Teacher>::insert(std::__cxx11::basic_string<char, std::char_traits, std::allocator>, Teacher)
main
Uninitialised value was created by a stack allocation
main

0 个答案:

没有答案