这段c++代码有内存泄露?

【字号: 日期:2023-04-23浏览:29作者:雯心

问题描述

学习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

valgrind错误信息如下 :

==32859== HEAP SUMMARY:==32859== in use at exit: 22,216 bytes in 190 blocks==32859== total heap usage: 256 allocs, 66 frees, 27,992 bytes allocated==32859== ==32859== 2,064 bytes in 1 blocks are possibly lost in loss record 58 of 63==32859== at 0x10000817C: malloc_zone_malloc (in /usr/local/Cellar/valgrind/3.11.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)==32859== by 0x1005E1EFD: _objc_copyClassNamesForImage (in /usr/lib/libobjc.A.dylib)==32859== by 0x1005D5182: protocols() (in /usr/lib/libobjc.A.dylib)==32859== by 0x1005D5093: readClass(objc_class*, bool, bool) (in /usr/lib/libobjc.A.dylib)==32859== by 0x1005D2C13: gc_init (in /usr/lib/libobjc.A.dylib)==32859== by 0x1005DA24E: objc_initializeClassPair_internal(objc_class*, char const*, objc_class*, objc_class*) (in /usr/lib/libobjc.A.dylib)==32859== by 0x1005E7132: layout_string_create (in /usr/lib/libobjc.A.dylib)==32859== by 0x1005D583C: realizeClass(objc_class*) (in /usr/lib/libobjc.A.dylib)==32859== by 0x1005D5300: copySwiftV1MangledName(char const*, bool) (in /usr/lib/libobjc.A.dylib)==32859== by 0x1005D52E9: copySwiftV1MangledName(char const*, bool) (in /usr/lib/libobjc.A.dylib)==32859== by 0x1005D52E9: copySwiftV1MangledName(char const*, bool) (in /usr/lib/libobjc.A.dylib)==32859== by 0x1005D52E9: copySwiftV1MangledName(char const*, bool) (in /usr/lib/libobjc.A.dylib)==32859== ==32859== LEAK SUMMARY:==32859== definitely lost: 0 bytes in 0 blocks==32859== indirectly lost: 0 bytes in 0 blocks==32859== possibly lost: 2,064 bytes in 1 blocks==32859== still reachable: 0 bytes in 0 blocks==32859== suppressed: 20,152 bytes in 189 blocks==32859== ==32859== For counts of detected and suppressed errors, rerun with: -v==32859== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 18 from 18)

我的main函数里面只有一句话, 就是return 0; 但是头文件中包括了上面这个文件...

#ifndef RE_GENERAL_H#define RE_GENERAL_H#ifndef NULL#define NULL 0#endifusing uint8 = unsigned char;using uint32 = unsigned int;using int32 = int;#endif //RE_GENERAL_H

#include <iostream>#include 'NTL/Stack.h'int main() {return 0;}

问题解答

回答1:

讲道理我是不相信的。。。最好贴出你的测试代码,以及general.h的内容。

回答2:

valgrind 能用来检测c++ 的程序吗

相关文章: