Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
C dynamic memory allocation
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Rationale== The [[C (programming language)|C programming language]] manages memory [[Static memory allocation|statically]], [[Automatic memory allocation|automatically]], or [[Dynamic memory allocation|dynamically]]. Static-duration variables are allocated in main memory, usually along with the executable code of the program, and persist for the lifetime of the program; automatic-duration variables are allocated on the [[call stack|stack]] and come and go as functions are called and return. For static-duration and automatic-duration variables, the size of the allocation must be [[compile-time]] constant (except for the case of variable-length automatic arrays<ref>{{cite web |url=https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html |title=gcc manual |publisher=gnu.org |access-date=14 December 2008 }}</ref>). If the required size is not known until [[Run time (program lifecycle phase)|run-time]] (for example, if data of arbitrary size is being read from the user or from a disk file), then using fixed-size data objects is inadequate. The lifetime of allocated memory can also cause concern. Neither static- nor automatic-duration memory is adequate for all situations. Automatic-allocated data cannot persist across multiple function calls, while static data persists for the life of the program whether it is needed or not. In many situations the programmer requires greater flexibility in managing the lifetime of allocated memory. These limitations are avoided by using [[dynamic memory allocation]], in which memory is more explicitly (but more flexibly) managed, typically by allocating it from the {{citation needed span|''free store'' (informally called the "heap"),|date=July 2022|reason=In my experience 'heap' is almost exclusively used in a C context. While free store is used in C++ context for the new/delete operators.}} an area of memory structured for this purpose. In C, the library function <code>malloc</code> is used to allocate a block of memory on the heap. The program accesses this block of memory via a [[pointer (computer programming)|pointer]] that <code>malloc</code> returns. When the memory is no longer needed, the pointer is passed to <code>free</code> which deallocates the memory so that it can be used for other purposes. The original description of C indicated that <code>calloc</code> and <code>cfree</code> were in the standard library, but not <code>malloc</code>. Code for a simple model implementation of a storage manager for [[Unix]] was given with <code>alloc</code> and <code>free</code> as the user interface functions, and using the <code>[[sbrk]]</code> system call to request memory from the operating system.<ref>Brian W. Kernighan, Dennis M. Ritchie, ''The C Programming Language'', Prentice-Hall, 1978; Section 7.9 (page 156) describes <code>calloc</code> and <code>cfree</code>, and Section 8.7 (page 173) describes an implementation for <code>alloc</code> and <code>free</code>.</ref> The 6th Edition Unix documentation gives <code>alloc</code> and <code>free</code> as the low-level memory allocation functions.<ref>{{man|3|alloc|v6}}</ref> The <code>malloc</code> and <code>free</code> routines in their modern form are completely described in the 7th Edition Unix manual.<ref>{{man|3|malloc|v7}}</ref><ref>Anonymous, ''Unix Programmer's Manual, Vol. 1'', Holt Rinehart and Winston, 1983 (copyright held by Bell Telephone Laboratories, 1983, 1979); The <code>man</code> page for <code>malloc</code> etc. is given on page 275.</ref> Some platforms provide library or [[intrinsic function]] calls which allow run-time dynamic allocation from the C stack rather than the heap (e.g. <code>alloca()</code><ref>{{man|3|alloca|FreeBSD}} </ref>). This memory is automatically freed when the calling function ends.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)