summaryrefslogtreecommitdiff
path: root/reference/C/FUNCTIONS/malloc.html
diff options
context:
space:
mode:
Diffstat (limited to 'reference/C/FUNCTIONS/malloc.html')
-rw-r--r--reference/C/FUNCTIONS/malloc.html123
1 files changed, 123 insertions, 0 deletions
diff --git a/reference/C/FUNCTIONS/malloc.html b/reference/C/FUNCTIONS/malloc.html
new file mode 100644
index 0000000..d4f7e0f
--- /dev/null
+++ b/reference/C/FUNCTIONS/malloc.html
@@ -0,0 +1,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>