C (programming language): Difference between revisions

No edit summary
No edit summary
Tag: Manual revert
 
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:
C is the low-level programming language taught in UMD's CMSC216 class.
C is a low-level programming language primarilly used for kernel and embedded development.


==Usage==
==Usage==
Line 9: Line 9:
Memory allocated by <code>malloc</code> and <code>calloc</code> are on the heap and should be deallocated by <code>free</code> when no longer used to avoid memory leaks.
Memory allocated by <code>malloc</code> and <code>calloc</code> are on the heap and should be deallocated by <code>free</code> when no longer used to avoid memory leaks.


{{ hidden | <code>_malloca</code> |
{{ hidden | <code>alloca</code> |
There is also a way to allocate memory on the stack.
There is also a way to dynamically allocate memory on the stack.
* <code>alloca(bytes)</code> Usage is [https://stackoverflow.com/questions/1018853/why-is-the-use-of-alloca-not-considered-good-practice Discouraged]
* <code>alloca(bytes)</code> Usage is [https://stackoverflow.com/questions/1018853/why-is-the-use-of-alloca-not-considered-good-practice Discouraged]
Memory allocated by <code>alloca</code> is allocated on the stack and will automatically be freed. Do not call <code>free</code> on this memory. Do not allocate more than a few bytes using <code>alloca</code> or you will risk a stack overflow leading to undefined behavior.<br>
Memory allocated by <code>alloca</code> is allocated on the stack and will automatically be freed at the end of the function, not scope.<br>
Do not call <code>free</code> on this memory. Do not allocate more than a few bytes using <code>alloca</code> or you will risk a stack overflow leading to undefined behavior.<br>
For automatic garbage collection, use [[C++]] smart pointers or Rust instead.
For automatic garbage collection, use [[C++]] smart pointers or Rust instead.


Line 21: Line 22:
As far as I can tell, the only benefit is to prevent heap fragmentation.
As far as I can tell, the only benefit is to prevent heap fragmentation.
}}
}}
[[Category:Programming languages]]