FACTOID # 172: The number of tourists in San Marino is almost 19 times the resident population.
 
 Home   Encyclopedia   Statistics   Countries A-Z   Flags   Maps   Education   Forum   FAQ   About 
 
 
 
WHAT'S NEW
RECENT ARTICLES
More Recent Articles »
 

SEARCH ALL

FACTS & STATISTICS    Advanced view

Search encyclopedia, statistics and forums:

 

 

(* = Graphable)

 

 


Encyclopedia > Free (programming)

In computing, malloc is a subroutine provided in the C programming language's standard library for performing dynamic memory allocation. Originally, the word computing was synonymous with counting and calculating, and a science that deals with the original sense of computing mathematical calculations. ... In computer science, a subroutine (function, procedure, or subprogram) is a sequence of code which performs a specific task, as part of a larger program, and is grouped as one or more statement blocks; such code is sometimes collected into software libraries. ... The C Programming Language, Brian Kernighan and Dennis Ritchie, the original edition that served for many years as an informal specification of the language The C programming language is a standardized imperative computer programming language developed in the early 1970s by Dennis Ritchie for use on the Unix operating system. ... In C++, the Standard Library is a collection of classes and functions, which are written in the core language. ... In computer science, dynamic memory allocation is the allocation of memory storage for use in a computer program during the runtime of that program. ...

Contents


Rationale

The C programming language normally manages memory statically, that is, on the stack. If space for a variable is needed, it is created when the function is entered and is automatically reclaimed when the function returns. However, stack-based allocation is somewhat limited: the size of the allocation must be a compile-time constant. Consider getting input from a user and storing it in a string: the size of the string must be known at compilation-time as the size of the stack frame must be known beforehand. Thus if the programmer allocates space for eleven characters and the user types fifteen, five characters will be lost (one byte is reserved for the terminating null character). The C Programming Language, Brian Kernighan and Dennis Ritchie, the original edition that served for many years as an informal specification of the language The C programming language is a standardized imperative computer programming language developed in the early 1970s by Dennis Ritchie for use on the Unix operating system. ... Computer code (HTML with JavaScript) in a tool that uses syntax highlighting (colors) to help the developer see the purpose of each piece of code. ... To meet Wikipedias quality standards, this article or section may require cleanup. ... Stack in computing refers to: Stack (data structure) Stack-based memory allocation as opposed to Heap-based memory allocation in computing architecture. ... In computer science and mathematics, a variable is a symbol denoting a quantity or symbolic representation. ... In computer science, a subroutine (function, procedure, or subprogram) is a sequence of code which performs a specific task, as part of a larger program, and is grouped as one or more statement blocks; such code is sometimes collected into software libraries. ... It has been suggested that this article or section be merged with Compile (software company). ... In computing, a stack frame is a data structure used to create temporary storage for data and saved state in functions. ... The null character (also null terminator) is a character with the value zero, present in the ASCII and Unicode character sets, and available in nearly all mainstream programming languages. ...


Another limitation of stack allocation is the lifetime of the allocated memory: it is automatically released when the function that allocates the memory returns. This can make it awkward to persist stack-allocated data over multiple function calls. In many situations the programmer requires greater flexibility in managing the lifetime of a memory allocation.


These limitations are avoided by allocating memory elsewhere, such as on the heap. The heap is a memory area reserved for allocating memory dynamically. Generally, one uses malloc() to allocate a block of memory on the heap. The program accesses this block of memory via a pointer; when the memory is no longer needed, the pointer is passed to free() and the memory can be reused by the system. It has been suggested that this article or section be merged with Memory allocation. ... In computer science, a pointer is a programming language datatype whose value refers directly to (points to) another value stored elsewhere in the computer memory using its address. ...


Dynamic memory allocation in C

The malloc function is the basic function used to allocate memory on the heap in C. Its prototype is

 void *malloc(size_t size) 

which allocates size bytes of memory. If the allocation succeeds, a pointer to the block of memory is returned. malloc returns a void pointer (void *), which indicates that it is a pointer to a region of unknown data type. This pointer is typically cast to a more specific pointer type by the programmer before being used. A void pointer is a pointer that points to an object of unknown type. ...


