Title: ????? ??????? ???????? ?-C
1????? 2
- ????? ??????? ???????? ?-C
- ??? ???? ?-C
- I/O redirection
- ?????? ?????
- ???????
- ??????
- ????? ?? ?????? ???????
2????? ??????? ???????? ?-C
- ???? main ????? ??????? ???? ??? ???????
- argc - ???? ?????????? ????? ?????? (???? ??????
????) - argv - ????? ????? ?? ??????? ??? ?????? ??
?????????? ????? ?????? - ????? ?????? ??? echo ??? ????? ?? ??????????
????? ?????? ??? ????? ??? ?? ???? ?????? - ??????
- gt echo hello, world
- hello, world
- gt
- argv0 ????? ?? ??? ?? ??????? ??????. ??? argc
??? ????? 1. ?? argc ??? 1 ??? ????????? ???? ??
???????. ?????? ???? argc ??? 3 ? argv0,
argv1 ? argv2 ?? "echo", "hello," ? "world"
??????
3????? ??????? ???????? ?-C
- ??????? ?????? ?????? ???? ?- argv1 ???????
???? ?-argvargc-1. argvargc ???? NULL. - ??????? echo
- include ltstdio.hgt
- int main (int argc, char argv) / echo
command-line arguments / - int i
- for (i 1 i lt argc i)
- printf ("s ", argvi)
-
- printf ("\n")
- return 0
-
4???? ?? ??? ??? ?-C
- ???????? ???/??? ?????? ?????? stdio.h
- ????? ??? ??????? ??? ???? ???? include
ltstdio.hgt - ???? ?????? ???? ??? ???????, ??? ???? ????/???
?????? - ???? ?????? ?? ?????
- 1. ?????? ?? ????? ?????? ?? ??????
- 2. ??????/?????? ?-/?? ?????
- 3. ?????? ?? ?????
- ?? ??????? ?? ????? ????? ?"? ????? ?????? ?????
?FILE- (?????? FILE ????? ?-stdio.h)
5????? ?? ????? ?????
- ????? ?? ????? ????? (system resources)
- ????? ????? ????? ?????/????? ?? ??????
- ????? ?? ???? ????? ????? ????? ?? ???? ??????
??????? ????? ?????? ?? ?????. - ??? ?????? ????? ????? ???? ???????? ????? ????
- ?? ????? ?? ????? ????? ????? ?????? ????? ??
?????? ?????? ???????
6????? ?? ????? ?????
- ???? ?????? ????? ?? ????? ?????
- ??????/????? ????
- ??????? ?????
- ??????/??????? ????
- ?? ????? ????? ?????? ???? ?????? ??????
(resource leak) ????? ????? ??????? ??????. - ???? ????? ?????
- ????? ????? ????? ??? ??????
7????? ?? ????
- ????? ???? ????? ?"? ???????? fopen
- FILE fopen(const char filename, const char
mode) - filename - ?? ????? ??????
- mode - ???? ?? ?? ????? ?? ?????
- "r" - ??? ?????? ????
- ?? ????? ?? ????, ???? ????? (????? NULL)
- "w" - ??? ?????? ????
- ?? ????? ?? ????, ?????? ???? ???
- ?? ????? ????, ????? ??? ????? ?????? ????? ????
- "a" - ??? ?????? ???? ?????
- ?? ????? ?? ????, ?????? ???? ???
- ???????? ?????? ????? ?-FILE ?????? ?? ????? ??
?????? ?????? ????? ?????? NULL. - FILE fd fopen("hello.c" , "r")
8????? ?? ????
- ????? ???? ????? ?"? fopen ????? ?"? ????????
fclose - int fclose (FILE fd)
- fclose ?????? 0 ?? ?????? ?-EOF ?? ???? ?????.
- ??????
- FILE fd
- fd fopen("hello.c" , "r")
- if(fd NULL) error(Failed to open
hello.c!\n) - ... / Perform input on file /
- if (fclose(fd) EOF) printf (Failed to close
file!\n") - else printf ("File closed successfuly.\n")
- ????? ???? ?????? ????? ?????? ????? ????? ?? ??
?????? - ????? ???, ????? ???? ??? ????? ?? ?? ??????
????? ?????? ?? ???? main
9????? ??????
- if(fd NULL) error(Failed to open hello.c\n)
- if(fclose(fd) EOF) printf(Failed to close
file\n) - ?????? ????? ??? ???? ?? ?? ??????? ?????? ??????
?? ????? ??? ??? ??????. - ???? ????? ??? ??? ?? ????? ??? ????? ?????
- ??? ????? ?????? ?????? ???? ????
- ??? ????? ???? ?? ????? ?? ??? ????? ????????
10????? ??????
- ???? ??????? ???? ??? ????? "?????????" ??? ????
????? ?? ???? ?????? ???? ?????? - ????? ?????? ????
- ????? ??????? ???????? ????
- ????? ???? ???? ?? ???????? ?????? ?????? ???
- ????? ????? ??? ???? "????? ?????" (defensive
programming)
11???????? ???/???
- char fgets (char line, int maxline, FILE file)
- ????? ????, ???? '\n, ????? file ???? line. ???
????? maxline-1 ????? ????? - ?????? NULL ?????? ?????
- ???? ????? ?? ?? ???? ?? ??
- ?? ???? ?????
- ???? ?????? line
- ?????? \0 ???? ???????
- int fputs(char line, FILE file)
- ????? ?? line ?????
- ?????? EOF ?? ???? ?????, ???? ?? ????? ????
12????? ??????? copy
- ????? ??? ???????.
- ?????? ?? ????? ??? ??? ??? ?????? ??????, ?????
??? ??? ??? ?????? ????.
12
13?????? copy ???????? ???
- define CHUNK_LENGTH 100
- void copy(FILE input, FILE output)
- char bufferCHUNK_LENGTH
- while (fgets(buffer, CHUNK_LENGTH, input) !
NULL) - fputs(buffer, output)
-
-
- void error(char message)
- fprintf(stderr, "Error s\n", message)
- exit(1)
-
14??????? copy
- include ltstdio.hgt
-
- int main(int argc, char argv)
- if (argc!3) error("Bad number of parameters")
- FILE input fopen(argv1, "r")
- if (inputNULL) error("Can't open input file")
- FILE output fopen(argv2, "w")
- if (output NULL)
- fclose(input)
- error("Can't open output file")
-
-
- copy(input,output)
- fclose(input) fclose(output)
- return 0
- ????? ????? ???? ??? !
- ( ????? ??????, ?? ???? ?????? ??????)
- ???? statements ?????
- ???? ?? if ????? ???? ???? ??????? ????????
14
15?????? ?????????
- ?C- ?? ???????? ????? ??? ???? ???? ?????. ??
???? ??????? ????? ????? ????? ?"? ??????? ?-FILE - ???? ?????? C ?????? ????, ????? ?????? ?????
????? ????? ???????? ????? ?????? ????????? - stdin???? ?????? (??"? ???????)
- stdout ???? ?????? (??"? ?? ????)
- stderr ???? ?????? (??"? ?? ????)
- stdin , stdout ?-stderr ?? ????? ???? ?????? ????
???? ?????? ??????? ?-FILE. - ?????? ??????
- fclose (stdin)
- ????? ??? ??? ???? ???? ????? ??? ???????
- ???? ?? ?????? ????? ?????? ??????? ??????
?????? ?? EOF ?? ????? - Ctrl-d ?-Unix
- Ctr-z ?-Windows
16???????? ???/???
- char gets(char str)
- ????? ????, ?? ???? ,'\n????? ???????? ???? str
- ?????? \0 ???? ???????
- ??? ????? ??? ?? fgets
- ???? ?????? ?????? security, ???? ?????? ?-fgets
- int puts(char str)
- ????? str ?? ???? ????????
- ?????\n ???? str
- ??? ????? ??? ?? fputs
17????? ??????? copy ???????
- ????? ?????? ??? ???????, ?????? ?? ????? ??? ???
??? ?????? ??????, ????? ??? ??? ??? ?????? ???? - ????? ?????? ????? ????, ?????? ?? ????? ??? ???
??? ????? ?? ????? ???? ???????? - ????? ??? ?????? ??????? ???, ??????? ?????? ??
?? ?????? ????? ???? ???????? ????? ???? ????????
18??????? copy ???????
- include ltstdio.hgt
-
- int main(int argc, char argv)
- FILE input stdin FILE output
stdout - if (argcgt3) error("Bad number of parameters")
- if (argcgt1) input fopen(argv1, "r")
- if (input NULL) error("Can't open input
file") - if (argcgt2) output fopen(argv2, "w")
- if (outputNULL)
- fclose(input)
- error("Can't open output file")
-
-
- copy(input,output)
- fclose(input) fclose(output)
- return 0
19I/O redirection
- ??? ?????? ???? ??? Unix ???? ???? ???????? ?????
?? ??????, ?????? ???? ??????? ?????? ??????????
??????? ?? ???? ?????? ???? - ???? ????? ???? ?? ?? ???? ???? ???????? ??? ??
????? ???? ???????? ?????????? ?? ???? ?????
?????. ?????? ???? ???? redirection
20Input redirection
- ltprogramgt lt ltfilenamegt
- ????? ?-program ???? ?? ???? ???????? ??? ??????
filename - ??????
- copy lt filename
- ????? ?- copy ?????? ???? ?? ???? ?????? filename
- ?????? ????? ??"? ????? (?????? ??????) ????
??????? copy ?????? - copy filename
- ?? ?? ????? ?????? ??????? ?
21Output redirection
- ltprogramgt gt ltfilenamegt
- ????? ?-program ????? ?? ???? ???????? ??? ?????
filename. ?? ???? ??? ???? ??? filename ????
????? ??????, ?????? ?? ????? - cat myfile
- line 1
- line 2
- cat myfile gt out
- cat out
- line 1
- line 2
22Output redirection
- ltprogramgt gtgt ltfilenamegt
- ?????? ?? ???? ???????? ?? program ????? filename
?? ?????? ???? ???? ?????. ?? ?? ???? ???? ???
filename ???? ????? ??????, ?????? ?? ????? - cat yourfile
- line 3
- cat yourfile gtgt out
- cat out
- line 1
- line 2
- line 3
23Output redirection
- ????? ???? ?????? ????? ???? ??????? ??????
??????. ??????? (lt) ?? ????? ????, ??????? (ltlt)
?? ????? ???? ???? - ???? ?????? ???????? ??"? ?????? ??? ???? ?"?
????? ! ??????? ?????? - ltprogramgt gt! ltfilenamegt
- ????? ?? ????? filename ?????? ???? ?? ????
???????? ?? program ??? ???? - ?? ????? filename ???? ????, ??? ?????? ????
??????? ??? lt - ltprogramgt gtgt! ltfilenamegt
- ????? ?? ????? filename ?? ??? ?? ????.
- ?? ????? filename ????, ??? ?????? ???? ???????
??? ltlt
24Input and output redirection
- ???? ?????? ?? ?? ???? ???? ???????? ?? ???????
??? ?? ???? ???? - ?????? ???????
- cat lt in gt out
- cat lt in gt! out
- ?????? ?????? ???? in ????? out (?? ??????)
- cat lt in gtgt out
- cat lt in gtgt! out
- ?????? ?????? ???? in ???? ???? out (?? ??????)
25Multiple redirection
- ????? ???? ????? ????? ????? ?? ?????? ??????
?? ???????? ?????. - ???? ???? ????? ?????? ???? ???? ????? ?? ???????
???????? ?????????? ????? - gcc try_input.c -o try_input gt out_err
- try_input.c In function main
- try_input.c4 parse error before printf
- Error code 1
- more out_err
??????? ?????? ???? ! ???? ???? ??? !
26Multiple redirection
- ?????? ????? ??????? ???/??? ????????? ????
?????? ?? ?? ???? ??????? ???????? ????? ?? ????? - ltcommandgt gt ltfilegt
- ???? ?? ?? ???? ???????? ??? ?? ???? ???????
???????? ????? file - gcc try_input.c -o try_input gt out_err
-
- more out_err
- try_input.c In function main'
- try_input.c4 parse error before printf'
- Error code 1
27Multiple redirection
- ltcommandgt gt! ltfilegt
- ??? ?????? ?????? ??? ????? ?? ?? ????? file ????
- ltcommandgt gtgt ltfilegt
- ????? ??? ????? ?????, ??? ??? ????? ???? ??????
?? ??? ?????. ???? ?? ?????? ?- ! ?? ????? ??????
?? ?????? ????? - ltcommandgt gtgt! ltfilegt
- ??????
- (ltcommandgt gt ltf1gt) gt ltf2gt
- ????? ???? ???????? ?????? ?? ????? f1 ?????
??????? ?????? ????? ?? ???? f2
28Multiple redirection
gcc prog.c o prog prog This is error This is
stdout prog gt out.txt This is error cat
out.txt This is stdout prog gt errout.txt cat
errout.txt This is error This is stdout (prog gt
out) gt err cat out This is stdout cat
err This is error
- include ltstdio.hgt
- int main()
- fprintf(stderr, This is error\n)
- printf(This is stdout\n)
- return 0
29????? ?????? ?? ??????? ?-C
- ?????? ?-C ?????? ???? ??????
- ????? ???? int, char ?-double. ??????? ?????
??? ????? ?????? ?????? ???????? ???? ?? ???,
???? ?????? ???? ???? ?? ?? ??? ???, ?????
??????? ?????? ???? ?? ??? ??? - ???? ?????? ???????
- ?????? ???? ?????? ???????? ?????? 5 ???? ??????
- ?????? ????????
- ?????? ??????? ?? ????
- ?????? ????????
- ?????? ??????? ?? ???????
- ?????? ???????
30????? ?????? ?? ??????? ?-C
- ?????? ???????? - ?????? ??? ??????? ????? ??
??????? ????? ???? ????? ??? ????????? ??
???????. ??????? ?????? ????? ??????? ????
??????? ??????, ??????? ????? ?? ????? - ?????? ??????? ?? ???? - ?????? ??? ??????? ?????
?? ???????. ???? ???? ????? ??? ????????? ??
????? ?? ????? ??????. ??????? ?????? ?????
??????? ???? ??????? ??????, ??????? ????? ??
????? - ?????? ???????? - ?????? ??????? ?? ????????, ??
???????? ??? ?? ??????? ????? ???? ?????. ??????
??? ?????? ??? ??? ????????? ?????, ?????????
???? ???????? ??????? - ?????? ??????? ?? ??????? - ?????? ??????? ??
????????. ?????? ???? ?????? ?? ???? ??? ??????
????? ????????, ????????? ?? ????? ???????. ????
?? ???? ??? ?? ?? ????? ??? ????? ?? ???????
31????? ?????? ?? ??????? ?-C
- ???? ???? ??????? ???? ??? ??????? ??????? ?????
??? ??????? ?????????. ????? ???? ???? ???????
???? ???? ?????? ?? - ??????? ??? ?? ????
???????? ?????? ???? ??????. ?????? - ?????? ??? ?? ??????
- side-effects ?? ????????
- ??? ???? ????? ?? ???????? ???? ??? ?????
- ??? ????? ????? ???????? ?????? ?????? ??????
?? ????? ?????
32?????? ???????
- ????? ?? ?????? ???????? ??? ???????? ?????? ????
??????? ?????. ?????? ???? ?? ?????? ???????
?????? ??????? ?? ???? ????????? ?????. ??? ??
??, ?????? ???? ?????? ???? ???? ???? ?? ??
?????? ??? ????? ???? ???? ???? ????? ??????? ???
?? ???? ???? - ?????? ??????? - ?????? ?"? ??????? ???? ????.
?????? ???? ?? ??????? ????? ??????? ??????
????? (Heap) ???????? ?????? ????? ?? ???????
?????? ???? ???? ??? ????
33?????? ??? ???? ?????
- ??? ?????? ?? ????? ????? ?????
- ????? ??????
- ??????? ???????
- ??????? ?? ??????? ?????? ???? ??????
- ????? ??????? ?????? ?"? ??????? malloc
- ????? ??????? ????? ?"? ??????? free
- ?? ????? ????? ?????? ???? ??????? ?????? (memory
leaks) ???????? ????? ?????? - ???? ????? ?????
34???? ???????
- ?????? ????? ???? ?? ??????? ????? ???? ?? ????
(bytes) - ??? ??? ?? ????? ??????? ??
- ??????? ????? ?????? ???? ??? ?? ???? ???? ?????
??????
???? ??? ??? ???? ??? ???? char .
???? ??????
??? ?????
??? 0x200
'a' 0x201
? 0x202
6 0x203
0 0x204
0 0x205
0 0x206
? 0x207
???? ??????
c
?????? 4 ???? ???? ??? ???? int .
i
35???? ???????
- ?????? ????? ????? ???? ??????? ???? ?????
??????? - ??? ?????? ????? ??????? ?"? ??????? ?? ??????
??????? ?????????, ????????? ????????? - heap (?????) - ??? ?????? ?"? ?????? ??????
- ???? ???? ?????? ???? ?? ???? ?????? ?????? ?????
?"? ?????? malloc - ????? ?????? ??? ??? ?? ???? ???? ?"? ?????? free
- ????? ?????? ?????? ?? ?????? ?? ????? ???
??????? ??????. ???? ????? ????? ?? ?? ??????
?????? ???? ?????
36???????
- ????? ???? ?????
- ????? ????? ??? ????? ??????? ?????? ???? ????
?????? - lttype namegt ltvariable namegt
- ?????? ????? ?????? ?? ???? ??????? ????? ?????
?????? - lttype namegt
- int ptr
- ptr ??? ????? ??? ????? ?????? ?? ???? ???????
?????? int - ??????
- ???? ??????? ????? ?"?
- int x
- ?????? x ??? ?????? ??????? ?? ???? ????? ??
.x - ????, ?? x ??? ???? ??????? ?????? 2000 ??? ????
?? ?????? x ??? 2000
37???????
float x float ptr ptr x ptr5.8
- ????? ?????? ???? ?????
- ???? ?????? ????? ????? ???????
- ?????? ??????? ????? ? x5.8.
- ???? ?? ?? ????? ??? ????? ????? ???? ?? ??? ????
???????.
38??????? ????????
- ????? ????? ???? ????? ?????? ????? ??? ???? ???
????? - int pi
- pi ???? ????? ????? ?????? ????? integer ??? ??
???? ?????? ??????? ?????? ??????? int - pi 0
- ???? 0 ????? ???? ????? pi ????? ????? ?????
??????? ????? ??? ???? ????? ?????? pi ?????
????? ????? ???? ??? "?????" ????? ????? ???? ??
????? ?????? - ???? ???? ????? ??????? ?????? ?"? ????? ?? ?????
?? ?????? ????? ?????? ?????. ?? ????? ??????? ??
?????? ?? ???? NULL ???? ??????? ????? ?????
?????, ????? ??? ???? ????? ??? ?? ???? ???????
?????? ????? - int j
- int pi NULL
- ...
- pi j
- pi 0
39??????? ???????
- ????? ???? ?? number ?????? ???? ???? ?????????
???? ??? 0 ? number-1 - lttype namegt ltarray namegt ltnumbergt
- int x3
- ???? ????? ????? (??-????) ?"? ltarray
namegtltindexgt - x2
- "?? ?????" ??? ????? ??? ?? ???? ????? ?? ?????
???? ??? ?????. ??? ?????? x ??? ???????? ?????
????? ?? ????? ?????? ?????! ?????, x3 ???? ?
x03 - ????? ???? ?????number ????? ????? ????????
number ????? ?????? ??????, ???? ?????? ?????
???? ????? ?????? ????? ?????? - ???? ?????? ????? (?? ???? ?????) ?? ?"? ?????
- ?????? ?????? ????????? ????????
- int p, q
- p x / p points to x0 /
- q x2 / q points to x2 /
- p q-1 / p points to x1 /
????? ???? ??????? ?? ?????? ! ?????? ?????? ??
?? ??????? ??? ???? ????
40??????? ????????
- ????? ??? ?????, ???? ?? ?? ?????. ???? ????? ??
?????? ?????? ?????? ????? ?????? - int a, b / a and b are integers /
- int pi a / pi is a pointer to integer
- that points to a /
- int ppi pi / ppi is a pointer to pointer
- to integer. It points to pi
/ - pi b / pi points to b /
- pi 5 / b is set to 5 /
- ppi a / pi is set to a, pi points to a
now / - pi 6 / a is set to 6 /
- (ppi) 7 / a is set to 7 /
- (????? ?????? ????? ????? ??????? ??? ?????(
????? ??? ?????
ppi 0x202 0x200
'a' 0x201
pi 0x205 0x202
6 0x203
0x204
a 7 0x205
0x206
b 5 0x207
41????? ?- void
void ptr int i double d .. if ()
ptr d else ptr i
- ???? ?????? ????? ?????? void.
- ?????? ?????? ?? ???? ????? ????? ?? ????? ?????
- ????? ?????????? ?? ???? ??? ?????? ?????? ?"? ??
??? ????? ???? void, ???? ???? ?? ???? ??????
???? ???? cast ????? ?? ?????? ?????? ??????
????? ?-void - ????? ? void ??? ????? ???? ???????? ?????
??????? ?????? ??? ??????? ?????? ??? ?????? ????
????? ?????. ?????? ?????? ?????? ???? ?? ????
?????.
int pi int j pi (int) ptr j pi j
((int ) ptr)
42??????? ??????? ?????? ???????
- void malloc (unsigned int size)
- ?? ?????? ?????? ????? ????? ???? ?????? ???????,
???? ????? NULL - ????? ???????? ?????? ???? ???? ?????? ?????
?????? ?????, ?????? ?????? ?"? malloc ??? void - ??????? ?? malloc ?-NULL ?????? ?- stdlib.h.
- ??????
- int ptr
- ptr(int)malloc(4) / allocate 4 bytes /
- if (ptrNULL) / check if allocated /
- error(Failed to allocate 4 bytes)
-
- ??? ptr ????? ????? ??????? ??? ????? ???? ??????
(?? ?????? ??????) - ????? ?-ptr ??? ?????? int ?-malloc ?????? ???
????? void, ???? ???? casting - ?????? ??????? ?? malloc ??? void malloc
(size_t size), size_t ???? ????? ?? ?????
43????? ???
- ?? ????? ????? ???? ?
- ptr(int)malloc(4) / allocate 4 bytes /
- 4 ??? ???? ??? (magic number)
- ????? ???? ??? ???? ???? ???????, ???? ?? 0 ?? 1
- (??? ???????? ???????? ?-define)
- ????? ??
- ????? ?? ???? ?? ????
- ?? ??? ??????? ???? ?????? ?????, ?????? ????? ??
?? ??????? ?????? ????? ????? ???? - ???? ?????? ?????? ??? ??????? ???? ???? ?? ??
???????
44??????? ??????? ?????? ???????
- ptr(int)malloc(4) / allocate 4 bytes /
- ?? ????? ?? ?????? ??? ?
- define SIZE_OF_INT (4)
- ...
- ptr(int)malloc(SIZE_OF_INT)
- ???? ??? ?? ????? ????? ??? int ??? ????? ????
?-4 - ??? ??? ???? ?-non portable ?? ???? ??????? ???
?????? ??? ?????? ?????
45??????? ??????? ?????? ???????
- ??? ????? ?? ????? ?????? ?????? ???? ???? ????
?? ???????, ???????/?????? ?????/?????? ?????,
???? ?????? ???????? sizeof - sizeof(lttype namegt)
- ????? ?? ???? ????? ?? ?????? ?????? ???????
- ptr(int)malloc(sizeof(int))
- ptr3
- ??? ???? ?????? ? ptr ?????? ???? int (?? ??????
??????) - ?? ????? ????? ????
- ptr(int)malloc(sizeof(int ))
46??????? ??????? ?????? ???????
- free(ltptr vargt)
- ????? ?? ???? ??????? ?????? ???????? ????? ????.
????? - ptr(int )malloc(sizeof(int))
- if(ptr NULL) error(Not enough memory)
- ... / use ptr /
- if(ptr ! NULL) free (ptr)
- ?? ??????? ?-free NULL, ?? ???? ????
- ??? ??? ???? ????? ?-ptr ?? NULL ???? ??????
?free-
47????? ?? ?????? ???????
48???? ?? ????? ???????
- ????? ???? ????? ?? ????? ????
- ?????? ??? ????? ??????
- ?????? ????? ?????? ?????
- ??????? ????? ?????
- /Bus error Segmentation Fault
(from The Seattle Times)
49???? ?? ????? ???????
- ??????? ????? ???? ?????
- ???... ???? ?????? ?? ??? ??? ?? ?????? ???? ??
??? ??? - ?? ???? (????? ?? ?????) ??? ????? ????? ?????
?????? ????? ????? ????? ??????
50?????? ??????
- ????? ?????? ???? ????
- ?? ????? ?? \0
- strlen ????? ????? ?? \0 ???? ??? ?????? ????
?? ?????? - ????? ???? ?-sizeof
- ???????? ???? ????? ?? ?????
- ???? ???? ???? ?????? ????? ????? ?
- ptr (int)malloc(sizeof(int ))
- ptr (int)malloc(sizeof(int))
- ptr (int)malloc(sizeof(ptr))
51?????? ??????
- ????? ??????? ?????
- ????????? ?? ???? ?? ????? ??? ???? ????? ????
???? ????? ???? ????????? ?? ???? ???? ???????
?????
52?????? ??????
- ???? ??? ????
- ???? free ?? ???? ?????? ???????, ?? ?? ??????
??? - ptr(int )malloc(sizeof(int))
- ...
- ptr2 ptr
- ...
- free (ptr)
- ...
- ptr i
- i ptr
- ptr2 i
- i ptr2
- ???? ????? ?? ??????? ??? ????? ???? ???? ?????,
?? ?? ??? ????? ???
53?????? ??????
- ????? ??????? ??????? ?? ??????? ???? ????????
- ????? ????? ????? ????????, ?????? ?????? ???
???????? ?????? - int foo()
- int i
- ...
- return i
54?????? ??????
- ????? ??? ?-
- if(ptr NULL) printf(Bad pointer!\n)
- if(ptr NULL) printf(Bad pointer!\n)
55????? ????? ??????? ?????
- include ltstdio.hgt
- define TITLE ("\n\tMemory Corruption How easy
it is\n\n") - define BUF_SIZE (8)
- int main()
- int i
- char cBUF_SIZE, bufBUF_SIZE
- printf("Type string c\n")
- gets(c)
- printf("s\n",c)
- printf("Type string buf\n")
- gets(buf)
- printf("s\n",buf)
- for (i4 i gt -10 i--)
- ci 'a'
- printf("i 2d, buf s c s\n", i, buf, c)
-
- return 0
560
1
2
3
4
5
6
\0
0
1
2
\0
???
???
buf
c
a