I'm creating a self initializing arrays class in C++ and I'm wondering how I'd throw an error not an exception if a user were to try and allocate more than 0x7fffffff bytes.
Similar to <array> where:
error C2148: total size of array must not exceed 0x7fffffff bytes
This is my code for one of my constructor where I'm testing this:
template<typename T> Array<T>::Array(const size_t _SIZE) : _SIZE(_SIZE), _content(nullptr){ #define __SIZE__ _SIZE #if (__SIZE__ > 0x7fffffff) #error Total size of Array must not exceed 0x7fffffff bytes. #endif _content = new T[_SIZE]; memset(_content, 0, (sizeof(_content) * _SIZE)); } The way that I'm creating the array is below:
Array<int> foo(-1) //-1 of size_t = ((2^31)*2)-1 error should be shown since ((2^31)*2)-1 > ((2^31)*2)-1 size_t's max size is ((2^31)*2)-1 and 0x7fffffff is (231)-1 now the issue is that the error isn't executing I've never used the #if macro before and I need to get this to work...
Any help would be appreciated.
sizeof(*_content). However, itTis not a primitive type, thenmemsetshould not be used at all, since it can to bad things to objects data, including the virtual table if the class have virtual function.std::vector? You can set both the size and the initial data when constructing astd::vector.