Memory allocated via malloc is persistent: it will continue to exist until the program terminates or the memory is explicitly deallocated by the programmer (that is, the block is said to be "freed"). This is achieved by use of the free function. Its prototype is

 void free(void *pointer) 

which releases the block of memory pointed to by pointer.


Usage example

The standard method of creating an array of ten integers on the stack: In computer programming, an array, also known as a vector or list (for one-dimensional arrays) or a matrix (for two-dimensional arrays), is one of the simplest data structures. ...

 int array[10]; 

But if we want to allocate the array dynamically we should write:

 #include <stdlib.h> /* Allocate space for an array with 10 elements of type int */ int *ptr = (int *)malloc(sizeof(int) * 10); if (ptr == NULL) exit(1); /* We could not allocate any memory, so exit */ /* allocation succeeded */ 

Related functions

malloc() returns a block of memory that is allocated for the programmer to use, but is uninitialized. The memory is usually initialized by hand if necessary -- either via the memset() function, or by one or more assignment statements that dereference the pointer. An alternative is to use the calloc() function, which allocates memory and then initializes it. Its prototype is

 void *calloc(size_t nelements, size_t bytes) 

which allocates a region of memory large enough to hold nelements of size bytes each. The allocated region is initialized to zero.


It is often useful to be able to grow or shrink a block of memory. This can be done using malloc() and free(): a new block of the appropriate size can be allocated, the content can be copied over, and then the old block can be freed. However, this is inefficient and awkward; instead, the realloc() function can be used. Its prototype is

 void *realloc(void *pointer, size_t bytes) 

realloc returns a pointer to a memory region of the specified size. If the new size is greater than the old size, the block is grown, otherwise it is shrunk.


Common errors

Some programmers find that the improper use of malloc() and related functions in C can be a frequent source of bugs.


Allocation failure

malloc() is not guaranteed to succeed — if there is no memory available, or if the program has exceeded the amount of memory it is allowed to reference, malloc() will return a NULL pointer. Depending on the nature of the underlying environment, this may or may not be a likely occurrence. Many programs do not check for malloc() failure. Such a program would attempt to use the NULL pointer returned by malloc() as if it pointed to allocated memory, and the program would crash. This has traditionally been considered an incorrect design, although it remains common, as memory allocation failures only occur rarely in most situations, and the program frequently can do nothing better than to exit anyway. Checking for allocation failure is more important when implementing libraries -- since the library might be used in low-memory environments, it is usually considered good practice to return memory allocation failures to the program using the library and allow it to choose whether to attempt to handle the error.


Memory leaks

The return value of malloc(), calloc(), and realloc() must be passed to the free() function so that it can be released. If this is not done, the allocated memory is not released until the process exits — in other words, a memory leak will occur. A memory leak is unnecessary memory consumption by a computer program. ...


Use after free

After a pointer has been passed to free(), it references a region of memory with undefined content, which may not be available for use. However, the pointer may still be used, for example:

 int *ptr = (int *)malloc(sizeof(int)); free(ptr); *ptr = 0; /* undefined behavior */ 

Code like this may have unpredictable behavior — after the memory has been freed, the system may reuse that memory region for storage of unrelated data. Therefore, writing through a pointer to a deallocated region of memory may result in overwriting another piece of data somewhere else in the program. Depending on what data is overwritten, this may cause data corruption or cause the program to crash at a later time. A particularly bad example of this problem is if the same pointer is passed to free() twice, known as a double free. To avoid this, some programmers set pointers to NULL after freeing them, as free(NULL) is safe—it does nothing.


Implementations

The implementation of memory management depends greatly upon operating system and architecture. Some operating systems supply an allocator for malloc, while others supply functions to control certain regions of data.


The same dynamic memory allocator is often used to implement both malloc and operator new in C++. Hence, we will call this the allocator rather than malloc. C++ (pronounced see plus plus, IPA: ) is a general-purpose computer programming language. ...


Heap-based

Implementation of the allocator on IA-32 architectures is commonly done using the heap, or data segment. The allocator will usually expand and contract the heap to fulfill allocation requests. IA-32, sometimes generically called x86-32, is the computer architecture of Intels most successful microprocessors. ...


