๋ชฉ๋ก์ ์ฒด ๊ธ (35)
๐ก

12.14 ๋ฉ๋ชจ๋ฆฌ ๋์์ free()์ ์ค์์ฑ - ๋์ ํ ๋น์ ํฌ์ธํฐ๋ง ์์ด๋ฒ๋ฆฌ์ง ์๊ณ ์ ๊ด๋ฆฌํ๋ฉด ์คํ๋ ค ๋ธ๋ก์ ๋๋๋ค๋ฉด์ ์ ์ฐํ๊ฒ ์ฌ์ฉํ ์ ์๋ค - ํ์ง๋ง ๋์ ํ ๋น์ผ๋ก ๋ฐ์ ๋ฉ๋ชจ๋ฆฌ์ฃผ์๊ฐ์ ๋์ ํ ํฌ์ธํฐ๋ฅผ ์์ด๋ฒ๋ฆฌ๋ฉด, ๋ค์ ์ฌ์ฉํ๊ฑฐ๋ ์ง์ธ ์ ์๋ค -Diagnostic Tools์ ํตํด Heap ์์ญ์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ ์ ์๋ค #define _CRT_SECURE_NO_WARNINGS #include #include int main() { printf("Dummy Output\n"); int* ptr_backup = NULL;//block scope ๋ฐ์์ ์ ์ธ { int n = 100000000; int* ptr = (int*)malloc(n * sizeof(int)); if (!ptr)// if(ptr ..

12.6 ๋ ์ง์คํฐ ๋ณ์ Register #define _CRT_SECURE_NO_WARNINGS #include void temp(register int r) { // do something with r } int main() { register int r; r = 123; //printf("%p\n", &r);//Error ๋ ์ง์คํฐ ๋ณ์์ ์ฃผ์๋ฅผ ๊ฐ์ ธ์ฌ ์ ์์ //int* ptr = &r;//Error ๋ ์ง์คํฐ ๋ณ์์ ์ฃผ์๋ฅผ ๊ฐ์ ธ์ฌ ์ ์์ return 0; } 12. 7 ๋ธ๋ก ์์ญ์ ์ ์ (static) ๋ณ์ -Static ๋ณ์๋ ์ ์ญ๋ณ์ ์ฒ๋ผ ํ์ผ ์ ๋ชจ๋ ๊ณณ์์ ์ฌ์ฉํ ์ ์๋ค๊ณ ์๊ฐํ๋๋ฐ ๋ธ๋ก ์์์ ํค์๋ static์ ๋ถ์ฌ ์ฌ์ฉํ๋ ๋ณ์๋ ์๊ธฐ ๋๋ฌธ์ ์ฃผ์ํด์ผํ๋ค. ์ด๊ฒ์ "๋ธ๋ก ์์ญ์ ์ ์ ..

11.10 ๋ช ๋ น์ค ์ธ์ Command-Line Arguments -๋ช ๋ น์ค ์ธ์๋ ํ๋ก๊ทธ๋จ ์คํ ์์ argument๋ก ๋ฃ์ด์ค ์ ์๋ค. #define _CRT_SECURE_NO_WARNINGS #include int main(int argc, char * argv[]) { int count; printf("The command line has %d arguments:\n", argc); for (count = 0; count < argc; count++) printf("Arg %d : %s\n", count, argv[count]); printf("\n"); return 0; } ๋ช ๋ น์ค ์ธ์๋ก์ ๋ฌธ์์ด์ ๋ฐฐ์ด์ argv[]์ ๋ฐ์๋ค์ด๊ณ ๋ฐฐ์ด์ ๊ฐ์๋ฅผ argc๋ก ๋ฐํํ๋ ์ฝ๋ 11.11 ๋ฌธ์์ด์ ์ซ์๋ก ๋ฐ..
11.3 ๋ฌธ์์ด์ ๋ฐฐ์ด #define _CRT_SECURE_NO_WARNINGS #include int main() { const char* mythings[5] = {//ํฌ์ธํฐ๋ฐฐ์ด "Dancing in the rain", "Counting apples", "Watching movies with friends", "Writing sad letters", "Studying the C language" }; char yourthings[5][40] = {//2์ฐจ์๋ฐฐ์ด "Studying the C++ language", "Eating", "Watcing Netflix", "Walking around till dark", "Deleting spam emails" }; const char* temp1 = "Dan..

