|
|
1.1 root 1:
2:
3: malloc() General Function malloc()
4:
5:
6:
7:
8: Allocate dynamic memory
9:
10: cchhaarr *mmaalllloocc(_s_i_z_e) uunnssiiggnneedd _s_i_z_e;
11:
12: malloc helps to manage a program's free-space arenas. It uses a
13: circular, first-fit algorithm to select an unused block of at
14: least size bytes, marks the portion it uses, and returns a
15: pointer to it. The function free returns allocated memory to the
16: free memory pool.
17:
18: Each area allocated by malloc is rounded up to the nearest even
19: number and preceded by an unsigned int that contains the true
20: length. Thus, if you ask for three bytes you get four, and the
21: unsigned that precedes the newly allocated area is set to four.
22:
23: When an area is freed, its low order bit is turned on; consolida-
24: tion occurs when malloc passes over an area as it searches for
25: space. The end of each arena contains a block with a length of
26: zero, followed by a pointer to the next arena. Arenas point in a
27: circle.
28:
29: The most common problem with malloc occurs when a program
30: modifies more space than it allocates with malloc. This can
31: cause later mallocs to crash with a message that indicates that
32: the arena has been corrupted.
33:
34: ***** Example *****
35:
36: This example reads from the standard input up to NITEMS items,
37: each of which is up to MAXLEN long, sorts them, and writes the
38: sorted list onto the standard output. It demonstrates the
39: functions qsort, malloc, free, exit, and strcmp.
40:
41:
42: #include <stdio.h>
43: #define NITEMS 512
44: #define MAXLEN 256
45: char *data[NITEMS];
46: char string[MAXLEN];
47:
48:
49:
50: main()
51: {
52: register char **cpp;
53: register int count;
54: extern int compare();
55: extern char *malloc();
56: extern char *gets();
57:
58:
59:
60:
61:
62:
63:
64: COHERENT Lexicon Page 1
65:
66:
67:
68:
69: malloc() General Function malloc()
70:
71:
72:
73: for (cpp = &data[0]; cpp < &data[NITEMS]; cpp++) {
74: if (gets(string) == NULL)
75: break;
76: if ((*cpp = malloc(strlen(string) + 1)) == NULL)
77: exit(1);
78: strcpy(*cpp, string);
79: }
80:
81:
82:
83: count = cpp - &data[0];
84: qsort(data, count, sizeof(char *), compare);
85:
86:
87:
88: for (cpp = &data[0]; cpp < &data[count]; cpp++) {
89: printf("%s\n", *cpp);
90: free(*cpp);
91: }
92: exit(0);
93: }
94:
95:
96:
97: compare(p1, p2)
98: register char **p1, **p2;
99: {
100: extern int strcmp();
101: return(strcmp(*p1, *p2));
102: }
103:
104:
105: ***** See Also *****
106:
107: arena, calloc(), free(), general functions, malloc.h, memok(),
108: realloc(), setbuf()
109:
110: ***** Diagnostics *****
111:
112: malloc returns NULL if insufficient memory is available.
113:
114: ***** Notes *****
115:
116: The commonest error associated with mmaalllloocc is failing to declare
117: it properly. You should always declare mmaalllloocc as returning a
118: pointer to char.
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130: COHERENT Lexicon Page 2
131:
132:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.