HW - Lecture 8
12191789 유호현
1-1.
Draw the memory map (process image) of the following program (ex1.cpp). What are the starting addresses of the code, data, heap, stack segment of this program and how many pages each segment occupies? What is the address of main function, the addresses of the global variables and local variables?
#include <stdio.h>
int x;
int y[10000];
int main(){
int k;
int *pk;
pk=new int;
printf(“ex1. &main:%p &x:%p &y:%p &y[9999]:%p &k:%p &pk:%p pk:%p\\n”,
main,&x,&y,&y[9999],&k,&pk,pk);
for(;;); // to see memory map of this process
return 0;
}
- 주어진 코드와 동일하게 ex1.cpp 파일 작성
- 저장 후, g++로 컴파일 후 실행함.
- proceess image를 확인할 수 있음.
-
메모리 할당
- main 함수 → Code 영역
- global variables → Data 영역
- 동적 할당된 메모리 → Heap 영역
- local variables → Stack 영역
⇒ 위와 같이 메모리에 할당된 것을 확인할 수 있음.
-
&y[9999]는 global variables로 Data 영역에 있어야 하지만, 배열 y의 크기가 큰 이유로 Heap 영역까지 할당됨.
1-2.
Write another simple program, ex2.cpp (see below), and run ex2, ex1 at the same time. Confirm they have the same address for main function. How can they run at the same location at the same time?
#include <stdio.h>
int x1;
int main(){
int *pk1;
pk1 = new int;
printf(“ex2. &main:%p &x1:%p\\n”, main,&x1);
for(;;); // to see memory map of this process
return 0;
}