10.12 ํฌ์ธํฐ์ ๋ํ ํฌ์ธํฐ(2์ค ํฌ์ธํฐ)์ ์๋ ์๋ฆฌ ํฌ์ธํฐ๋ฅผ ๋ฐฐ์ด์ฒ๋ผ ์ฌ์ฉ(ํฐ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ์ ๊ทผํด์ ์ฌ์ฉ)ํ๋ค๊ณ ํ๋ฉด ์ด์คํฌ์ธํฐ๋ ๋ฐฐ์ด์ ๋ฐฐ์ด์ฒ๋ผ ์ฌ์ฉํ๋ค ์ง๊ธ์ ๊ทธ๋ฅ ๊ทธ๋ ๊ตฌ๋ ํ๊ณ ๋์ด๊ฐ๋ฉด ๋๋ค๊ณ ํ๋ค 10.13 ํฌ์ธํฐ์ ๋ฐฐ์ด๊ณผ 2์ฐจ์ ๋ฐฐ์ด #define _CRT_SECURE_NO_WARNINGS #include int main() { /* Two of 1D arrays */ int arr0[3] = { 1, 2, 3 }; int arr1[3] = { 1, 2, 3 }; int* parr[2] = { arr0, arr1 }; for (int j = 0; j < 2; j++) { for (int i = 0; i < 3; i++) printf("%d(==%d, %d, %d) ", parr[j][i..
10.1 ๋ฐฐ์ด๊ณผ ๋ฉ๋ชจ๋ฆฌ -&arr[0] = &(arr[0])์ธ ์ด์ ๋ []๊ฐ &๋ณด๋ค ์ฐ์ฐ์ ์ฐ์ ์์๊ฐ ๋๊ธฐ ๋๋ฌธ์ด๋ค -๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฝ์๋ ์๋ฃํ์ ํฌ๊ธฐ๋งํผ ๊ฑด๋์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฐพ๋๋ค -์ธ๋ฑ์ค๊ฐ 0๋ถํฐ ์์ํ๋ ์ด์ ๋ ์ธ๋ฑ์ค ์ซ์๋ ์ฒซ๋ฒ์งธ ์ฃผ์๋ก๋ถํฐ์ ๊ฑฐ๋ฆฌ๋ฅผ ์๋ฏธํ๊ธฐ ๋๋ฌธ์ด๋ค ๋ฐ๋ผ์ ์ฒซ๋ฒ์งธ ์ธ๋ฑ์ค๋ ์ฒซ๋ฒ์งธ ์ฃผ์ ๊ทธ ์์ฒด(๊ฑฐ๋ฆฌ๊ฐ 0)์ด๊ธฐ ๋๋ฌธ์ 0์ด ๋๋ค ex) arr = &arr[0] 10.2 ๋ฐฐ์ด์ ๊ธฐ๋ณธ์ ์ธ ์ฌ์ฉ๋ฐฉ๋ฒ #define _CRT_SECURE_NO_WARNINGS #include #define MONTH 12 int main() { /* Basic usage */ int high[MONTH] = { 2, 5, 11, 18, 23, 27, 29, 30, 26, 20, 12, 4 }; for (int i ..
9.10 ํผ๋ณด๋์น ์์ ์ ์ฌ๊ท ํธ์ถ์ ์ฅ๋จ์ #define _CRT_SECURE_NO_WARNINGS #include int fibonacci(int number); int main() { for (int count = 1; count < 13; ++count) printf("%d ", fibonacci(count)); return 0; } int fibonacci(int number) { if (number == 1 || number == 2) return 1; return fibonacci(number-1) + fibonacci(number-2); } ์ฌ๊ทํธ์ถ์ ์ฌ์ฉํ๋ฉด ๊ตฌํ์ ๊ฐ๊ฒฐํ๊ฒ ํ ์ ์๋ค. ๋์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๋ง์ด ์ฌ์ฉํ๊ณ , ๊ณ์ฐ์ด ์ค๋ณต๋๋ค 9.11 ํค๋ ํ์ผ ๋ง๋๋ ๋ฐฉ๋ฒ ํ๋ก์ ํธ๋ช ์ ๋ง์ฐ์ค ์ฐ..
9.1 ํจ์๊ฐ ํ์ํ ๋ -์ผ๋ฐํ(๋ค์ํ ๊ฒฝ์ฐ์ ์ฑ๋ฆฝํ ์ ์๋๋ก) -๊ฐ์ ๊ธฐ๋ฅ์ด ์ฌ๋ฌ๋ฒ ๋์ฌ ๋ -์ง์์ ์ผ๋ก ์ ์ง๋ณด์๊ฐ ํ์ํ๋ ค๋ฉด #define _CRT_SECURE_NO_WARNINGS #include #include //strlen() #include #define WIDTH 30 #define NAME "Na-Da Ga" #define ADDRESS "Seoul, Korea" void print_chars(char c, int n_stars, bool endl) { for (int i = 0; i < n_stars; i++) printf("%c",c); if (endl == true) printf("\n"); } void print_centered_str(char str[]) { int n_bla..