From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/C/FUNCTIONS/malloc.html | 123 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 reference/C/FUNCTIONS/malloc.html (limited to 'reference/C/FUNCTIONS/malloc.html') 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 @@ +malloc function + + + + + + +
+
+

malloc function

+
+
+malloc (memory allocation) is used to dynamically allocate memory +at run time. Possible uses for this function are: + + + +The simplest way to reserve memory is to code something like: +

+ + + + +
+
+
+	main()
+        {
+            char string[1000];
+
+            strcpy (string, "Some text");
+        }
+
+
+

+The example above has two problems: +

+malloc allows us to allocate exactly the correct amount of memory and +with the use of free only for the time it is required. +

+


+
+	Library:   stdlib.h
+
+	Prototype: void *malloc(size_t size);
+
+	Syntax:    char * String;
+
+                   String = (char *) malloc(1000);
+
+
+

+Looking at the example syntax above, 1000 bytes are reserved and the pointer +String points to the first byte. The 1000 bytes are NOT initialized +by malloc. If the memory is NOT available, a +NULL pointer is returned. + +Please note, the cast cast is required to +return a pointer of the correct type. +

Examples:

+ + example +program. + +

+


+

See Also:

+ +char data type.

+ +free function.

+ + +sizeof operator.

+


+ +

C++

+ +new is the C++ equivalent to malloc.

+ + +delete is the C++ equivalent to +free.

+ + +

+ +


+

+

+ + + + +
+ Top + + Master Index + + Keywords + + Functions +
+
+

+


+
Martin Leslie +

+ + -- cgit v1.2.3-54-g00ecf