The heap method suffers from a few inherent flaws, stemming entirely from fragmentation. Like any method of memory allocation, the heap will become fragmented; that is, there will be sections of used and unused memory in the allocated space on the heap. A good allocator will attempt to find an unused area of already allocated memory to use before resorting to expanding the heap. However, due to performance it can be impossible to use an allocator in a real time system and a memory pool must be deployed instead. Fragmentation is a term that occurs in several fields and describes a process of something breaking or being divided into pieces (fragments). ... Memory pools allow dynamic memory allocation comparable to malloc or the operator new in C++. As those implementations suffer from fragmentation because of variable block sizes, it can be impossible to use them in a real time system due to performance. ...


The major problem with this method is that the heap has only two significant attributes: base, or the beginning of the heap in virtual memory space; and length, or its size. The heap requires enough system memory to fill its entire length, and its base can never change. Thus, any large areas of unused memory are wasted. The heap can get "stuck" in this position if a small used segment exists at the end of the heap, which could waste any magnitude of address space, from a few megabytes to a few hundred.


The glibc allocator

The GNU C library, glibc, uses both brk and mmap on the Linux operating system. The brk system call will change the size of the heap to be larger or smaller as needed; while the mmap system call will be used when extremely large segments are allocated. The heap method suffers the same flaws as any other, while the mmap method may avert problems with huge buffers trapping a small allocation at the end after their expiration. Glibc is the GNU projects C standard library, licensed under the LGPL. The lead contributor and maintainer is Ulrich Drepper. ... In computing, mmap() is a POSIX-compliant Unix system call that maps files or devices into memory. ... Tux is the official Linux mascot. ...


The mmap method has its own flaws. It always allocates a segment by mapping pages. Only one set of mapped pages exists for each allocated segment. Mapping a single byte will use an entire page, usually 4096 bytes, on IA-32; however, large pages are 4MiB, 1024 times larger, and so this method could be particularly devastating if userspace uses only large pages. The advantage to the mmap method is that when the segment is freed, the memory is returned to the system immediately.


OpenBSD's malloc

OpenBSD's implementation of the malloc function makes use of mmap. For requests greater in size than one page, the entire allocation is retrieved using mmap; smaller sizes are assigned from memory pools maintained by malloc within a number of "bucket pages," also allocated with mmap. On a call to free, memory is released and unmapped from the process address space using munmap. This system is designed to improve security by taking advantage of the address space layout randomization and gap page features implemented as part of OpenBSD's mmap system call, and to detect use-after-free bugs—as a large memory allocation is completely unmapped after it is freed, further use causes a segmentation fault and termination of the program. OpenBSD is a freely available Unix-like computer operating system descended from Berkeley Software Distribution (BSD), a Unix derivative created by the University of California, Berkeley. ... In computer operating systems, paging memory allocation algorithms divide computer memory into small partitions, and allocates memory using a page as the smallest building block. ... The introduction to this article provides insufficient context for those unfamiliar with the subject matter. ... In computer science, Address space layout randomization (ASLR) is a process which entails arranging the positions of major data areas randomly in virtual address space. ... In computing, a system call is the mechanism used by an application program to request service from the operating system, or more specifically, the operating system kernel. ... A segmentation fault (sometimes referred to as segfault for short) is a particular error condition that can occur during the operation of computer software. ...


See also

In computer security and programming, a buffer overflow, or buffer overrun, is an anomalous condition where a process attempts to store more data in a buffer than there is memory allocated for it. ... A memory debugger is a programming tool for finding memory leaks and buffer overflows. ... In Unix-like operating systems, mprotect() is a POSIX system call for controlling memory protections. ...

External links



 
 

COMMENTARY     


Share your thoughts, questions and commentary here
Your name
Your comments

Want to know more?
Search encyclopedia, statistics and forums:

 


Lesson Plans | Student Area | Student FAQ | Reviews | Press Releases |  Feeds | Contact
The Wikipedia article included on this page is licensed under the GFDL.
Images may be subject to relevant owners' copyright.
All other elements are (c) copyright NationMaster.com 2003-5. All Rights Reserved.
Usage implies agreement with terms, 1022, m