블로그 이미지
Leeway is... the freedom that someone has to take the action they want to or to change their plans.
maetel

Notice

Recent Post

Recent Comment

Recent Trackback

Archive

calendar

1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
  • total
  • today
  • yesterday

Category

1. swapping
지난 (주어진 데이터 - 수열 - 에서 최대값과 최소값을 구하는) 숙제로 assignment에 대해 완전히 이해했다고 생각했었는데 아니었다. ( ..)^
Swapping two data elements
int a, b, c;
a=10; b=20;
How can you exchange the values of the two variables?
이 문제 정말 헤맸음. int c가 선언되어 있어서 힌트가 되었다.


2. address
중간에 방을 세 개나 비워 둔다... 왜 그럴까...? >> ㅎㅎ 비워 두는 거 아니다.
number[]와 number는 다른 거 아니었어??? > 아니다. 같은 거다.
max와 min을 먼저 만드는 것을 보면 memory는 OS가 main() 함수를 호출하기 이전에 (즉, 프로그램이 실행되기 이전에) 마련되는 것인가? > 아니구나. main() 함수에서 선언된 순서대로인 것이 맞다.

(관련 있는지 모르겠지만,)http://cplusplus.com/doc/tutorial/pointers.html
> 관련 없다.

number+i를 number로 바꾸면,
3221223080 0xbffff6a8 10
3221223080 0xbffff6a8 5
3221223080 0xbffff6a8 30
3221223080 0xbffff6a8 93
3221223080 0xbffff6a8 0
3221223080 0xbffff6a8 84
3221223080 0xbffff6a8 65
3221223080 0xbffff6a8 3
3221223080 0xbffff6a8 78
3221223080 0xbffff6a8 67
3221223080 0xbffff6a8 45
3221223080 0xbffff6a8 12
3221223080 0xbffff6a8 28
3221223080 0xbffff6a8 75
3221223080 0xbffff6a8 111
3221223080 0xbffff6a8 3
3221223080 0xbffff6a8 845
3221223080 0xbffff6a8 41
3221223080 0xbffff6a8 343
3221223080 0xbffff6a8 43
address=3221223080 (bffff6a8) = 10
address=3221223080 (bffff6a8) = 5
address=3221223080 (bffff6a8) = 30
address=3221223080 (bffff6a8) = 93
address=3221223080 (bffff6a8) = 0
address=3221223080 (bffff6a8) = 84
address=3221223080 (bffff6a8) = 65
address=3221223080 (bffff6a8) = 3
address=3221223080 (bffff6a8) = 78
address=3221223080 (bffff6a8) = 67
address=3221223080 (bffff6a8) = 45
address=3221223080 (bffff6a8) = 12
address=3221223080 (bffff6a8) = 28
address=3221223080 (bffff6a8) = 75
address=3221223080 (bffff6a8) = 111
address=3221223080 (bffff6a8) = 3
address=3221223080 (bffff6a8) = 845
address=3221223080 (bffff6a8) = 41
address=3221223080 (bffff6a8) = 343
address=3221223080 (bffff6a8) = 43
Their sum is 1981
Their max is 845
Their min is 0
Address of the variable max is 0xbffff6a0
Address of the variable min is 0xbffff6a4

sum의 address를 첨가하면,
Address of the variable sum is 0xbffff69c
그렇다면, int sum이 가장 먼저 생성된다.

쉘에서 다시 실행시키면,
3221223816 0xbffff988 10
3221223816 0xbffff988 5
3221223816 0xbffff988 30
3221223816 0xbffff988 93
3221223816 0xbffff988 0
3221223816 0xbffff988 84
3221223816 0xbffff988 65
3221223816 0xbffff988 3
3221223816 0xbffff988 78
3221223816 0xbffff988 67
3221223816 0xbffff988 45
3221223816 0xbffff988 12
3221223816 0xbffff988 28
3221223816 0xbffff988 75
3221223816 0xbffff988 111
3221223816 0xbffff988 3
3221223816 0xbffff988 845
3221223816 0xbffff988 41
3221223816 0xbffff988 343
3221223816 0xbffff988 43
address=3221223816 (bffff988) = 10
address=3221223816 (bffff988) = 5
address=3221223816 (bffff988) = 30
address=3221223816 (bffff988) = 93
address=3221223816 (bffff988) = 0
address=3221223816 (bffff988) = 84
address=3221223816 (bffff988) = 65
address=3221223816 (bffff988) = 3
address=3221223816 (bffff988) = 78
address=3221223816 (bffff988) = 67
address=3221223816 (bffff988) = 45
address=3221223816 (bffff988) = 12
address=3221223816 (bffff988) = 28
address=3221223816 (bffff988) = 75
address=3221223816 (bffff988) = 111
address=3221223816 (bffff988) = 3
address=3221223816 (bffff988) = 845
address=3221223816 (bffff988) = 41
address=3221223816 (bffff988) = 343
address=3221223816 (bffff988) = 43
Their sum is 1981
Their max is 845
Their min is 0
Address of the variable max is 0xbffff980
Address of the variable min is 0xbffff984
Address of the variable sum is 0xbffff97c

아항~ memory의 주소는 compile할 때 할당되고, update를 하더라도 기존 주소를 그대로 찾아간다. 맞나??? > 틀리다. 그냥 그렇게 보였을 뿐. (메모리를 달리 사용하고 있지 않았기 때문이었다.)
여하간, 도대체 왜! sum, max, min이 먼저 만들어지고 나서 number[]가 만들어지는 것이야??? > 변수들의 형태에 따라서 분류하여 그 순서대로 만들기 때문이란다.

