[英]I executed the following code and my system stuck, but I am not able to figure out reason behind it?
#include<stdio.h>
int main(){
while(1){
int *a = new int[1000];
}
}
How the heap is growing here and how the operating system handles it?堆在这里如何增长以及操作系统如何处理它? I do not understand how memory management is handled here.
我不明白这里是如何处理 memory 管理的。 I thought that the OS allocates a fixed-size stack and heap?
我以为操作系统分配了一个固定大小的堆栈和堆? What is the max amount of heap and stack allocated to the program and how?
分配给程序的最大堆和堆栈数量是多少?如何分配?
Int *a = new int[1000] creates an array in new location of memory, and sets pointer to first element of array into variable a. int *a = new int[1000] 在 memory 的新位置创建一个数组,并将指向数组第一个元素的指针设置为变量 a。 (Variable a is an pointer).
(变量 a 是一个指针)。 You won't get this problem in java and other languages because they have garbage collector, that delete old array, c++ don't have it, so you need to use delete keyword.
在 java 和其他语言中你不会遇到这个问题,因为它们有垃圾收集器,删除旧数组,c++ 没有它,所以你需要使用 delete 关键字。 (Deleting variables and arrays have little bit different syntaxis. Your loop will run until you'r pc run out of ram, read about memory leak in c++
(删除变量和 arrays 的语法略有不同。您的循环将一直运行,直到您的 pc 用完 ram,请阅读 c++ 中的 memory 泄漏
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.