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 syntax
(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!
====Dynamic arrays==== {{main|C dynamic memory allocation}} Arrays that can be resized dynamically can be produced with the help of the [[C standard library]]. The <code>[[malloc]]</code> function provides a simple method for allocating memory. It takes one parameter: the amount of memory to allocate in bytes. Upon successful allocation, {{code|malloc}} returns a generic ({{code|void}}) pointer value, pointing to the beginning of the allocated space. The pointer value returned is converted to an appropriate type implicitly by assignment. If the allocation could not be completed, {{code|malloc}} returns a [[null pointer]]. The following segment is therefore similar in function to the above desired declaration: <syntaxhighlight lang=C> #include <stdlib.h> /* declares malloc */ ... int *a = malloc(n * sizeof *a); a[3] = 10; </syntaxhighlight> The result is a "pointer to {{code|int}}" variable (''a'') that points to the first of ''n'' contiguous {{code|int}} objects; due to array–pointer equivalence this can be used in place of an actual array name, as shown in the last line. The advantage in using this [[dynamic allocation]] is that the amount of memory that is allocated to it can be limited to what is actually needed at run time, and this can be changed as needed (using the standard library function [[realloc|{{code|realloc}}]]). When the dynamically allocated memory is no longer needed, it should be released back to the run-time system. This is done with a call to the {{code|free}} function. It takes a single parameter: a pointer to previously allocated memory. This is the value that was returned by a previous call to {{code|malloc}}. As a security measure, some programmers {{who|date=August 2020}} then set the pointer variable to {{code|NULL}}: <syntaxhighlight lang=C> free(a); a = NULL; </syntaxhighlight> This ensures that further attempts to dereference the pointer, on most systems, will crash the program. If this is not done, the variable becomes a [[dangling pointer]] which can lead to a use-after-free bug. However, if the pointer is a local variable, setting it to {{code|NULL}} does not prevent the program from using other copies of the pointer. Local use-after-free bugs are usually easy for [[static analyzer]]s to recognize. Therefore, this approach is less useful for local pointers and it is more often used with pointers stored in long-living structs. In general though, setting pointers to {{code|NULL}} is good practice {{according to whom|date=August 2020}} as it allows a programmer to {{code|NULL}}-check pointers prior to dereferencing, thus helping prevent crashes. Recalling the array example, one could also create a fixed-size array through dynamic allocation: <syntaxhighlight lang=C> int (*a)[100] = malloc(sizeof *a); </syntaxhighlight> ...Which yields a pointer-to-array. Accessing the pointer-to-array can be done in two ways: <syntaxhighlight lang=C> (*a)[index]; index[*a]; </syntaxhighlight> Iterating can also be done in two ways: <syntaxhighlight lang=C> for (int i = 0; i < 100; i++) (*a)[i]; for (int *i = a[0]; i < a[1]; i++) *i; </syntaxhighlight> The benefit to using the second example is that the numeric limit of the first example isn't required, which means that the pointer-to-array could be of any size and the second example can execute without any modifications.
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)