summaryrefslogtreecommitdiff
path: root/reference/C/FUNCTIONS/malloc.html
blob: d4f7e0fd1e7911c677892b556a5f79e41f16517e (plain)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<title>malloc function</title>

<head>
<script language="JavaScript">
</script>
</head>

<body bgcolor=#ffffcc>
<hr>
<center>
<h1>malloc function </h1>
</center>
<hr>
<b>malloc</b> (memory allocation) is used to dynamically allocate memory 
at run time. Possible uses for this function are:

<ul>
<li>Read records of an unknown length.
<li>Read an unknown number of database records.
<li><a href="../MISC/linklists.html">Link lists</a>.
</ul>

The simplest way to reserve memory is to code something like:
<p>
<table border=2 width=100% bgcolor=ivory>
<tr>
<td>
<pre>

	main()
        {
            char string[1000];

            <a href="strcpy.html">strcpy</a> (string, "Some text");
        }
</pre>
</tr>
</td>
</table>
<p>
The example above has two problems:
<ul>
<li>If the data is less than

1000 bytes we are wasting memory.
<li>If the data is greater than 1000 bytes the program is going to crash.
<li>The 1000 bytes are reserved throught out the life of the program.    
If this was a long running program that rarely used the memory, it would 
again be wasteful.
</ul>
malloc allows us to allocate exactly the correct amount of memory and 
with the use of <a href="free.html">free</a> only for the time it is required.
<p>
<hr>
<pre>
	Library:   stdlib.h

	Prototype: void *malloc(size_t size);

	Syntax:    char * String;

                   String = (char *) malloc(1000);
</pre>
<hr>
<p>
Looking at the example syntax above, 1000 bytes are reserved and the pointer
<b>String</b> points to the first byte. The 1000 bytes are NOT initialized
by malloc. If the memory is NOT available, a 
<a href="../SYNTAX/null.html">NULL</a> pointer is returned.

Please note, the cast <a href="../CONCEPT/cast.html">cast</a> is required to 
return a pointer of the correct type.
<h2>Examples:</h2>

<img src="../../GRAPHICS/computer.gif"><a href="../EXAMPLES/linklst1.c"> example</a> 
program.

<p>
<hr>
<h2>See Also:</h2>
<img src=../../GRAPHICS/whiteball.gif>
<a href="../CONCEPT/data_types.html#char">char</a> data type.<p>
<img src=../../GRAPHICS/whiteball.gif>
<a href="free.html">free</a> function.<p>

<img src=../../GRAPHICS/whiteball.gif>
<a href="../SYNTAX/sizeof.html">sizeof</a> operator.<p>
<hr>
<font color=brown>
<h2>C++</h2>
<img src=../../GRAPHICS/whiteball.gif>
<a href="../../CPLUSPLUS/SYNTAX/new.html">new</a> is the C++ equivalent to malloc.<p>

<img src=../../GRAPHICS/whiteball.gif>
<a href="../../CPLUSPLUS/SYNTAX/delete.html">delete</a> is the C++ equivalent to 
<a href="free.html">free</a>.<p>
</font>

<p>

<hr>
<p>
<center>
<table border=2 width="80%" bgcolor="ivory">
<tr align=center>
<td width="25%">
<a href="../cref.html">                  Top</a>
</td><td width="25%">
<a href="../master_index.html">          Master Index</a>
</td><td width="25%">
<a href="../SYNTAX/keywords.html">                 Keywords</a>
</td><td width="25%">
<a href="funcref.htm">      Functions</a>
</td>
</tr>
</table>
</center>
<p>
<hr>
<address>Martin Leslie 
</address><p>
</body>
</html>