问题描述
当我们动态分配内存时,说100个整数,说int *x = new int(1000);
那么语句完成后是否会立即分配全部内存?
我的意思是页面错误的概念会在这里出现吗?
When we dynamically allocate the memory say 100 integers sayint *x = new int(1000);
then does entire chunk of memory gets allocated at once after the completion of the statement?
I mean will the the concept of page fault come into picture over here?
推荐答案
当我们动态分配内存时,说100个整数说
When we dynamically allocate the memory say 100 integers say
int *x = new int(1000);
当我们要分配100
整数时,应使用正确的语法.
您的代码段只是为1
(是,一个)整数分配内存,然后使用值1000
初始化.正确的语句(用于分配100
整数)是:
When we want to allocate 100
integers, we should use the correct syntax.
Your code snippet just allocates memory for 1
(yes, one) integer and then initializes it with the value 1000
. The correct statement (for allocating 100
integers) is:
int * x = new int[100];
成功时,它会(立即)为100
整数分配连续的内存,失败时,会抛出异常,如 [ ^ ].
on success, it allocates (at once) contiguous memory for 100
integers, on failure an exception is thrown as specified in documentation of new operator[^].
这篇关于内存分配查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!