简体   繁体   中英

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. 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. (Variable a is an pointer). 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. (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++

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM