test_demo.cpp < /p>
#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;
}
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;
}
< /code>
gcc test_demo.cpp -lpthread -o test_demo
./test_demo
< /code>
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 };< /code>
the smaps info shows that the PSS and RSS of test_demo increase about 40M
2、const int array[LEN] = { 0 };< /code>
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 };< /code>
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?
Подробнее здесь: https://stackoverflow.com/questions/719 ... ay-and-int
Какова разница между (статический массив Const int) 、 (Const int Array) и (int массив) в памяти ⇐ C++
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение