new[]得到的内存居然不能delete[],WHY,代码比较多,有心者进。该如何解决
new[]得到的内存居然不能delete[],WHY,代码比较多,有心者进。
new[]得到的内存居然不能delete[],WHY,代码比较多,有心者进。
《提高C++性能的编程技术》一书源代码中,固定大小对象的内存池,有误,百思不得其解,最终的改正却运行成功,但还是不解。
三个源文件:
//MemoryPool.h
#ifndef MEMORYPOOL_H_
#define MEMORYPOOL_H_
template <class T>
class MemoryPool
{
private:
MemoryPool <T> * next;
enum {EXPANSION_SIZE = 32};
void expandTheFreeList(int howMany = EXPANSION_SIZE);
public:
MemoryPool(size_t size = EXPANSION_SIZE);
~MemoryPool();
inline void* alloc(size_t size);
inline void free(void* doomed);
};
template <class T>
MemoryPool <T> ::MemoryPool(size_t size)
{
expandTheFreeList(size);
}
template <class T>
MemoryPool <T> ::~MemoryPool()
{
MemoryPool <T> * nextPtr = next;
delete nextPtr;//此处为我修改代码,代替下面的注释代码才可运行。
// for (nextPtr = next; nextPtr != NULL; nextPtr = next)
// {
// next = next-> next;
// delete [] nextPtr;
////delete nextPtr;
// }
}
template <class T>
inline
void* MemoryPool <T> ::alloc(size_t size)
{
if (!next)
expandTheFreeList();
MemoryPool <T> * head = next;
next = head-> next;
return head;
}
template <class T>
inline
void MemoryPool <T> ::free(void* doomed)
{
MemoryPool <T> * head = static_cast <MemoryPool <T> * > (doomed);
head-> next = next;
next = head;
}
template <class T>
inline
void MemoryPool <T> ::expandTheFreeList(int howMany)
{
size_t size = (sizeof (T) > sizeof (MemoryPool <T> *)) ?
sizeof (T) : sizeof (MemoryPool <T> *);
MemoryPool <T> * runner = static_cast <MemoryPool <T> * > ((void*)new char[size]);
next = runner;
for (int i = 0; i < howMany; i++)
{
runner-> next = static_cast <MemoryPool <T> * > ((void*)new char[size]);
runner = runner-> next;
new[]得到的内存居然不能delete[],WHY,代码比较多,有心者进。
《提高C++性能的编程技术》一书源代码中,固定大小对象的内存池,有误,百思不得其解,最终的改正却运行成功,但还是不解。
三个源文件:
//MemoryPool.h
#ifndef MEMORYPOOL_H_
#define MEMORYPOOL_H_
template <class T>
class MemoryPool
{
private:
MemoryPool <T> * next;
enum {EXPANSION_SIZE = 32};
void expandTheFreeList(int howMany = EXPANSION_SIZE);
public:
MemoryPool(size_t size = EXPANSION_SIZE);
~MemoryPool();
inline void* alloc(size_t size);
inline void free(void* doomed);
};
template <class T>
MemoryPool <T> ::MemoryPool(size_t size)
{
expandTheFreeList(size);
}
template <class T>
MemoryPool <T> ::~MemoryPool()
{
MemoryPool <T> * nextPtr = next;
delete nextPtr;//此处为我修改代码,代替下面的注释代码才可运行。
// for (nextPtr = next; nextPtr != NULL; nextPtr = next)
// {
// next = next-> next;
// delete [] nextPtr;
////delete nextPtr;
// }
}
template <class T>
inline
void* MemoryPool <T> ::alloc(size_t size)
{
if (!next)
expandTheFreeList();
MemoryPool <T> * head = next;
next = head-> next;
return head;
}
template <class T>
inline
void MemoryPool <T> ::free(void* doomed)
{
MemoryPool <T> * head = static_cast <MemoryPool <T> * > (doomed);
head-> next = next;
next = head;
}
template <class T>
inline
void MemoryPool <T> ::expandTheFreeList(int howMany)
{
size_t size = (sizeof (T) > sizeof (MemoryPool <T> *)) ?
sizeof (T) : sizeof (MemoryPool <T> *);
MemoryPool <T> * runner = static_cast <MemoryPool <T> * > ((void*)new char[size]);
next = runner;
for (int i = 0; i < howMany; i++)
{
runner-> next = static_cast <MemoryPool <T> * > ((void*)new char[size]);
runner = runner-> next;