[英]Is there a possible memory leak in my C++ file?
I'm using an example code given to me by another C++ coder for a project. 我正在使用另一个C ++编码器为项目提供的示例代码。 I'm a new student of C++ language and I wondered is there a possible memory leak / bugs in this class file given to me (PlacementHead.cpp):
我是C ++语言的新学员,我想知道在给我(PlacementHead.cpp)的此类文件中是否可能发生内存泄漏/错误:
#include "PlacementHead.h"
#include <string>
#include <iostream>
#include <string.h>
PlacementHead::PlacementHead(int width, int height, int gap, char* s) {
width_ = width;
height_ = height;
gap_ = gap;
size_ = (width*height)+1;
set_ = new char[size_ + 1];
from_ = new int[size_ + 1];
original_ = new char[size_ + 1];
strcpy(set_,s);
strcpy(original_,s);
}
PlacementHead::~PlacementHead() {
}
int PlacementHead::getSize() { return size_; }
int PlacementHead::getHeight() { return height_; }
int PlacementHead::getWidth() { return width_; }
int PlacementHead::getGap() { return gap_; }
// Palauttaa indeksissä i olevan suuttimen
char PlacementHead::getNozzle(int i) {
return set_[i-1];
}
// Asettaa indeksissä i olevan suuttimen
void PlacementHead::setNozzle(int i, char c) {
set_[i-1] = c;
}
// Merkitsee suuttimen poimituksi poistamalla sen listasta
void PlacementHead::markNozzle(int i, int bankPos) {
set_[i-1] = ' ';
from_[i-1] = bankPos;
}
// Palauttaa seuraavan poimimattoman suuttimen indeksin
int PlacementHead::getNextUnmarkedPos() {
for (int i=0; i<size_; i++) {
if (set_[i]!=' ') {
return i+1;
}
}
return 0;
}
// Palauttaa suuttimen alkuperäisen sijainnin pankissa
int PlacementHead::getBankPos(int i) {
return from_[i-1];
}
// Plauttaa alkuperäisen ladontapaan suutinjärjestyksen
void PlacementHead::reset() {
//for (int i=0; i<size_; i++) {
// set_[i] = original_[i];
//}
strcpy(set_,original_);
}
// Tulostusmetodi
void PlacementHead::print() {
std::cout << "ladontapaa:\n";
for (int h=height_; h>0; h--) {
for (int w=width_; w>0; w--) {
int i = ((h-1)*width_)+w;
std::cout << getNozzle(i);
}
std::cout << "\n";
}
}
PlacementHead.h: PlacementHead.h:
#ifndef PLACEMENTHEAD_H
#define PLACEMENTHEAD_H
class PlacementHead {
public:
PlacementHead(int size, int rows, int gap, char* s);
~PlacementHead();
int getSize();
int getHeight();
int getWidth();
int getGap();
char getNozzle(int i);
void setNozzle(int i, char c);
void markNozzle(int i, int bankPos);
int getNextUnmarkedPos();
int getBankPos(int i);
void reset();
void print();
private:
char* set_;
int* from_;
char* original_;
int size_;
int width_;
int height_;
int gap_;
};
#endif
I notice that there is dynamic allocation of memory, but I don't see a delete
anywhere...is this a problem? 我注意到内存是动态分配的,但是在任何地方都看不到
delete
...这是问题吗? How could I fix this if it is a problem? 如果有问题该怎么解决?
Thnx for any help! Thnx寻求任何帮助!
PS 聚苯乙烯
I noticed there is no keyword class
used in this example?...Can you define a class like this? 我注意到此示例中没有使用关键字
class
吗?...可以定义这样的类吗?
It's impossible to say without seeing the class definition (the header); 不看类定义(标题)就不可能说; if
size_
, etc. are something like boost::shared_array
, or std::unique_ptr
, there is no leak. 如果
size_
等类似boost::shared_array
或std::unique_ptr
,则没有泄漏。 If they are simply int*
, there is a leak. 如果它们只是
int*
,则存在泄漏。
Of course, no C++ programmer would write this sort of code anyway. 当然,没有C ++程序员会写这种代码。 The class would contain
std::vector<int>
and std::string
. 该类将包含
std::vector<int>
和std::string
。 Judging from what we see here, the author doesn't know C++. 从我们在这里看到的内容来看,作者并不了解C ++。
Another problem is that your code does not obey Rule of three (links here and here ) 另一个问题是您的代码不遵守三个规则( 此处和此处的链接)
once you will write code like: 一旦您将编写如下代码:
{
PlacementHead a(0,0,0,"asdsa");
PlacementHead b(0,0,0,"asdsa");
a = b; // line 1
} // here segfault
you will get segfault, in line 1
, pointers will be copied from b
to a
, and once you will finally have destructors, pointers will be deleted twice, which is wrong. 您将在
line 1
遇到段错误,将指针从b
复制到a
,一旦您最终拥有析构函数,指针将被删除两次,这是错误的。 This is called shallow copy, you need deep copy, where new array will be allocated. 这称为浅拷贝,您需要深拷贝,将在其中分配新阵列。
the code has leak . 代码泄漏了。 the constructor allocates the memory .Destructor or some other function have to clean that before the object gets destroyed
构造函数分配内存。析构函数或其他函数必须在销毁对象之前清除内存
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.