- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
该代码将编译并运行,并创建预期的输出,除非运行valgrind时才会出现这些内存泄漏。以下代码在Visual Studio上运行,不会出现任何警告或错误。
所以我的问题是,此内存泄漏发生在哪里?我是CPP的新手,已经花了数小时的时间,所以这些错误让我感到惊讶。
就顺序而言,我有做错什么吗?我在某个地方传递未初始化的值吗?困惑。
我在弄清楚发生内存丢失的位置时遇到了麻烦。这些是文件:
/// Saiyan.cpp
#define _CRT_SECURE_NO_WARNINGS
#include <string.h>
#include <iostream>
#include "Saiyan.h"
using namespace std;
namespace sdds
{
// CONSTRUCTORS:
Saiyan::Saiyan()
{
// default state
m_name = nullptr; // Dynamic allocation: set to nullptr!
m_dob = 0;
m_power = 0;
m_super = false;
m_level = 0;
}
Saiyan::Saiyan(const char* name, int dob, int power)
{
set(name, dob, power);
}
// MEMBER FUNCTIONS:
void Saiyan::set(const char* name, int dob, int power, int level, bool super)
{
// Check if arguments are valid:
if (name == nullptr || strlen(name) <= 0 || dob > 2020 || power <= 0)
{
*this = Saiyan(); // Calls constructor that creates default.
}
else
{
// Deallocate previosly allocated memory for m_name to avoid memory leak:
if (m_name != nullptr && strlen(name) == 0)
{
delete[] m_name;
m_name = nullptr;
}
// Assign validate values to current object:
m_name = new char[strlen(name) + 1];
strcpy(m_name, name);
m_dob = dob;
m_power = power;
m_super = super;
m_level = level;
}
}
bool Saiyan::isValid() const
{
bool valid_state = m_name != nullptr && strlen(m_name) > 0 && m_dob < 2020 && m_power > 0;
return valid_state;
}
void Saiyan::display() const
{
if (isValid())
{
cout << m_name << endl;
cout.setf(ios::right);
cout.width(10);
cout << "DOB: " << m_dob << endl;
cout.width(10);
cout << "Power: " << m_power << endl;
cout.width(10);
if (m_super == true) {
cout << "Super: " << "yes" << endl;
cout.width(10);
cout << "Level: " << m_level;
}
else
{
cout << "Super: " << "no";
}
cout.unsetf(ios::left);
}
else
{
cout << "Invalid Saiyan!";
}
cout << endl;
}
bool Saiyan::fight(Saiyan& other)
{
// Check both Saiyans for super level and power up accordingly:
if (m_super == true)
{
m_power += int(m_power * (.1 * m_level)); // Cast an int to avoid possible memory loss.
}
if (other.m_super == true)
{
other.m_power += int(other.m_power * (.1 * other.m_level));
}
bool value = m_power > other.m_power;
return value;
}
// DESTRUCTOR:
Saiyan::~Saiyan()
{
if (m_name != nullptr)
{
delete[] m_name; // Deallocate memory of member.
m_name = nullptr;
}
}
}
// Saiyan.h
#pragma once
#ifndef SDDS_SAIYAN_H
#define SDDS_SAIYAN_H
namespace sdds
{
class Saiyan
{
char* m_name; // Dynamically allocated array of chars.
int m_dob; // Year the Saiyan was born.
int m_power; // Integer indicating the strength of the Saiyan (>= 0).
bool m_super; // indicates whether Saiyan can evolve
int m_level; // an integer indicating the level of a SS
/*
***Valid Name*** : a dynamically allocated array of chars.
***Valid Year of Birth***: an integer within the interval[0, 2020].
***Valid Power***: an integer that is greater than 0.
*/
public:
Saiyan();
Saiyan(const char* name, int dob, int power); // Custom constructor
void set(const char* name, int dob, int power, int level = 0, bool super = false);
bool isValid() const;
void display() const;
bool fight(Saiyan& other); // Fight and power up Saiyans.
~Saiyan();
};
}
#endif
// main.cpp
#include <iostream>
#include "Saiyan.h"
#include "Saiyan.h" // this is on purpose
using namespace std;
using namespace sdds;
void printHeader(const char* title)
{
char oldFill = cout.fill('-');
cout.width(40);
cout << "" << endl;
cout << "|> " << title << endl;
cout.fill('-');
cout.width(40);
cout << "" << endl;
cout.fill(oldFill);
}
int main()
{
{
printHeader("T1: Checking default constructor");
Saiyan theSayan;
theSayan.display();
cout << endl;
}
{
printHeader("T2: Checking custom constructor");
Saiyan army[] = {
Saiyan("Nappa", 2025, 1),
Saiyan("Vegeta", 2018, -1),
Saiyan("Goku", 1990, 200),
Saiyan(nullptr, 2015, 1),
Saiyan("", 2018, 5)
};
cout << "Only #2 should be valid:" << endl;
for (int i = 0; i < 5; i++)
{
cout << " Sayan #" << i << ": " << (army[i].isValid() ? "valid" : "invalid") << endl;
}
for (int i = 0; i < 5; i++)
{
army[i].display();
}
cout << endl;
}
// valid saiyans
Saiyan s1("Goku", 1990, 2000);
Saiyan s2;
s2.set("Vegeta", 1989, 2200);
{
printHeader("T3: Checking the fight");
s1.display();
s2.display();
cout << "S1 attacking S2, Battle " << (s1.fight(s2) ? "Won" : "Lost") << endl;
cout << "S2 attacking S1, Battle " << (s2.fight(s1) ? "Won" : "Lost") << endl;
cout << endl;
}
{
printHeader("T4: Checking powerup");
s1.set("Goku", 1990, 1900, 1, true);
int round = 0;
bool gokuWins = false;
while (!gokuWins) // with every fight, the super saiyan should power up
{
cout << "Round #" << ++round << endl;
gokuWins = s1.fight(s2);
s1.display();
s2.display();
}
cout << "Bonus round. Is s2 winning? " << (s2.fight(s1) ? "yes" : "no") << endl;
s1.display();
s2.display();
cout << endl;
}
{
printHeader("T5: Upgrading s2");
s2.set("Vegeta", 1990, 2200, 3, true);
cout << "Super Battle. Is s2 winning? " << (s2.fight(s1) ? "yes" : "no") << endl;
s1.display();
s2.display();
cout << endl;
}
return 0;
}
这是最终工作的结果:
/// Saiyan.cpp
#define _CRT_SECURE_NO_WARNINGS
#include <string.h>
#include <iostream>
#include "Saiyan.h"
using namespace std;
namespace sdds
{
Saiyan::Saiyan()
{
}
Saiyan::Saiyan(const char* name, int dob, int power)
{
set(name, dob, power);
}
void Saiyan::set(const char* name, int dob, int power, int level, bool super)
{
if (name != nullptr && name[0] != '\0')
{
if (m_name != nullptr)
{
delete[] m_name;
m_name = nullptr;
}
m_name = new char[strlen(name) + 1];
strcpy(m_name, name);
}
if (dob != 0 && dob < 2020)
{
m_dob = dob;
}
if (power > 0)
{
m_power = power;
}
if (level > 0)
{
m_level = level;
}
m_super = super;
}
bool Saiyan::isValid() const
{
bool valid_state = m_name != nullptr && m_dob != 0 && m_dob < 2020 && m_power > 0 && m_level >= 0;
return valid_state;
}
void Saiyan::display() const
{
if (isValid())
{
cout << m_name << endl;
cout.setf(ios::right);
cout.width(10);
cout << "DOB: " << m_dob << endl;
cout.width(10);
cout << "Power: " << m_power << endl;
cout.width(10);
if (m_super == true) {
cout << "Super: " << "yes" << endl;
cout.width(10);
cout << "Level: " << m_level;
}
else
{
cout << "Super: " << "no";
}
cout.unsetf(ios::left);
}
else
{
cout << "Invalid Saiyan!";
}
cout << endl;
}
bool Saiyan::fight(Saiyan& other)
{
// Check both Saiyans for super level and power up accordingly:
if (m_super == true)
{
m_power += int(m_power * (.1 * m_level)); // Cast an int to avoid possible memory loss.
}
if (other.m_super == true)
{
other.m_power += int(other.m_power * (.1 * other.m_level));
}
bool value = m_power > other.m_power;
return value;
}
Saiyan::~Saiyan()
{
if (m_name != nullptr)
{
delete[] m_name; // Deallocate memory of member.
m_name = nullptr;
}
}
}
// Saiyan.h
#pragma once
#ifndef SDDS_SAIYAN_H
#define SDDS_SAIYAN_H
namespace sdds
{
class Saiyan
{
char* m_name{}; // Dynamically allocated array of chars.
int m_dob{}; // Year the Saiyan was born.
int m_power{}; // Integer indicating the strength of the Saiyan (>= 0).
bool m_super{}; // indicates whether Saiyan can evolve
int m_level{}; // an integer indicating the level of a SS
/*
***Valid Name*** : a dynamically allocated array of chars.
***Valid Year of Birth***: an integer within the interval[0, 2020].
***Valid Power***: an integer that is greater than 0.
*/
public:
Saiyan();
Saiyan(const char* name, int dob, int power); // Custom constructor
void set(const char* name, int dob, int power, int level = 0, bool super = false);
bool isValid() const;
void display() const;
bool fight(Saiyan& other); // Fight and power up Saiyans.
~Saiyan();
};
}
#endif
最佳答案
通常---避免手动内存管理,为什么不只使用std::string?
关于代码中的问题。
这部分代码很重要:
if (name == nullptr || strlen(name) <= 0 || dob > 2020 || power <= 0)
{
*this = Saiyan(); // Calls constructor that creates default.
}
实际上,您实际上是在这里绕过析构函数,因此,如果初始化了m_name,则会泄漏内存。
Saiyan::Saiyan(const char* name, int dob, int power)
{
set(name, dob, power);
}
您不能确保始终在调用此构造函数后确保对象处于良好状态。
if (m_name != nullptr && strlen(name) == 0)
{
delete[] m_name;
m_name = nullptr;
}
仅当新名称简短时才取消分配m_name,但是无论新名称长度如何都应取消分配,因为您要将新值设置为m_name而不考虑新名称长度。
class Saiyan
{
char* m_name = nullptr; // Dynamically allocated array of chars.
int m_dob = 0; // Year the Saiyan was born.
int m_power = 0; // Integer indicating the strength of the Saiyan (>= 0).
bool m_super = false; // indicates whether Saiyan can evolve
int m_level = 0; // an integer indicating the level of a SS
public:
Saiyan() {};
...
关于c++ - CPP中的Valgrind和内存泄漏: “Conditional jump or move depends on uninitialised values”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62498819/
在 risc-v 规范中,它说 j 是 jal 的伪代码。我对它的工作原理感到困惑,因为 j (25 位立即数)和 jal (20 位立即数)的立即值范围不同 - 还有 jalr 带 12 位立即数。
希望这是我提出问题的正确位置。为什么不能使用这两个指令递归调用子例程? 提前致谢! 最佳答案 如果您“跳转并链接”,返回地址将存储在寄存器中。如果再次“跳转链接”,返回地址就会被新的地址覆盖,破坏原来
我正在尝试在运行服务器端口的数据库和另一台服务器之间创建一个 SSH 隧道,如下所示。 MySQL:3306 Server-A:3306 我想使用 Server-A:3306作为连接到数据库的数据库
我喜欢包含键:值的“跳转字典”的概念,其中值是函数。不过,我不确定我是否应该喜欢这个概念。 我想替换一长串 if if-else if 语句。 (我应该使用 switch 语句吗?) 有没有不用eva
我是 js 的新手我正在尝试通过 Prop isActive .但我收到一个错误。你们能告诉我为什么我会收到下面的错误代码吗?我在这一行收到了错误 import React from 'react'
我一直在创建自己的 UIControl 子类以用于我的调整 iDunnoU .我已经完成了 UIControl,但展开/折叠动画除外。这个动画的问题是它在展开/折叠时会向下/向上“跳跃”,而不是像我原
我正在尝试让相机在我的 LWJGL 程序中跳跃。我尝试编写一个 if/else 语句,它会说:“当你到达这个位置时,转到默认的起始位置。”到目前为止,它还在继续飞翔。这是我的代码: if (flyUp
在过去的一个半星期里,我一直在使用 Java 和 Swing 从头开始编写游戏。到目前为止,游戏运行一切顺利,除了一件事:跳跃。我正在尝试实现抛物线跳跃,以便玩家不只是向上传送一点点。相反,我希望
我如何处理 JuMP 中的稀疏矩阵? 例如,假设我想施加以下形式的约束: A * x == 0 哪里A是一个稀疏矩阵和 x变量向量。我假设 A 的稀疏性可以被利用来使优化更快。如何在 JuMP 中利用
我有一个文本字段,您通常在其中输入数字。但是,用户必须单击键盘上的数字按钮才能进入数字屏幕。有没有办法直接跳转到键盘上的屏幕并可以输入数字? 如有任何意见和建议,我们将不胜感激。 提前致谢。 最佳答案
粘性导航栏“跳跃” 我最近制作了一个导航栏,它在滚动时粘在屏幕顶部(进入网页时它从中间开始)。我是在 Mac 上做的,一切正常。但是,当在我的 Windows 桌面上进入网页时,整个导航栏要么跳动,跳
我有一个功能类似于 w3schools 的侧边栏,而 w3schools 的侧边栏显示相同的问题。侧边栏“跳跃”并在该区域留下难看的空间,当向下滚动页面和移动 chrome 的导航栏隐藏时,它在顶部留
我正在尝试创建一组在悬停时展开和收缩的 Font-Awesome 社交媒体图标。但是,当我停止将鼠标悬停在每个图标上时会出现问题,图标中间的图像会“跳跃”而不是像图标的其余部分那样平滑地折叠。这是代码
我的垂直自动滚动似乎在某些点不规律地停止,我想要一个平滑的滚动。其次,我希望垂直滚动自动重置(几乎无限循环或暗示它永无止境而不仅仅是跳跃)。我将如何着手完成这个?到目前为止,这是我的代码:http:/
我在 YouTube 上发布了一个视频,您可以在其中看到该问题。我放慢了视频速度,这样你就能真正看到跳跃。 链接:https://www.youtube.com/watch?v=17Wftj2-MRM
我在 Drupal 8 中构建了一个站点(但我认为这是一个 CSS 问题)。 当您重新加载页面时,页面“跳转”。它也会在您第一次访问时执行此操作,但如果您按“f5”,您会看得更清楚。我尝试删除 Log
如您所见,此背景将向下动画。我拥有我想要的一切正常工作,除了在动画完成后,它会跳转,这是我不想要的。我只希望它无休止地滚动而无需跳转到它。下面的 CodePen。 关于如何在大约 10 秒后继续滚动而
更新:这基本上是我想要的,但 div 固定在底部 - http://www.wduffy.co.uk/blog/wp-content/demos/jquery-scrolling-element/ 有
我正在 Python 3.4 中使用 numpy 和矩阵构建一个神经网络草图,以学习简单的 XOR。我的符号如下: a 是神经元的事件 z 是一个神经元的输入 W 是一个权重矩阵,大小为 R^{#上一
请原谅标题措辞不佳,希望我能在这里解释这个问题。 我用 jQuery 编写了一个简单的画廊控件,其中有六个图像和两个按钮控件,允许用户循环浏览整个图像集合。我的这部分工作正常,但是每当我单击一个按钮在
我是一名优秀的程序员,十分优秀!