'What the difference between (static const int array)、(const int array) and ( int array ) in memory

test_demo.cpp

#define LEN 1024*1024*10
static const int array[LEN] = { 0 }; 

static void* test_memory(void *pArg){

    const char *pThreadName = "mem_test";
    prctl(PR_SET_NAME, (unsigned long)pThreadName, 0, 0, 0);

    int count = 0;
    for(int i = 0; i < LEN; i +=16){
        count = array[i];
    }
    printf("test thread, get count=%d \n", count);
    getchar();
    
    return NULL;
}

int main(){
    printf("Welcome to array example:%d\n", getpid());
    getchar();

    pthread_t memId;
    int status = pthread_create(&memId, NULL,test_memory, NULL);
    if(status != 0){
        printf("create thread failed,error=%d \n",status);
    }

    pthread_join(memId, NULL);
    printf("main thread\n");
    getchar();

    return 0;
}
gcc test_demo.cpp -lpthread -o test_demo
./test_demo

I cat /proc/pid/smaps when the code run to getChar().So I have three smaps info. What confused me is that the smaps info is difference by how I declare the Array. Like this:

1、static const int array[LEN] = { 0 };
the smaps info shows that the PSS and RSS of test_demo increase about 40M
2、const int array[LEN] = { 0 };
the smaps info shows that the PSS and RSS of test_demo increase about 40M, and a block of memory is mapped for libc.so, meantime the size of the block of memory is 2048KB, PSS is 8KB.
3、int array[LEN] = { 0 };
only a block of memory is mapped for libc.so,meantime the size of the block of memory is 2048KB, PSS is 8KB.

I do not understand why this happened.Could someone tell me?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source