- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这是我的作业说明:复制构造函数。复制构造函数应该执行参数对象的深层复制,即它应该构造一个与参数具有相同大小和容量的 IntCollection,并具有参数数据数组的完整拷贝。
赋值运算符 (=)。赋值运算符还应该执行参数对象的深层复制。它必须返回自身(或更有效地返回对自身的引用),以便支持同一行上的多个赋值,例如a = b = c。如果您首先实现赋值运算符,则可以在复制构造函数中使用它,但这不是必需的。
等于运算符 (==)。如果参数对象与接收对象具有相同的大小,并且两个对象的数据数组中的值相同,则“等于”运算符应返回 true。
插入运算符 (<<)。插入运算符应将 int 参数添加到接收 IntCollection 中。功能与 add() 函数完全相同,即将整数添加到集合中。但请注意,该函数必须返回对其自身的引用,以便支持同一行上的多次插入,例如c << 45 << -210。与赋值运算符不同的是,这个返回必须通过引用来完成,因为每次插入实际上都会修改 IntCollection 对象,并且插入是从左到右进行的。
析构函数。当需要更多空间时,函数 add() 调用 addCapacity() 来分配内存。这个程序中没有任何地方用delete[]释放内存,这意味着我们有内存泄漏!添加一个正确处理此问题的析构函数。
添加容量。请注意,addCapacity() 是一个私有(private)成员函数。如果您尝试从类外部调用它,即通过将下面的行添加到 main() 中,会发生什么?
c.addCapacity();
这是我的代码:IntCollection.h:
#ifndef INTCOLLECTION_H
#define INTCOLLECTION_H
// Allocate memory in chunks of ints of this size.
const int CHUNK_SIZE = 5;
class IntCollection
{
private:
// The number of ints currently stored in the int
int size;
// the total number of elements available for storage
// in the data array
int capacity;
// A pointer to the dynamically allocated data array
int* data;
// a private member function to allocate more memory
// if necessary
void addCapacity();
public:
// Constructor
IntCollection();
// Destructor
~IntCollection();
// Copy constructor:
IntCollection(const IntCollection &c);
void add(int value);
int get(int index);
int getSize();
IntCollection& operator=(const IntCollection &c);
bool operator==(const IntCollection &c);
IntCollection& operator<<(int value);
};
#endif
IntCollection.cpp:
#include "IntCollection.h"
#include <cstdlib>
#include <iostream>
using namespace std;
IntCollection::IntCollection()
{
// Initialize member data to reflect an empty
// IntCollection
size = capacity = 0;
data = NULL;
}
IntCollection::~IntCollection()
{
delete [] data;
}
IntCollection::IntCollection(const IntCollection &c) {
size = c.size;
capacity = c.capacity;
data = c.data;
for (int i = 0; i < c.size; i++)
{
data[i] = c.data[i];
}
}
void IntCollection::addCapacity()
{
// Create a new, bigger buffer, copy the current data to
// it, delete the old buffer, and point our data
// pointer to the new buffer
int *newData;
data = new int[capacity];
capacity += CHUNK_SIZE;
newData = new int[capacity];
for (int i = 0; i < size; i++)
{
newData[i] = data[i];
delete[] data;
data = newData;
}
}
void IntCollection::add(int value)
{
// first, allocate more memory if we need to
if (size == capacity)
{
addCapacity();
}
// Now, add the data to our array and increment size
data[size++] = value;
}
int IntCollection::get(int index)
{
if (index < 0 || index >= size)
{
cout << "ERROR: get() trying to access index out of range.\n";
exit(1);
}
return data[index];
}
int IntCollection::getSize()
{
return size;
}
IntCollection &IntCollection::operator=(const IntCollection &c)
{
size = c.size;
capacity = c.capacity;
data = c.data;
return *this;
}
bool IntCollection::operator==(const IntCollection &c)
{
if ((size == c.size) && (capacity == c.capacity))
{
for (int m = 0; m < size; m++)
{
if (data[m] == c.data[m])
{
continue;
} else
{
return false;
}
}
}
return true;
}
IntCollection &IntCollection::operator<<(int value)
{
add(value);
return *this;
}
main.cpp:
#include "IntCollection.h"
#include <iostream>
using namespace std;
int main()
{
IntCollection c;
c.add(45);
c.add(-210);
c.add(77);
c.add(2);
c.add(-21);
c.add(42);
c.add(7);
for (int i = 0; i < c.getSize(); i++)
{
cout << c.get(i) << endl;
}
IntCollection d(c);
for (int i = 0; i < c.getSize(); i++)
{
cout << c.get(i) << endl;
}
IntCollection e;
e = c;
cout << "Testing = Overload" << endl;
for(int i = 0; i < c.getSize(); i++)
{
cout << c.get(i) << endl;
}
IntCollection f;
f<<8<<9<<10;
cout << "Testing<<Overload" << endl;
for(int i = 0; i < f.getSize(); i++)
{
cout << f.get(i) << endl;
}
cout << "Testing == Overload" << endl;
c.add(10);
if(f == c)
{
cout << "Both objects match" << endl;
}
else
{
cout << "They don't match" << endl;
}
return 0;
}
在我相信我修复了大部分错误之后,我得到以下输出:
*** Error in `./main': double free or corruption (fasttop): 0x0000000000b2ec80 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x70bfb)[0x7ff7e6f70bfb]
/lib/x86_64-linux-gnu/libc.so.6(+0x76fc6)[0x7ff7e6f76fc6]
/lib/x86_64-linux-gnu/libc.so.6(+0x7780e)[0x7ff7e6f7780e]
./main[0x400fa1]
./main[0x400fe2]
./main[0x400aa2]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf1)[0x7ff7e6f202e1]
./main[0x40097a]
======= Memory map: ========
00400000-00402000 r-xp 00000000 08:01 11008976 /home/runner/main
我不会全部发布,因为它很长。是我的析构函数造成的吗?我不知道如何解决这个问题,也没有遇到过这个错误。
最佳答案
复制构造函数中的这一行可能是问题:
data = c.data;
在该行之后,您有两个对象指向相同数据。这是一个浅拷贝。
如果其中一个对象被破坏,那么 if 将会删除[]
数据,而给另一个对象留下无效的指针。当第二个对象析构函数尝试删除[]
数据(再次!)时,它将导致 undefined behavior .
您需要进行深层复制,这是您的作业。这包括新内存分配和实际复制数据。
与复制赋值运算符相同。
关于c++ - 我的析构函数是否给我这个错误 : *** Error in `./main' : double free or corruption (fasttop):?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50034580/
我有一个 if 语句,如下所示 if (not(fullpath.lower().endswith(".pdf")) or not (fullpath.lower().endswith(tup
然而,在 PHP 中,可以: only appears if $foo is true. only appears if $foo is false. 在 Javascript 中,能否在一个脚
XML有很多好处。它既是机器可读的,也是人类可读的,它具有标准化的格式,并且用途广泛。 它也有一些缺点。它是冗长的,不是传输大量数据的非常有效的方法。 XML最有用的方面之一是模式语言。使用模式,您可
由于长期使用 SQL2000,我并没有真正深入了解公用表表达式。 我给出的答案here (#4025380)和 here (#4018793)违背了潮流,因为他们没有使用 CTE。 我很欣赏它们对于递
我有一个应用程序: void deleteObj(id){ MyObj obj = getObjById(id); if (obj == null) { throw n
我的代码如下。可能我以类似的方式多次使用它,即简单地说,我正在以这种方式管理 session 和事务: List users= null; try{ sess
在开发J2EE Web应用程序时,我通常会按以下方式组织我的包结构 com.jameselsey.. 控制器-控制器/操作转到此处 服务-事务服务类,由控制器调用 域-应用程序使用的我的域类/对象 D
这更多是出于好奇而不是任何重要问题,但我只是想知道 memmove 中的以下片段文档: Copying takes place as if an intermediate buffer were us
路径压缩涉及将根指定为路径上每个节点的新父节点——这可能会降低根的等级,并可能降低路径上所有节点的等级。有办法解决这个问题吗?有必要处理这个吗?或者,也许可以将等级视为树高的上限而不是确切的高度? 谢
我有两个类,A 和 B。A 是 B 的父类,我有一个函数接收指向 A 类型类的指针,检查它是否也是 B 类型,如果是将调用另一个函数,该函数接受一个指向类型 B 的类的指针。当函数调用另一个函数时,我
有没有办法让 valgrind 使用多个处理器? 我正在使用 valgrind 的 callgrind 进行一些瓶颈分析,并注意到我的应用程序中的资源使用行为与在 valgrind/callgrind
假设我们要使用 ReaderT [(a,b)]超过 Maybe monad,然后我们想在列表中进行查找。 现在,一个简单且不常见的方法是: 第一种可能性 find a = ReaderT (looku
我的代码似乎有问题。我需要说的是: if ( $('html').attr('lang').val() == 'fr-FR' ) { // do this } else { // do
根据this文章(2018 年 4 月)AKS 在可用性集中运行时能够跨故障域智能放置 Pod,但尚不考虑更新域。很快就会使用更新域将 Pod 放入 AKS 中吗? 最佳答案 当您设置集群时,它已经自
course | section | type comart2 : bsit201 : lec comart2 :
我正在开发自己的 SDK,而这又依赖于某些第 3 方 SDK。例如 - OkHttp。 我应该将 OkHttp 添加到我的 build.gradle 中,还是让我的 SDK 用户包含它?在这种情况下,
随着 Rust 越来越充实,我对它的兴趣开始激起。我喜欢它支持代数数据类型,尤其是那些匹配的事实,但是对其他功能习语有什么想法吗? 例如标准库中是否有标准过滤器/映射/归约函数的集合,更重要的是,您能
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 9 年前。 Improve
我一直在研究 PHP 中的对象。我见过的所有示例甚至在它们自己的对象上都使用了对象构造函数。 PHP 会强制您这样做吗?如果是,为什么? 例如: firstname = $firstname;
...比关联数组? 关联数组会占用更多内存吗? $arr = array(1, 1, 1); $arr[10] = 1; $arr[] = 1; // <- index is 11; does the
我是一名优秀的程序员,十分优秀!