Rozmiar: 8938 bajtów


Malloc



malloc is a function for performing dynamic memory allocation in the C programming language. == 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, and the lifetime of the allocation is limited to the current function call. This can make it awkward to persist stack-allocated data over multiple function calls. 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 byte). This problem is alleviated 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, and a pointer to this block of memory is maintained. ==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. 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: int array[10]; But if we want to allocate the array dynamically we should write: #include int *ptr = malloc(sizeof(int) * 10); /* Allocates space for an array with 10 elements of type int */ if (ptr == NULL) { exit(1); /* We couldn't 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 uninitialised. 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. In the motivating example at first, it may be useful to grow or shrink a block of memory. One can allocate a new block, then copy the blocks and then free the old block, however, the realloc function is often used (which can conceivably optimise this operation). Its prototype is void *realloc(void *pointer, size_t bytes) If the new size is to be 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 virtual memory available, or the program has exceeded the amount of virtual memory it is allowed to reference, malloc will return a NULL pointer. Depending on the design of the operating system and the C standard library, this may happen with some frequency in production environments. Therefore, a well-written program ''should'' handle this situation. Unfortunately, many programs do not, and will crash if malloc fails. One reason why malloc failures are often ignored is that recovering from the error can be difficult. ====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. ====Double free==== When a pointer has been passed to free, the pointer can still be used, but it now references a region of memory with undefined content, which may not be available for use. For example: int *ptr = malloc(sizeof(int)); free(ptr); *ptr = 0; /* undefined behavior */ Problems of this kind can result in unpredictable program behavior — after the memory has been freed, the system may reuse that memory region for storage of unrelated data. So writing through a pointer to a deallocated region of memory may result in overwriting another piece of data somewhere else in the program — which may cause data corruption, or crash the program at some future point in time. A particularly bad example of this problem is if the same pointer is passed to free twice. ==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 plus plus programming language. Hence, we will call this the allocator rather than malloc. ===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. 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. 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 system RAM, 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. 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, huge pages are 4MiB, 1024 times larger, and so this method could be particularly devastating if userspace uses all huge pages. The advantage to the mmap method is that when the segment is freed, the memory is returned to the system immediately. ==See also== *Buffer overflow *Memory debugger ==External links== *[http://www.opengroup.org/onlinepubs/009695399/functions/malloc.html Definition of malloc in IEEE Std 1003.1 standard] *[http://gee.cs.oswego.edu/dl/html/malloc.html The design of the basis of the glibc allocator] by [http://gee.cs.oswego.edu/dl Doug Lea] *The Hoard memory allocator, by [http://www.cs.umass.edu/~emery Emery Berger] *"[http://www.research.ibm.com/people/m/michael/pldi-2004.pdf Scalable Lock-Free Dynamic Memory Allocation]" by Maged M. Michael *"[http://www-106.ibm.com/developerworks/linux/library/l-memory/ Inside memory management - The choices, tradeoffs, and implementations of dynamic allocation]" by Jonathan Bartlett C standard library Memory management

Malloc



In all actuality, I've solved the heap problem for glibc on Linux, in theory; but I can't bring myself to write a "Hybrid allocator" section with an explaination. I'm currently trying to get the glibc people to adopt the design and code it, but they haven't responded to me yet. The reason why I won't write the section is because I'm a source of bias. Until I get the thing accepted, my only reasoning for putting it on there could be that I'm trying to draw attention to the idea; however, it may be useful to draw attention to it, as others may benefit from the concept. So, if anyone wants me to write a section on it, I will, by request only. ''requests+++'' ==Pointers vs. arrays== Pointers and arrays are ''not'' the same in C; malloc returns a pointer to an allocated region of memory, it does not return an array (or a pointer to the beginning of an array, or anything else). The article confused this in a few places. See [http://www.eskimo.com/~scs/C-faq/q6.2.html here] and [http://www.eskimo.com/~scs/C-faq/q6.3.html here] for more information. User:Neilc 18:12, 27 Mar 2005 (UTC) :Pointers are not the same as declared arrays, but a pointer can be used in place of an array name. I don't know about recent compilers, but back in the 80's, the compiler took any array notation and converted it to pointer notation. User:Wrp103 - User talk:Wrp103 14:23, 29 Mar 2005 (UTC) ::I don't disagree that in some situations pointer notation and array notation are equivalent in C -- that is pretty elementary. That doesn't change the fact they are two distinct concepts, however. Furthermore, it is ''not'' true that the compiler will generate identical addressing code for an array and a pointer -- there is an [http://www.eskimo.com/~scs/C-faq/q6.2.html extra level of indirection] involved in accessing a pointer. For example, this program will dump core:
/* File 1 */
#include 
extern char *declared_as_ptr;

int main(void)
{
	printf("ptr = %s\n", declared_as_ptr);
	return 0;
}

/* File 2 */
char declared_as_ptr[] = "xxx";
::The pages I linked to originally have more information on this. User:Neilc 15:49, 29 Mar 2005 (UTC)

Malloc



==Wikipedia:Welcome, newcomers to the Wikipedia== Welcome, newcomer! Here are some useful tips to ease you into the Wikipedia experience: * First, take a look at the Wikipedia:Tutorial, and perhaps dabble a bit in the Wikipedia:Test area. * When you have some free time, take a look at the Wikipedia:Manual of Style and Wikipedia:Policies and guidelines. They can come in very handy! * Remember to use a Wikipedia:Neutral point of view! * If you need any Wikipedia:Help, feel free to post a question at the Wikipedia:Help desk * Explore, Wikipedia:Be bold in updating pages, and, most importantly, have fun! Also, here are some odds and ends that I find useful from time to time: *Wikipedia:Policy Library *Wikipedia:Utilities *Wikipedia:Cite your sources *Wikipedia:Verifiability *Wikipedia:Wikiquette *Wikipedia:Civility *Wikipedia:Conflict resolution *Wikipedia:Brilliant prose *Wikipedia:Pages needing attention *Wikipedia:Peer review *Wikipedia:Bad jokes and other deleted nonsense *Wikipedia:Village pump *Wikipedia:Boilerplate text Feel free to ask me anything the links and talk pages don't answer. You can most easily reach me by posting on User_talk:ClockworkTroll. You can sign your name on any page by typing 4 tildes, likes this: ~~~~. Best of luck, and have fun! User:ClockworkSoulUser talk:ClockworkSoul 03:09, 20 Dec 2004 (UTC)


See other meanings of words starting from letter:

M

MA | MB | MC | MD | ME | MF | MG | MH | MI | MJ | MK | ML | MN | MO | MP | MR | MS | MT | MU | MW | MX | MY | MZ |

Words begining with Malloc:

Malloc
Malloc
Malloc
Mallocks
Mallocks


These materials are based on Wikipedia and licensed under the GNU FDL



YouTube.com videos better site than Turbo Tax 2007
encyklopedia online