开发者

can the compiler optimize the memory management allocating a static array on the heap?

开发者 https://www.devze.com 2023-03-23 20:17 出处:网络
I wanted to allocate a big char array in the data se开发者_开发问答gment so I used some code like this one:

I wanted to allocate a big char array in the data se开发者_开发问答gment so I used some code like this one:

const int size = 1000000000 ;
static char chr [ size ] ;

int main ( ) 
{ 
    chr [ size - 1 ] = 1 ;  // line 1 
    string s ;
    cin >> s ;              // line 2
} 

line 1 : I put this line so that the array is used at least once and it's not optimized-out from the compiler

line 2 : in order to stop the execution and check the memory occupation eg: in the windows' task manager

on a windows system the result is that when the program is stuck on line 2 waiting for the user's input, on task manager (in both columns of Memory and Working Set) the amount of memory used by the process is way less than the expected 1GB.

I then tried with the following code:

    int main ( ) 
    { 
        for ( int i = 0 ; i < size ; ++ i )
        {
            chr [ i ] = i ;
        }
        string s ;
        cin >> s ; // line 2
     } 

Now, when the program reaches line 2 the memory use reaches the expected 1GB after a few seconds of quick growth.

It seems like the memory is dynamically allocated instead of statically.

Is my understanding of arrays / memory model wrong ?

Does the compiler allocate big amounts of data dynamically in order to optimize ?

Does the task manager show the physically allocated memory and so the 1GB is initially allocated on the hard drive till the first use ?


The operating system allocates memory only after it has been accessed. Read about demand paging.


You are allocating virtual memory.

If you don't actually use it, it doesn't have to be stored anywhere.


Since you don't actually use the array (removing it will have not impact on the output of your program, the compiler is free to optimize it away. And some systems do a form of lazy allocation: they don't allocate (and charge your process for) the individual pages until you actually access them; even without optimization, you only touch a single page. (Arguably, you can't have a conformant implementation of C or C++ on a system which does lazy allocation. But since both Windows and Linux are guilty, we have to live with it.)


Does the compiler allocate big amounts of data dynamically in order to optimize ?
No it does'nt!

An Compiler will never do an optimization like that.
Only explicitly dynamically allocated memory will go on freestore(or heap)

When you do

const int size = 1000000000 ;
static char chr [ size ] ;

at a global scope, the memory that is used not even stack memory, the memory in use is the Data segment or the BSS segment however, the C++ standard does not specify as to where it should be allocated, it is left as an internal implementation of the compilers but it is definitely not allocated on freestore(a.k.a heap)

Also, Do not use Task Manager to profile the memory usage of your C++ programs, specifically use Profiling tools to do so, What task manager shows you is not true w.r.t program.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号