次のエラーが発生する次のコードがあります
エラー C2036: 'void *': 不明なサイズ
エラー C2440: 'static_cast': 'void *' から 'size_t' に変換できません
行で
void *addr = static_cast <void*> (static_cast <size_t> (mem + bytesAlreadyAllocated));
私の質問は、上記のエラーが発生する理由と、これらのエラーを取り除く方法です。
手伝ってくれてありがとう。
class MemoryChunk {
public:
MemoryChunk (MemoryChunk *nextChunk, size_t chunkSize);
~MemoryChunk() {delete mem; }
inline void *alloc (size_t size);
inline void free (void* someElement);
// Pointer to next memory chunk on the list.
MemoryChunk *nextMemChunk() {return next;}
// How much space do we have left on this memory chunk?
size_t spaceAvailable() { return chunkSize - bytesAlreadyAllocated; }
// this is the default size of a single memory chunk.
enum { DEFAULT_CHUNK_SIZE = 4096 };
private:
// The MemoryChunk class is a cleaner version of NextOnFreeList. It separates the next pointer from
// the actual memory used for the allocated object. It uses explicit next and mem pointers, with no
// need for casting.
MemoryChunk *next;
void *mem;
// The size of a single memory chunk.
size_t chunkSize;
// This many bytes already allocated on the current memory chunk.
size_t bytesAlreadyAllocated;
};
MemoryChunk::MemoryChunk(MemoryChunk *nextChunk, size_t reqSize) {
chunkSize = (reqSize > DEFAULT_CHUNK_SIZE) ? reqSize : DEFAULT_CHUNK_SIZE;
next = nextChunk;
bytesAlreadyAllocated = 0;
mem = new char [chunkSize];
}
void* MemoryChunk :: alloc (size_t requestSize) {
void *addr = static_cast <void*> (static_cast <size_t> (mem + bytesAlreadyAllocated));
bytesAlreadyAllocated += requestSize;
return addr;
}
inline void MemoryChunk :: free (void *doomed) {}