我编写了一些C ++代码来实现自己的堆栈。但是,当我使用valgrind
进行检查时,它表示可能存在内存泄漏。我检查了这么久但仍然找不到它。
这是代码:
#ifndef RE_STACK_H
#define RE_STACK_H
#include <cstdlib>
#include "general.h"
template <typename T>
class Stack {
private:
T* bottom;
T* top;
T* sentinel;
uint32 size;
public:
Stack(uint32 size = 30);
~Stack();
void push(const T&);
T pop();
void expand();
};
template <typename T>
Stack<T>::Stack(uint32 size) :size(size){
bottom = top = static_cast<T*>(malloc(size * sizeof(T)));
sentinel = bottom + size * sizeof(T);
}
template <typename T>
Stack<T>::~Stack() {
while (top-- != bottom){
top->~T();
}
free(bottom);
}
template <typename T>
void Stack<T>::push(const T& item){
if(top == sentinel){
expand();
}
new(top++)T(item);
}
template <typename T>
T Stack<T>::pop() {
if(bottom == top){
return 0;
}
T re = *--top;
top->~T();
return re;
}
template <typename T>
void Stack<T>::expand() {
size *= 2;
uint32 temp = top - bottom;
bottom = static_cast<T*>(realloc(bottom, size * sizeof(T)));
top = bottom + temp;
sentinel = bottom + size * sizeof(T);
}
#endif //RE_STACK_H
#include <iostream>
#include "NTL/Stack.h"
int main() {
return 0;
}
这是来自valgrind
的错误信息:
==33519== HEAP SUMMARY:
==33519== in use at exit: 22,216 bytes in 190 blocks
==33519== total heap usage: 256 allocs, 66 frees, 27,992 bytes allocated
==33519==
==33519== 2,064 bytes in 1 blocks are possibly lost in loss record 58 of 63
==33519== at 0x10000817C: malloc_zone_malloc (in /usr/local/Cellar/valgrind/3.11.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==33519== by 0x1005E1EFD: _objc_copyClassNamesForImage (in /usr/lib/libobjc.A.dylib)
==33519== by 0x1005D5182: protocols() (in /usr/lib/libobjc.A.dylib)
==33519== by 0x1005D5093: readClass(objc_class*, bool, bool) (in /usr/lib/libobjc.A.dylib)
==33519== by 0x1005D2C13: gc_init (in /usr/lib/libobjc.A.dylib)
==33519== by 0x1005DA24E: objc_initializeClassPair_internal(objc_class*, char const*, objc_class*, objc_class*) (in /usr/lib/libobjc.A.dylib)
==33519== by 0x1005E7132: layout_string_create (in /usr/lib/libobjc.A.dylib)
==33519== by 0x1005D583C: realizeClass(objc_class*) (in /usr/lib/libobjc.A.dylib)
==33519== by 0x1005D5300: copySwiftV1MangledName(char const*, bool) (in /usr/lib/libobjc.A.dylib)
==33519== by 0x1005D52E9: copySwiftV1MangledName(char const*, bool) (in /usr/lib/libobjc.A.dylib)
==33519== by 0x1005D52E9: copySwiftV1MangledName(char const*, bool) (in /usr/lib/libobjc.A.dylib)
==33519== by 0x1005D52E9: copySwiftV1MangledName(char const*, bool) (in /usr/lib/libobjc.A.dylib)
==33519==
==33519== LEAK SUMMARY:
==33519== definitely lost: 0 bytes in 0 blocks
==33519== indirectly lost: 0 bytes in 0 blocks
==33519== possibly lost: 2,064 bytes in 1 blocks
==33519== still reachable: 0 bytes in 0 blocks
==33519== suppressed: 20,152 bytes in 189 blocks
==33519==
==33519== For counts of detected and suppressed errors, rerun with: -v
==33519== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 18 from 18)
我看过几条评论,我想解释一下:
我使用malloc()
和free()
而不是new
和delete
,因为我想分开内存分配和类构造的过程,你可以在我的析构函数中看到,我在那里释放内存。每次我pop()
一个项目,我都会使用类的析构函数来确保它被正确破坏,但是我没有释放内存,因为我以后可能会使用它。
感谢Joachim Pileborg,sentinel = bottom + size * sizeof(T);
应更改为sentinel = bottom + size;
,但这并不是valgrind
说 1个块中的2,064个字节可能丢失的原因在损失记录58 of 63
我知道我没有遵循3/5规则,事实上我没有完成代码,我将编写复制构造函数并稍后分配运算符,我只想测试代码时并不大。
请不要说代码是垃圾,只要看一眼就注意我使用malloc()
;如果您看到STL的源代码,您将看到分配器的工作方式
答案 0 :(得分:0)
最后我认为这是因为valgrind
。我不知道为什么,即使我写的跟注码一样简单,我也有错误......
int main(int argc, char* argv[]){
return 0;
}
无论如何,感谢那些帮助我找出错误的人:
sentinel
只需要添加尺寸...... realloc
无法帮助我调用析构函数。