뜬금없이 제일 마지막에 int why 변수를 하나 첨가해 보았더니, (Xcode 실행창)
3221223084 0xbffff6ac 10
3221223084 0xbffff6ac 5
3221223084 0xbffff6ac 30
3221223084 0xbffff6ac 93
3221223084 0xbffff6ac 0
3221223084 0xbffff6ac 84
3221223084 0xbffff6ac 65
3221223084 0xbffff6ac 3
3221223084 0xbffff6ac 78
3221223084 0xbffff6ac 67
3221223084 0xbffff6ac 45
3221223084 0xbffff6ac 12
3221223084 0xbffff6ac 28
3221223084 0xbffff6ac 75
3221223084 0xbffff6ac 111
3221223084 0xbffff6ac 3
3221223084 0xbffff6ac 845
3221223084 0xbffff6ac 41
3221223084 0xbffff6ac 343
3221223084 0xbffff6ac 43
address=3221223084 (bffff6ac) = 10
address=3221223084 (bffff6ac) = 5
address=3221223084 (bffff6ac) = 30
address=3221223084 (bffff6ac) = 93
address=3221223084 (bffff6ac) = 0
address=3221223084 (bffff6ac) = 84
address=3221223084 (bffff6ac) = 65
address=3221223084 (bffff6ac) = 3
address=3221223084 (bffff6ac) = 78
address=3221223084 (bffff6ac) = 67
address=3221223084 (bffff6ac) = 45
address=3221223084 (bffff6ac) = 12
address=3221223084 (bffff6ac) = 28
address=3221223084 (bffff6ac) = 75
address=3221223084 (bffff6ac) = 111
address=3221223084 (bffff6ac) = 3
address=3221223084 (bffff6ac) = 845
address=3221223084 (bffff6ac) = 41
address=3221223084 (bffff6ac) = 343
address=3221223084 (bffff6ac) = 43
Their sum is 1981
Their max is 845
Their min is 0
Address of the variable max is 0xbffff6a0
Address of the variable min is 0xbffff6a4
Address of the variable sum is 0xbffff69c
Address of the variable why is 0xbffff6a8

헉!!! 주소 바꾼다... 도대체 뭣이야... 점점 더 이해 안 가. ㅜㅜ
어쨌든, update 시 변경 사항이 있으면 compile 후 주소를 바꿔치기도 하는 등 각 변수에게 고정된 주소를 주는 것이 아니구나.


서 교수님:
컴파일러가 C 소스코드를 분석해서 기계어로 바꾸는데, 소스코드 전체를 들여다 보면 변수들이 여럿 나오겠지요. 단일 변수들은 단일 변수들로 배치할 것이고 어레이로 선언된 것들은 따로 배치해서 메모리를 할당하게 되어 있을 겁니다. 물론 어떤 컴파일러는 그렇지 않고 다른 방법을 사용할 수도 있습니다. 그래서,  number의 어드레스가 다른 변수들과 달리 나중의 주소를 가지게 되는 것으로 판단됩니다.

number[10] 이라고 배열 선언을 하면 컴파일러는 10개의 연속된 데이터를 위한 공간을 확보하고 그 첫번째 공간의 주소를 number 에 넣습니다. 그래서 number 는 number[0] 의 주소를 가지는 것입니다.

number[1] 은 배열에서  number[0] 다음의 값을 가지는데 그 공간의 주소값을 알고 싶으면 &number[1] 이라고 하든지 number+1  이라고 하면 됩니다.

꺼꾸로, number 에서 시작하여 두 번째 즉 number[1] 의 값을 주소로부터 얻고 싶으면 number[1] 이라고  하든지 *(number+1) 로 하여 얻을 수 있습니다.

서 교수님:
각 변수들의 실제 메모리 주소 (physical address) 는 프로그램이 실행되는 시점에서 결정됩니다. 적당한 메모리 위치에 OS 가 실행프로그램을 통째로 카피하고, CPU가 main() 의 첫 부분부터 실행하도록 합니다.  다른 프로그램들이 자주 실행되고 끝나는 상황이라면 변수들의 주소가 거의 매번 다르게 보일 것입니다. 그래서, 실제 주소를 지정하는 방법은 거의 없습니다.

서 교수님:
변수들이 몇 개가 있는지 어떤 형태인지를 컴파일러가 모두 조사한 후에 각 변수들에 필요한 메모리를 미리 할당해 둡니다.  이때는 상대적인 메모리 주소를 사용하죠. 소스코드에서는 변수들의 선언 순서가 중요합니다. 그러나, 실제로 실행되는 상황에서는 이미 필요한 모든 변수들이 메모리를 차지하고 있는 상황이 됩니다.

서 교수님:
C/C++ 프로그래밍은 컴퓨터 구조에 대해서 약간 알고 있으면 훨씬 더 쉽게 접근할 수 있습니다.



3.
교수님 강조:
문제의 풀이를 순차적인 언어로 표현할 수 있으면, 그것을 프로그래밍 언어로 바꿀 수 있다.



4.
warning: control reaches end of non-void function

무슨 뜻인가 했더니 >
It means that you have to return a value using the return statement before the end of your function. This does not apply to void functions because a void function does not return a value.
-> omission으로 함수의 return 값을 지정해 주지 않았을 때 생기는 경고였다.



5.
int *address;

int printf ( const char * format, ... );
%[flags][width][.precision][length]specifier

*
The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.
> 난 이거 아직 이해 못 했다.



6.
address = &max;

&
2) A symbol used to precede a variable name (as in &x). Means the address of the named variable (address of x). Used to assign a value to a pointer variable.


 


posted by maetel