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.
example program.
char data type.
free function.
sizeof operator.
new is the C++ equivalent to malloc.
delete is the C++ equivalent to free.
Top | Master Index | Keywords | Functions |