行为:内存操作错误或内存泄漏C++
Behavior: Bad operation on memory or Memory leak C++
我是C++新手,我遇到了这样的错误:
行为:内存操作错误或内存泄漏: MyInteger::isPrime(int( (MyInteger.cpp:72( by: main (main.cpp:23(
at: MyInteger::isPrime(int( (MyInteger.cpp:59(
这是什么意思,我该如何解决这个问题?我试图使用valgrind
但对新生来说似乎有点复杂。
//MyInteger.cpp
bool MyInteger::isPrime(int z){
int i,flag1;
if(z == 1){
flag1 = 1;
}
for(i = 2; i < z; i++){
if(z % i == 0)
{
flag1 = 1;
break;
}else flag1 = 0;
}
if(flag1 == 1){
return false;
}else return true;
}
//MyInteger.hpp
class MyInteger{
public:
static bool isPrime(int);
};
//main.cpp
int main{
const string words[3][2] = {"not even", "even",
"not odd", "odd",
"not prime", "prime"};
..............................
..............................
...............................
cout << "Integer " << b << " is: ";
cout << words[0][MyInteger::isEven(b)] << ", ";
cout << words[1][MyInteger::isOdd(b)] << ", ";
cout << words[2][MyInteger::isPrime(b)] << ".n";
}
似乎您的函数isPrime
是私有函数,无法在main((中调用这段代码对我有用
// Example program
#include <iostream>
#include <string>
using namespace std;
class MyInteger
{
public: static bool isPrime(int);
};
bool MyInteger::isPrime(int z)
{
int i,flag1;
if(z == 1){
flag1 = 1;
}
for(i = 2; i < z; i++){
if(z % i == 0)
{
flag1 = 1;
break;
}else flag1 = 0;
}
if(flag1 == 1){
return false;
}else return true;
}
int main()
{
const string words[3][2] = {"not even", "even",
"not odd", "odd",
"not prime", "prime"};
int b=2;
cout << "Integer " << b << " is: ";
cout << words[2][MyInteger::isPrime(b)] << ".n";
}
输出
Integer 2 is: not prime.
相关文章:
- 从构造函数抛出异常时如何克服内存泄漏
- malloc() 可能出现内存泄漏
- 这个极客对极客的trie实现是否存在内存泄漏问题
- 尽管遵循了规则,内存泄漏在哪里
- 为什么调用堆栈数组会导致内存泄漏
- 在简单示例中,Python3 + ctypes 回调会导致内存泄漏
- 使用模板类的自定义列表类型中的内存泄漏
- 为什么以下C++代码中存在内存泄漏?
- OpenCV 我应该使用智能指针来防止内存泄漏吗?
- 我是否生成线程并导致内存泄漏?
- 多线程程序中出现意外的内存泄漏
- 为什么此函数会导致内存泄漏?
- 在 C++ 库中使用cythonized python时内存泄漏
- 需要帮助查找内存泄漏
- 瓦尔格林德的内存泄漏使用新的
- 无法找出我的代码中的内存泄漏
- C++ 结构内部的unordered_map会导致内存泄漏问题吗?
- 可视化 使用 VS Code 查找C++应用程序中的内存泄漏
- Shared_ptr双链接列表内存泄漏
- C++ 在类中使用常量引用文本时 O2 内存泄漏