home *** CD-ROM | disk | FTP | other *** search
/ Sams Teach Yourself C in 21 Days (6th Edition) / STYC216E.ISO / mac / Examples / Day20 / malloc.c < prev    next >
MacBinary  |  2002-08-11  |  1.8 KB  |  [TEXT/LMAN]

open in: MacOS 8.1     |     Win98     |     DOS

browse contents    |     view JSON data     |     view as text


This file was processed as: MacBinary (archive/macBinary).

ConfidenceProgramDetectionMatch TypeSupport
66% dexvert Compact Compressed (Unix) (archive/compact) ext Supported
10% dexvert MacBinary (archive/macBinary) fallback Supported
1% dexvert Text File (text/txt) fallback Supported
100% file MacBinary II, Sun Aug 11 11:26:06 2002, modified Sun Aug 11 11:26:06 2002, creator 'LMAN', type ASCII, 1186 bytes "malloc.c" , at 0x522 286 bytes resource default (weak)
99% file data default
74% TrID Macintosh plain text (MacBinary) default
25% TrID MacBinary 2 default (weak)
100% siegfried fmt/1762 MacBinary (II) default
100% lsar MacBinary default


id metadata
keyvalue
macFileType[TEXT]
macFileCreator[LMAN]



hex view
+--------+-------------------------+-------------------------+--------+--------+
|00000000| 00 08 6d 61 6c 6c 6f 63 | 2e 63 00 00 00 00 00 00 |..malloc|.c......|
|00000010| 00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00 |........|........|
|00000020| 00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00 |........|........|
|00000030| 00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00 |........|........|
|00000040| 00 54 45 58 54 4c 4d 41 | 4e 00 00 ff ff ff ff 00 |.TEXTLMA|N.......|
|00000050| 00 00 00 00 00 04 a2 00 | 00 01 1e b9 7c 32 8e b9 |........|....|2..|
|00000060| 7c 32 8e 00 00 00 00 00 | 00 00 00 00 00 00 00 00 ||2......|........|
|00000070| 00 00 00 00 00 00 00 00 | 00 00 81 81 a0 e0 00 00 |........|........|
|00000080| 2f 2a 20 55 73 69 6e 67 | 20 6d 61 6c 6c 6f 63 28 |/* Using| malloc(|
|00000090| 29 20 74 6f 20 64 65 74 | 65 72 6d 69 6e 65 20 66 |) to det|ermine f|
|000000a0| 72 65 65 20 6d 65 6d 6f | 72 79 2e 2a 2f 0d 0a 0d |ree memo|ry.*/...|
|000000b0| 0a 23 69 6e 63 6c 75 64 | 65 20 3c 73 74 64 69 6f |.#includ|e <stdio|
|000000c0| 2e 68 3e 0d 0a 23 69 6e | 63 6c 75 64 65 20 3c 73 |.h>..#in|clude <s|
|000000d0| 74 64 6c 69 62 2e 68 3e | 0d 0a 0d 0a 2f 2a 20 44 |tdlib.h>|..../* D|
|000000e0| 65 66 69 6e 69 74 69 6f | 6e 20 6f 66 20 61 20 73 |efinitio|n of a s|
|000000f0| 74 72 75 63 74 75 72 65 | 20 74 68 61 74 20 69 73 |tructure| that is|
|00000100| 0d 0a 20 20 20 31 30 32 | 34 20 62 79 74 65 73 20 |.. 102|4 bytes |
|00000110| 28 31 20 6b 69 6c 6f 62 | 79 74 65 29 20 69 6e 20 |(1 kilob|yte) in |
|00000120| 73 69 7a 65 2e 29 20 2a | 2f 0d 0a 0d 0a 73 74 72 |size.) *|/....str|
|00000130| 75 63 74 20 6b 69 6c 6f | 20 7b 0d 0a 20 20 20 73 |uct kilo| {.. s|
|00000140| 74 72 75 63 74 20 6b 69 | 6c 6f 20 2a 6e 65 78 74 |truct ki|lo *next|
|00000150| 3b 0d 0a 20 20 20 63 68 | 61 72 20 64 75 6d 6d 79 |;.. ch|ar dummy|
|00000160| 5b 31 30 32 32 5d 3b 0d | 0a 7d 3b 0d 0a 0d 0a 69 |[1022];.|.};....i|
|00000170| 6e 74 20 46 72 65 65 4d | 65 6d 28 76 6f 69 64 29 |nt FreeM|em(void)|
|00000180| 3b 0d 0a 0d 0a 69 6e 74 | 20 6d 61 69 6e 28 20 76 |;....int| main( v|
|00000190| 6f 69 64 20 29 0d 0a 7b | 0d 0a 0d 0a 20 20 20 70 |oid )..{|.... p|
|000001a0| 72 69 6e 74 66 28 22 59 | 6f 75 20 68 61 76 65 20 |rintf("Y|ou have |
|000001b0| 25 64 20 6b 69 6c 6f 62 | 79 74 65 73 20 66 72 65 |%d kilob|ytes fre|
|000001c0| 65 2e 5c 6e 22 2c 20 46 | 72 65 65 4d 65 6d 28 29 |e.\n", F|reeMem()|
|000001d0| 29 3b 0d 0a 20 20 20 72 | 65 74 75 72 6e 20 30 3b |);.. r|eturn 0;|
|000001e0| 0d 0a 7d 0d 0a 0d 0a 69 | 6e 74 20 46 72 65 65 4d |..}....i|nt FreeM|
|000001f0| 65 6d 28 76 6f 69 64 29 | 0d 0a 7b 0d 0a 20 20 20 |em(void)|..{.. |
|00000200| 2f 2a 52 65 74 75 72 6e | 73 20 74 68 65 20 6e 75 |/*Return|s the nu|
|00000210| 6d 62 65 72 20 6f 66 20 | 6b 69 6c 6f 62 79 74 65 |mber of |kilobyte|
|00000220| 73 20 28 31 30 32 34 20 | 62 79 74 65 73 29 0d 0a |s (1024 |bytes)..|
|00000230| 20 20 20 6f 66 20 66 72 | 65 65 20 6d 65 6d 6f 72 | of fr|ee memor|
|00000240| 79 2e 20 2a 2f 0d 0a 0d | 0a 20 20 20 6c 6f 6e 67 |y. */...|. long|
|00000250| 20 63 6f 75 6e 74 65 72 | 3b 0d 0a 20 20 20 73 74 | counter|;.. st|
|00000260| 72 75 63 74 20 6b 69 6c | 6f 20 2a 68 65 61 64 2c |ruct kil|o *head,|
|00000270| 20 2a 63 75 72 72 65 6e | 74 2c 20 2a 6e 65 78 74 | *curren|t, *next|
|00000280| 6f 6e 65 3b 0d 0a 0d 0a | 20 20 20 63 75 72 72 65 |one;....| curre|
|00000290| 6e 74 20 3d 20 68 65 61 | 64 20 3d 20 28 73 74 72 |nt = hea|d = (str|
|000002a0| 75 63 74 20 6b 69 6c 6f | 2a 29 20 6d 61 6c 6c 6f |uct kilo|*) mallo|
|000002b0| 63 28 73 69 7a 65 6f 66 | 28 73 74 72 75 63 74 20 |c(sizeof|(struct |
|000002c0| 6b 69 6c 6f 29 29 3b 0d | 0a 0d 0a 20 20 20 69 66 |kilo));.|... if|
|000002d0| 20 28 68 65 61 64 20 3d | 3d 20 4e 55 4c 4c 29 0d | (head =|= NULL).|
|000002e0| 0a 20 20 20 20 20 20 72 | 65 74 75 72 6e 20 30 3b |. r|eturn 0;|
|000002f0| 20 20 20 20 20 20 2f 2a | 4e 6f 20 6d 65 6d 6f 72 | /*|No memor|
|00000300| 79 20 61 76 61 69 6c 61 | 62 6c 65 2e 2a 2f 0d 0a |y availa|ble.*/..|
|00000310| 0d 0a 20 20 20 63 6f 75 | 6e 74 65 72 20 3d 20 30 |.. cou|nter = 0|
|00000320| 3b 0d 0a 20 20 20 64 6f | 0d 0a 20 20 20 7b 0d 0a |;.. do|.. {..|
|00000330| 20 20 20 20 20 20 63 6f | 75 6e 74 65 72 2b 2b 3b | co|unter++;|
|00000340| 0d 0a 20 20 20 20 20 20 | 63 75 72 72 65 6e 74 2d |.. |current-|
|00000350| 3e 6e 65 78 74 20 3d 20 | 28 73 74 72 75 63 74 20 |>next = |(struct |
|00000360| 6b 69 6c 6f 2a 29 20 6d | 61 6c 6c 6f 63 28 73 69 |kilo*) m|alloc(si|
|00000370| 7a 65 6f 66 28 73 74 72 | 75 63 74 20 6b 69 6c 6f |zeof(str|uct kilo|
|00000380| 29 29 3b 0d 0a 20 20 20 | 20 20 20 63 75 72 72 65 |));.. | curre|
|00000390| 6e 74 20 3d 20 63 75 72 | 72 65 6e 74 2d 3e 6e 65 |nt = cur|rent->ne|
|000003a0| 78 74 3b 0d 0a 20 20 20 | 20 20 20 70 72 69 6e 74 |xt;.. | print|
|000003b0| 66 28 22 5c 72 25 64 22 | 2c 20 63 6f 75 6e 74 65 |f("\r%d"|, counte|
|000003c0| 72 29 3b 0d 0a 20 20 20 | 7d 20 77 68 69 6c 65 20 |r);.. |} while |
|000003d0| 28 63 75 72 72 65 6e 74 | 20 21 3d 20 4e 55 4c 4c |(current| != NULL|
|000003e0| 29 3b 0d 0a 0d 0a 20 20 | 20 2f 2a 20 4e 6f 77 20 |);.... | /* Now |
|000003f0| 63 6f 75 6e 74 65 72 20 | 68 6f 6c 64 73 20 74 68 |counter |holds th|
|00000400| 65 20 6e 75 6d 62 65 72 | 20 6f 66 20 74 79 70 65 |e number| of type|
|00000410| 20 6b 69 6c 6f 0d 0a 20 | 20 20 20 20 20 73 74 72 | kilo.. | str|
|00000420| 75 63 74 75 72 65 73 20 | 77 65 20 77 65 72 65 20 |uctures |we were |
|00000430| 61 62 6c 65 20 74 6f 20 | 61 6c 6c 6f 63 61 74 65 |able to |allocate|
|00000440| 2e 20 57 65 0d 0a 20 20 | 20 20 20 20 6d 75 73 74 |. We.. | must|
|00000450| 20 66 72 65 65 20 74 68 | 65 6d 20 61 6c 6c 20 62 | free th|em all b|
|00000460| 65 66 6f 72 65 20 72 65 | 74 75 72 6e 69 6e 67 2e |efore re|turning.|
|00000470| 20 2a 2f 0d 0a 0d 0a 20 | 20 20 63 75 72 72 65 6e | */.... | curren|
|00000480| 74 20 3d 20 68 65 61 64 | 3b 0d 0a 20 20 20 64 6f |t = head|;.. do|
|00000490| 0d 0a 20 20 20 7b 0d 0a | 20 20 20 20 20 20 6e 65 |.. {..| ne|
|000004a0| 78 74 6f 6e 65 20 3d 20 | 63 75 72 72 65 6e 74 2d |xtone = |current-|
|000004b0| 3e 6e 65 78 74 3b 0d 0a | 20 20 20 20 20 20 66 72 |>next;..| fr|
|000004c0| 65 65 28 63 75 72 72 65 | 6e 74 29 3b 0d 0a 20 20 |ee(curre|nt);.. |
|000004d0| 20 20 20 20 63 75 72 72 | 65 6e 74 20 3d 20 6e 65 | curr|ent = ne|
|000004e0| 78 74 6f 6e 65 3b 0d 0a | 20 20 20 7d 20 77 68 69 |xtone;..| } whi|
|000004f0| 6c 65 20 28 6e 65 78 74 | 6f 6e 65 20 21 3d 20 4e |le (next|one != N|
|00000500| 55 4c 4c 29 3b 0d 0a 0d | 0a 20 20 20 72 65 74 75 |ULL);...|. retu|
|00000510| 72 6e 20 63 6f 75 6e 74 | 65 72 3b 0d 0a 7d 0d 0a |rn count|er;..}..|
|00000520| 0d 0a 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00 |........|........|
|00000530| 00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00 |........|........|
|00000540| 00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00 |........|........|
|00000550| 00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00 |........|........|
|00000560| 00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00 |........|........|
|00000570| 00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00 |........|........|
|00000580| 00 00 01 00 00 00 01 00 | 00 00 00 00 00 00 00 1e |........|........|
|00000590| 54 68 69 73 20 72 65 73 | 6f 75 72 63 65 20 66 6f |This res|ource fo|
|000005a0| 72 6b 20 69 6e 74 65 6e | 74 69 6f 6e 61 6c 6c 79 |rk inten|tionally|
|000005b0| 20 6c 65 66 74 20 62 6c | 61 6e 6b 20 20 20 00 00 | left bl|ank ..|
|000005c0| 00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00 |........|........|
|000005d0| 00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00 |........|........|
|000005e0| 00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00 |........|........|
|000005f0| 00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00 |........|........|
|00000600| 00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00 |........|........|
|00000610| 00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00 |........|........|
|00000620| 00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00 |........|........|
|00000630| 00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00 |........|........|
|00000640| 00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00 |........|........|
|00000650| 00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00 |........|........|
|00000660| 00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00 |........|........|
|00000670| 00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00 |........|........|
|00000680| 00 00 01 00 00 00 01 00 | 00 00 00 00 00 00 00 1e |........|........|
|00000690| 00 00 00 00 00 00 00 00 | 00 1c 00 1e ff ff 00 00 |........|........|
|000006a0| 00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00 |........|........|
|000006b0| 00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00 |........|........|
|000006c0| 00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00 |........|........|
|000006d0| 00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00 |........|........|
|000006e0| 00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00 |........|........|
|000006f0| 00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00 |........|........|
+--------+-------------------------+-------------------------+--------+--------+