可能运算符new的大小参数溢出了吗? [英] May the size argument of operator new overflow?
问题描述
你好!
C ++标准是否定义当void *
operator new(size_t size)的size参数不能代表总数时会发生什么分配的字节数是多少?b $ b例如:
struct S
{
char a [64];
};
S * allocate(int size)
{
返回新S [size]; //这里发生了什么?
}
int main()
{
allocate( 0x7FFFFFFF);
}
Hello!
Does the C++ standard define what happens when the size argument of void*
operator new(size_t size) cannot represent the total number of bytes to be
allocated? For example:
struct S
{
char a[64];
};
S* allocate(int size)
{
return new S[size]; // What happens here?
}
int main()
{
allocate(0x7FFFFFFF);
}
推荐答案
Angel Tsankov写道:
Angel Tsankov wrote:
你好!
C ++标准是否定义当void *
operator new(size_t size)的size参数不能代表总数时会发生什么分配的字节数是多少?b $ b例如:
Hello!
Does the C++ standard define what happens when the size argument of void*
operator new(size_t size) cannot represent the total number of bytes to be
allocated? For example:
size_t总是足够宽,以表示给定系统上的最大内存范围
。
如果系统无法提供所需的大小,则新抛出std :: bad_alloc。
-
Ian Collins。
size_t will always be wide enough to represent the maximum memory range
on a given system.
If the system can''t supply the requested size, new throws std::bad_alloc.
--
Ian Collins.
6月18日,5:44 * am,Angel Tsankov < fn42 ... @fmi.uni-sofia.bgwrote:
On Jun 18, 5:44*am, "Angel Tsankov" <fn42...@fmi.uni-sofia.bgwrote:
你好!
C ++标准是否定义当void *
运算符new(size_t size)的size参数无法表示要分配的
的总字节数时会发生什么?例如:
Hello!
Does the C++ standard define what happens when the size argument of void*
operator new(size_t size) cannot represent the total number of bytes to be
allocated? For example:
是。您不能超过numeric_limits< size_t> :: max()。对于数组大小,同样是
。
Yes. You cannot exceed numeric_limits<size_t>::max(). The same is
true for array size.
你好!
>
C ++标准是否定义当void *
operator new的size参数时会发生什么(size_t size)不能表示要分配的总字节数是多少?例如:
>
Does the C++ standard define what happens when the size argument of void*
operator new(size_t size) cannot represent the total number of bytes to be
allocated? For example:
是。您不能超过numeric_limits< size_t> :: max()。对于数组大小,同样是
。
好的,但是你已经切断的例子会发生什么?
Yes. You cannot exceed numeric_limits<size_t>::max(). The same is
true for array size.
OK, but what happens in the example that you have cut off?
这篇关于可能运算符new的大小参数溢出了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!