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!
==Extensions and alternatives== The C library implementations shipping with various operating systems and compilers may come with alternatives and extensions to the standard <code>malloc</code> interface. Notable among these is: * <code>[[alloca]]</code>, which allocates a requested number of bytes on the [[call stack]]. No corresponding deallocation function exists, as typically the memory is deallocated as soon as the calling function returns. <code>alloca</code> was present on Unix systems as early as [[UNIX/32V|32/V]] (1978), but its use can be problematic in some (e.g., embedded) contexts.<ref>{{Cite web|title = Why is the use of alloca() not considered good practice?|url = https://stackoverflow.com/questions/1018853/why-is-the-use-of-alloca-not-considered-good-practice|website = stackoverflow.com|access-date = 2016-01-05}}</ref> While supported by many compilers, it is not part of the [[ANSI C|ANSI-C]] standard and therefore may not always be portable. It may also cause minor performance problems: it leads to variable-size stack frames, so that both [[Call stack#STACK-POINTER|stack and frame pointers]] need to be managed (with fixed-size stack frames, one of these is redundant).<ref>{{cite web |first1=Saman |last1=Amarasinghe |first2=Charles |last2=Leiserson |title=6.172 Performance Engineering of Software Systems, Lecture 10 |year=2010 |publisher=Massachusetts Institute of Technology |website=MIT OpenCourseWare |url=http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-172-performance-engineering-of-software-systems-fall-2010/video-lectures/lecture-8-cache-efficient-algorithms/ |access-date=27 January 2015 |archive-url=https://web.archive.org/web/20150622092347/http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-172-performance-engineering-of-software-systems-fall-2010/video-lectures/lecture-8-cache-efficient-algorithms/ |archive-date=22 June 2015 }}</ref> Larger allocations may also increase the risk of undefined behavior due to a [[stack overflow]].<ref>{{Cite web|title = alloca(3) - Linux manual page|url = http://man7.org/linux/man-pages/man3/alloca.3.html|website = man7.org|access-date = 2016-01-05}}</ref> C99 offered [[variable-length arrays]] as an alternative stack allocation mechanism{{snd}} however, this feature was relegated to optional in the later [[C11 (C standard revision)|C11]] standard. * [[POSIX]] defines a function <code>posix_memalign</code> that allocates memory with caller-specified alignment. Its allocations are deallocated with <code>free</code>,<ref>{{man|sh|posix_memalign}}</ref> so the implementation usually needs to be a part of the malloc library.
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)