【发布时间】:2014-03-04 21:13:50
【问题描述】:
我知道max_size() 来自std::allocator 是一个理论上的限制,但我只是尝试了一些东西,我得到的数字非常巨大;那么我应该如何解释这个数字以及这个函数背后的理念是什么?
我的例子:
#include <iostream>
#include <memory>
#include <cstdint>
typedef int8_t Tnum_1;
struct X
{
template <typename T>
static void printAllocInfo()
{
std::cout << std::allocator<T>().max_size() << "\t Alloc max_size\n";
std::cout << ((std::allocator<T>().max_size()) / (1024))
<< "\t Alloc max_size / 1024\n";
std::cout << (((std::allocator<T>().max_size()) / (1024)) / (1024))
<< "\t\t Alloc max_size / 1024 / 1024\n";
}
};
int main()
{
X::printAllocInfo<Tnum_1>();
return (0);
}
打印出来:
18446744073709551615 Alloc max_size
18014398509481983 Alloc max_size / 1024
17592186044415 Alloc max_size / 1024 / 1024
考虑到我只是使用int8_t 作为模板参数,这意味着我可以在我的机器上分配18446744073709551615 bytes 吗?这个数字太大了,以至于它开始失去任何实际意义。
【问题讨论】:
标签: c++ memory-management c++11