[英]Memory Heap and Leak Summary on Valgrind
我們學校的提交都是通過矩陣使用valgrind,它逐行檢查output。 但是,提交時,我收到“檢測到內存錯誤”。
編譯結果:
Success! No errors or warnings...
執行:
Script started, file is student_output.txt
Script started, file is student_output.txt
==110143== Memcheck, a memory error detector
==110143== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==110143== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==110143== Command: ms
==110143==
Loading Data
Library Application:
輸入數據時:
Publication Title: e
==111103== Conditional jump or move depends on uninitialised value(s)
==111103== at 0x404318: sdds::LibApp::search(int, char) (LibApp.cpp:145)
==111103== by 0x404ABC: sdds::LibApp::removePublication() (LibApp.cpp:329)
==111103== by 0x404DC1: sdds::LibApp::run() (LibApp.cpp:415)
==111103== by 0x404F6F: runLibApp(char const*) (LibAppMain_prof.cpp:9)
==111103== by 0x405084: main (LibAppMain_prof.cpp:20)
==111103==
其他信息
我在添加出版物時也遇到錯誤(使用 newPublication() 函數),它在下面的同一文件中。
==140314== Use of uninitialised value of size 8
==140314== at 0x404926: sdds::LibApp::newPublication() (LibApp.cpp:282)
==140314== by 0x404DAA: sdds::LibApp::run() (LibApp.cpp:411)
==140314== by 0x404F6F: runLibApp(char const*) (LibAppMain_prof.cpp:9)
==140314== by 0x405084: main (LibAppMain_prof.cpp:20)
==140314==
==140314== Use of uninitialised value of size 8
==140314== at 0x40493F: sdds::LibApp::newPublication() (LibApp.cpp:284)
==140314== by 0x404DAA: sdds::LibApp::run() (LibApp.cpp:411)
==140314== by 0x404F6F: runLibApp(char const*) (LibAppMain_prof.cpp:9)
==140314== by 0x405084: main (LibAppMain_prof.cpp:20)
==140314==
該文件發布在下面。
LibApp.cpp
#define _CRT_SECURE_NO_WARNINGS
#include <fstream>
#include <iostream>
#include <fstream>
#include <cstring>
#include <iomanip>
#include "LibApp.h"
#include "Book.h"
#include "PublicationSelector.h"
namespace sdds {
int NOLP=0;
int LLRN=0;
bool LibApp::confirm(const char* message)
{
Menu conf(message);
conf<<"Yes";
int t_return = conf.run();
if(t_return) return true;
return false;
}
LibApp::LibApp(const char filename[256])
{
m_mainMenu << "Add New Publication"
<< "Remove Publication"
<< "Checkout publication from library"
<<"Return publication to library";
m_exitMenu << "Save changes and exit" << "Cancel and go back to the main menu";
strcpy(m_filename, filename);
m_publicationMenu << "Book" << "Publication" ;
load();
}
LibApp::~LibApp()
{
for (int i = 0; i< NOLP ; i++) {
delete PPA[i];
}
}
void LibApp::load(){
std::cout<<"Loading Data\n";
std::ifstream infile(m_filename);
char type{};
for (int i = 0; infile ; i++) {
infile >> type;
infile.ignore();
if (infile) {
if (type == 'P')
PPA[i] = new Publication;
else if (type == 'B')
PPA[i] = new Book;
else std::cout<<"no data\n";
if (PPA[i] && i < SDDS_LIBRARY_CAPACITY ) {
infile >> *PPA[i];
LLRN=PPA[i]->getRef();
NOLP++;
}
}
}
}
void LibApp::save(){
std::cout<<"Saving Data\n";
std::ofstream outfile(m_filename);
for (int i = 0; i < NOLP; i++) {
if (PPA[i]->getRef()!=0) {
outfile << *PPA[i] << std::endl;
}
}
}
void prnPub(Publication* p[], int size, int ref) {
int i;
for (i = 0; i < size; i++) {
if (ref == p[i]->getRef()) {
std::cout << *p[i] << std::endl;
i = size;
}
}
}
int LibApp::search(int option,char type){
PublicationSelector ps("Select one of the following found matches:", 15);
std::cout<<"Publication Title: ";
char title[256];
std::cin.getline(title,256);
if(option==1)
{
for (int i = 0; i< NOLP; i++) {
if (strstr(*PPA[i],title) && PPA[i]->getRef()!=0 && type==PPA[i]->type())
ps << PPA[i];
}
}
else if(option==2)
{
for (int i = 0; i< NOLP; i++) {
if (strstr(*PPA[i],title) && !PPA[i]->onLoan() && PPA[i]->getRef()!=0 && type==PPA[i]->type())
ps << PPA[i];
}
}
else if(option==3)
{
for (int i = 0; i< NOLP; i++) {
if (strstr(*PPA[i],title) && PPA[i]->onLoan() && PPA[i]->getRef()!=0 && type==PPA[i]->type())
ps << PPA[i];
}
}
int ref = 0;
if (ps) {
ps.sort();
ref = ps.run();
if (ref) {
prnPub(PPA, NOLP , ref);
}
else {
std::cout << "Aborted!\n";
}
}
else {
std::cout << "No matches to found!" << std::endl;
}
return ref;
}
void LibApp::returnPub()
{
std::cout<<"Return publication to the library\n";
int i=m_publicationMenu.run();
char type;
if(i==1) type='B';
else type='P';
int ref=search(3,type);
if(ref!=0 && confirm("Returning publication?"))
{
Date date=getPub(ref)->checkoutDate();
Date today;
int days=today-date;
days-=15;
if(days>0)
{
std::cout << std::fixed;
std::cout << std::setprecision(2);
std::cout<<"Please pay $"<<float(days)*(0.5)<<" penalty for being "<<days<<" days late!\n";
}
getPub(ref)->set(0);
std::cout<<"Publication returned\n";
m_changed=true;
}
}
void LibApp::newPublication()
{
if( NOLP >= SDDS_LIBRARY_CAPACITY )
{
std::cout<<"Library is at its maximum capacity!\n";
return;
}
std::cout<<"Adding new publication to library\n";
int i=m_publicationMenu.run();
Publication *p=nullptr;
if(i==0)
{ std::cout<<"Aborted!\n";
return;
}
else if(i==1)
{
p = new Book;
std::cin >> *p;
}
else if( i==2 )
{
p = new Publication;
std::cin >> *p;
}
if(std::cin.fail())
{
std::cout<<"\nAborted!\n";
exit(0);
}
if(confirm("Add this publication to library?"))
{
m_changed = true;
PPA[NOLP]=p;
LLRN=PPA[NOLP]->getRef();
NOLP++;
std::cout<<"Publication added\n";
}
if( !*p )
{
std::cout<<"Failed to add publication!\n";
delete p;
}
}
Publication* LibApp::getPub(int libRef)
{
for(int i=0;i<NOLP;i++)
{
if(libRef==PPA[i]->getRef()) return PPA[i];
}
return nullptr;
}
void LibApp::removePublication()
{
//std::cout<<;
std::cout<<"Removing publication from the library\n";
int i=m_publicationMenu.run();
char type;
if(i==1) type='B';
else
{
type='P';
}
int ref=search(1,type);
if(ref!=0 && confirm("Remove this publication from the library?"))
{
m_changed = true;
getPub(ref)->setRef(0);
std::cout<<"Publication removed\n";
}
}
void LibApp::checkOutPub()
{
std::cout<<"Checkout publication from the library\n";
int i=m_publicationMenu.run();
char type;
if(i==1) type='B';
else type='P';
int ref=search(2,type);
if(ref!=0 && confirm("Check out publication?"))
{
m_changed = true;
int mn;
std::cout << "Enter Membership Number: ";
while (1)
{
std::cin>>mn;
if(mn > 9999 && mn <= 99999 ) break;
std::cout<<"Invalid membership number, try again: ";
}
getPub(ref)->set(mn);
std::cout<<"Publication checked out\n";
}
}
LibApp::LibApp()
{
m_mainMenu << "Add New Publication"
<< "Remove Publication"
<< "Checkout publication from library"
<<"Return publication to library";
m_exitMenu << "Save changes and exit" << "Cancel and go back to the main menu";
load();
}
void LibApp::run()
{
while(1)
{
int option = m_mainMenu.run();
if( option == 1 )
{
newPublication();
}
else if( option == 2 )
{
removePublication();
}
else if( option == 3 )
{
checkOutPub();
}
else if( option == 4 )
{
returnPub();
}
else if( option == 0 )
{
if(m_changed)
{
int opn = m_exitMenu.run();
if( opn == 1 )
{
save();
break;
}
else if( opn == 2 )
{
;
}
else if( opn == 0)
{
if(confirm("This will discard all the changes are you sure?"))
break;
}
}
else break;
}
std::cout<<std::endl;
}
std::cout<<std::endl;
std::cout<<"-------------------------------------------\n";
std::cout<<"Thanks for using Seneca Library Application\n";
}
}
請幫我解決一下這個。 我今天提交的內容是我知道問題是什么。
就像錯誤消息說的那樣:
Conditional jump or move depends on uninitialised value(s)
==111103== at 0x404318: sdds::LibApp::search(int, char) (LibApp.cpp:145)
在 LibApp.cpp 的第 145 行有一個 if 語句(或邏輯上等同於 if 語句的東西),它基於它決定采用哪條路徑來處理從未初始化為任何值的變量; 因此,該 if-test 的行為是未定義的(即 go 是否可以取決於當時該變量的 memory 位置出現的任意數據)。
因此,您需要查看第 145 行的 if-test 中存在的變量,以確定哪些變量沒有事先設置,然后通過確保首先設置它們來修復錯誤。
如有必要,您可以添加臨時 if-tests 以激發 valgrind 為您提供更多結果:
if (some_suspect_var != 0) fprintf(stderr, “Yea\n”);
else fprintf(stderr, “Nay\n”);
…那么如果你在那個 if 行上得到一個 valgrind 錯誤,你就知道 some_suspect_var 在那一點上是未初始化的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.