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!
==Usage example== Creating an [[Array data structure|array]] of ten integers with automatic scope is straightforward in C: <syntaxhighlight lang="c"> int array[10]; </syntaxhighlight> However, the size of the array is fixed at compile time. If one wishes to allocate a similar array dynamically without using a [[variable-length array]], which is not guaranteed to be supported in all [[C11 (C standard revision)|C11]] implementations, the following code can be used: <syntaxhighlight lang="c"> int *array = malloc(10 * sizeof(int)); </syntaxhighlight> This computes the number of bytes that ten integers occupy in memory, then requests that many bytes from <code>malloc</code> and assigns the result to a [[Pointer (computer programming)|pointer]] named <code>array</code> (due to C syntax, pointers and arrays can be used interchangeably in some situations). Because <code>malloc</code> might not be able to service the request, it might return a [[null pointer]] and it is [[Coding best practices|good programming practice]] to check for this: <syntaxhighlight lang="c"> int *array = malloc(10 * sizeof(int)); if (array == NULL) { fprintf(stderr, "malloc failed\n"); return -1; } </syntaxhighlight> When the program no longer needs the [[dynamic array]], it must eventually call <code>free</code> to return the memory it occupies to the free store: <syntaxhighlight lang="c"> free(array); </syntaxhighlight> The memory set aside by <code>malloc</code> is not [[Initialization (programming)|initialized]] and may contain [[cruft]]: the remnants of previously used and discarded data. After allocation with <code>malloc</code>, elements of the array are [[uninitialized variable]]s. The command <code>calloc</code> will return an allocation that has already been cleared: <syntaxhighlight lang="c" > int *array = calloc(10, sizeof(int)); </syntaxhighlight> With realloc we can resize the amount of memory a pointer points to. For example, if we have a pointer acting as an array of size <math>n</math> and we want to change it to an array of size <math>m</math>, we can use realloc. <syntaxhighlight lang="c"> int *arr = malloc(2 * sizeof(int)); arr[0] = 1; arr[1] = 2; arr = realloc(arr, 3 * sizeof(int)); arr[2] = 3; </syntaxhighlight> Note that realloc must be assumed to have changed the base address of the block (i.e. if it has failed to extend the size of the original block, and has therefore allocated a new larger block elsewhere and copied the old contents into it). Therefore, any pointers to addresses within the original block are also no longer valid.
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)