简体   繁体   English

在VS 2010中,static_cast无法从void *转换为C ++中的size_t错误

[英]static_cast cannot convert from void* to size_t error in C++ in VS 2010

I am having following code which I am getting following error 我有以下代码,我得到以下错误

error C2036: 'void *' : unknown size 错误C2036:'void *':未知大小
error C2440: 'static_cast' : cannot convert from 'void *' to 'size_t' 错误C2440:'static_cast':无法从'void *'转换为'size_t'

at line 在线

void *addr = static_cast <void*> (static_cast <size_t> (mem + bytesAlreadyAllocated));

My question is why I am getting above errors and how to get rid of these errors? 我的问题是为什么我遇到错误以及如何摆脱这些错误?

Thanks for help. 感谢帮助。

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) {}

the expression: 表达方式:

static_cast <size_t> (mem + bytesAlreadyAllocated)

applies an offset using a type of undefined size ( void ). 使用未定义大小( void )类型应用偏移量。 since void has no size, the program is ill formed. 由于void没有大小,程序生成错误。

char* is a suitable pointer for your usage in this scenario. char*是适合您在此方案中使用的指针。 for example: 例如:

`char* mem;`
 // and
 char* addr(mem + bytesAlreadyAllocated);

Update 更新

So in the following program: 所以在以下程序中:

#include <iostream>

int main(int argc, const char* argv[]) {
    const int array[3] = {-1, 0, 1};
    std::cout << *(array + 0) << ", "
      << *(array + 1) << ", " << *(array + 2) << "\n";
    return 0;
}

The output is -1, 0, 1 . 输出为-1, 0, 1 The element offsets are applied based on the size and type of the array elements. 根据数组元素的大小和类型应用元素偏移。 With void -- size is not proper. void - 大小不合适。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不能伪造来自 c++ 的托管回调(不能通过 static_cast 从 'void (GenFlowcacheTests::*)(int)' 转换为 'void(*)(int)') - cannot fake a managed callback from c++ (cannot cast from 'void (GenFlowcacheTests::*)(int)' to 'void(*)(int)' via static_cast) 错误 C2440:“static_cast”:无法从“void (__thiscall Visualizza::*)(char [])”转换为“AFX_PMSG” - error C2440: 'static_cast' : cannot convert from 'void (__thiscall Visualizza::* )(char [])' to 'AFX_PMSG' C ++ static_cast从float **到void ** - C++ static_cast from float** to void** C ++ static_cast <void *> - C++ static_cast<void *> C ++ std :: forward <T> vs static_cast <T> - C++ std::forward<T> vs static_cast<T> 使用C ++样式转换从Void *转换为TYPE *:static_cast或reinterpret_cast - Cast from Void* to TYPE* using C++ style cast: static_cast or reinterpret_cast C ++:不能从double *到int *的static_cast - C++: can't static_cast from double* to int* 无法将参数从“ SIZE_T *”转换为“ size_t *”-如何进行投射? - cannot convert parameter from 'SIZE_T *' to 'size_t *' - how to cast? static_cast &lt;&gt;中的C ++编译错误 - C++ compilation error in static_cast< > 为什么使用implicit_cast<size_t> (int) 而不是 static_cast<size_t> (内部)? - Why uses implicit_cast<size_t>(int) instead of static_cast<size_t>(int)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM