C (programming language): Difference between revisions

From David's Wiki
Created page with "C is the low-level programming language taught in UMD's CMSC216 class. ==Usage== ==Memory Allocation== There are 3 ways to allocate memory in C * <code>malloc(bytes)</code> A..."
 
No edit summary
Tag: Manual revert
 
(12 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==
==Memory Allocation==
===Memory Allocation===
There are 3 ways to allocate memory in C
<code>#include <stdlib.h></code><br>
There are 2 primary ways to allocate memory in C:
* <code>malloc(bytes)</code> Allocated memory is uninitialized.
* <code>malloc(bytes)</code> Allocated memory is uninitialized.
* <code>calloc(number, bytes)</code> Allocated memory is initialized to 0. Allocates (number * bytes) bytes of memory.
* <code>calloc(number, bytes)</code> Allocated memory is initialized to 0. Allocates (number * bytes) bytes of memory.
* <code>alloca(bytes)</code> [https://stackoverflow.com/questions/1018853/why-is-the-use-of-alloca-not-considered-good-practice Discouraged]
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.<br>
 
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>
{{ hidden | <code>alloca</code> |
For automatically garbage collection, use [[C++]] which has smart pointers.
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]
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.
 
On Windows you also have:
* <code>_malloca</code>
* <code>_calloca</code>
These are not portable so I wouldn't use them. They are a safer version of <code>alloca</code> which allocates to the heap if there isn't enough stack space. However, you need to free them using <code>_freea</code> which eliminates the main benefit of <code>alloca</code>.<br>
As far as I can tell, the only benefit is to prevent heap fragmentation.
}}
 
[[Category:Programming languages]]

Latest revision as of 19:29, 8 January 2024

C is a low-level programming language primarilly used for kernel and embedded development.

Usage

Memory Allocation

#include <stdlib.h>
There are 2 primary ways to allocate memory in C:

  • malloc(bytes) Allocated memory is uninitialized.
  • calloc(number, bytes) Allocated memory is initialized to 0. Allocates (number * bytes) bytes of memory.

Memory allocated by malloc and calloc are on the heap and should be deallocated by free when no longer used to avoid memory leaks.

alloca

There is also a way to dynamically allocate memory on the stack.

Memory allocated by alloca is allocated on the stack and will automatically be freed at the end of the function, not scope.
Do not call free on this memory. Do not allocate more than a few bytes using alloca or you will risk a stack overflow leading to undefined behavior.
For automatic garbage collection, use C++ smart pointers or Rust instead.

On Windows you also have:

  • _malloca
  • _calloca

These are not portable so I wouldn't use them. They are a safer version of alloca which allocates to the heap if there isn't enough stack space. However, you need to free them using _freea which eliminates the main benefit of alloca.
As far as I can tell, the only benefit is to prevent heap fragmentation.