作为大学项目的一部分,我正在从头开始编写文本编辑器。我遇到的问题是我编写的用于构建维护核心数据结构的代码。
下面是我的源代码,gcc / g ++给出的错误,以及我从gdb获得的堆栈跟踪。我将在下面详细介绍我的设计。
是的,我对Document :: insertChar中的所有打印和详细声明感到不舒服。我认为这可能是一个初始化问题。我还在用gdb学习缰绳。
重要的是,temp.push_back(lnd);和Lines.insert(iter,nline); (lnd是originall nLine)都会导致相同的错误。
源码
Document.cpp : http://pastebin.com/LgGHmir8
Line.cpp : http://pastebin.com/BBhbnxUt
(代码从未在非Linux平台上测试,使用g ++构建可能不会与其他编译器一起使用)
错误:
test:malloc.c:2388:sysmalloc:断言`(old_top ==(((mbinptr)(((char *)&((av) - > bins [((1) - 1)* 2 ])) - __builtin_offsetof(struct malloc_chunk,fd))))&& old_size == 0)|| ((unsigned long)(old_size)> =(unsigned long)(((__ builtin_offsetof(struct malloc_chunk,fd_nextsize))+((2 *(sizeof(size_t))) - 1))&〜((2 * (sizeof(size_t))) - 1)))&&((old_top) - > size& 0x1)&&((unsigned long)old_end&(pagesize - 1))== 0) '失败了。 中止(核心倾销)
堆栈跟踪:
(gdb) backtrace
#0 0x00007ffff71d45f8 in raise () from /usr/lib/libc.so.6
#1 0x00007ffff71d5a7a in abort () from /usr/lib/libc.so.6
#2 0x00007ffff7218468 in __malloc_assert () from /usr/lib/libc.so.6
#3 0x00007ffff721a056 in sysmalloc () from /usr/lib/libc.so.6
#4 0x00007ffff721b0a6 in _int_malloc () from /usr/lib/libc.so.6
#5 0x00007ffff721c3d4 in malloc () from /usr/lib/libc.so.6
#6 0x00007ffff7ae70e8 in operator new (sz=24) at /build/gcc/src/gcc-5.3.0/libstdc++-v3/libsupc++/new_op.cc:50
#7 0x0000000000401f15 in __gnu_cxx::new_allocator<Line>::allocate (this=0x7fffffffe950, __n=1) at /usr/include/c++/5.3.0/ext/new_allocator.h:104
#8 0x0000000000401d9e in __gnu_cxx::__alloc_traits<std::allocator<Line> >::allocate (__a=..., __n=1) at /usr/include/c++/5.3.0/ext/alloc_traits.h:182
#9 0x0000000000401b90 in std::_Vector_base<Line, std::allocator<Line> >::_M_allocate (this=0x7fffffffe950, __n=1) at /usr/include/c++/5.3.0/bits/stl_vector.h:170
#10 0x0000000000401654 in std::vector<Line, std::allocator<Line> >::_M_insert_aux (this=0x7fffffffe950, __position=<error reading variable: Cannot access memory at address 0x0>,
__x=...) at /usr/include/c++/5.3.0/bits/vector.tcc:353
#11 0x000000000040139e in std::vector<Line, std::allocator<Line> >::push_back (this=0x7fffffffe950, __x=...) at /usr/include/c++/5.3.0/bits/stl_vector.h:925
#12 0x0000000000400fff in Document::insertChar (this=0x7fffffffe9b0, chr=10 '\n') at Document.cpp:67
#13 0x00000000004011be in main (argc=1, argv=0x7fffffffeae8) at Document.cpp:204
设计简介,我正在使用std :: vector来保存线条(主要是为了方便起见)。和类Line一样,类似于 向量,它是动态数组的一个实现。原因是我最终会在那里持有行元数据。
当我尝试使用insert插入一行时,Vector似乎不喜欢它。 实际上,超出了Document :: Document()。每当我尝试将Line对象插入向量时,无论是push_back还是insert。它失败了。
我...不太明白为什么,并且愿意。失败了 - 我只是要实现自己的版本,所以我至少要了解发生了什么。 (遗憾的是你不能在插入和搜索操作上获得O(1)...如果我不需要快速搜索,链接列表会很好...)
答案 0 :(得分:0)
好的,睡了一觉。回到这个。
在复制构造函数中调用 delete [] mData
。 mData
在复制构造函数的初始化列表中初始化为NULL
。这就是问题A.更重要的是;
std::copy(ln.mData, **mData**+mCapacity, mData);
不知道那时内存地址mData
是什么。但它确定不是故意的。这是分配问题的根源。感谢指导我查看复制构造函数的人。