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 (programming language)
(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!
=== Arrays === {{See also|C string handling}} <!-- Please be careful when editing this. C does *not* forbid bounds checking, nor does it require that pointers are memory addresses. Of course it does not require bounds checks, either, and all common implementations map those language constructs to the machine in an "obvious way", but there are ANSI-conforming implementations that handle these things in other ways. --> [[Array (data type)|Array]] types in C are traditionally of a fixed, static size specified at compile time. The more recent C99 standard also allows a form of variable-length arrays. However, it is also possible to allocate a block of memory (of arbitrary size) at run-time, using the standard library's <code>malloc</code> function, and treat it as an array. Since arrays are always accessed (in effect) via pointers, array accesses are typically ''not'' checked against the underlying array size, although some compilers may provide [[bounds checking]] as an option.<ref name="fedoraproject">For example, gcc provides _FORTIFY_SOURCE. {{cite web |url=http://fedoraproject.org/wiki/Security/Features |title=Security Features: Compile Time Buffer Checks (FORTIFY_SOURCE) |publisher=fedoraproject.org |access-date=August 5, 2012 |archive-date=January 7, 2007 |archive-url=https://web.archive.org/web/20070107153447/http://fedoraproject.org/wiki/Security/Features |url-status=live }}</ref><ref name="Programming with C">{{Cite book|title=Programming with C|last1=เอี่ยมสิริวงศ์|first1=โอภาศ|publisher=SE-EDUCATION PUBLIC COMPANY LIMITED|year=2016|isbn=978-616-08-2740-4|location=Bangkok, Thailand|pages=225–230}}</ref> Array bounds violations are therefore possible and can lead to various repercussions, including illegal memory accesses, corruption of data, [[buffer overruns]], and run-time exceptions. C does not have a special provision for declaring [[multi-dimensional array]]s, but rather relies on [[Recursion (computer science)|recursion]] within the type system to declare arrays of arrays, which effectively accomplishes the same thing. The index values of the resulting "multi-dimensional array" can be thought of as increasing in [[row-major order]]. Multi-dimensional arrays are commonly used in numerical algorithms (mainly from applied [[linear algebra]]) to store matrices. The structure of the C array is well suited to this particular task. However, in early versions of C the bounds of the array must be known fixed values or else explicitly passed to any subroutine that requires them, and dynamically sized arrays of arrays cannot be accessed using double indexing. (A workaround for this was to allocate the array with an additional "row vector" of pointers to the columns.) C99 introduced "variable-length arrays" which address this issue. The following example using modern C (C99 or later) shows allocation of a two-dimensional array on the heap and the use of multi-dimensional array indexing for accesses (which can use bounds-checking on many C compilers): <syntaxhighlight lang="c"> int func(int N, int M) { float (*p)[N] [M] = malloc(sizeof *p); if (p == 0) return -1; for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) (*p)[i] [j] = i + j; print_array(N, M, p); free(p); return 1; } </syntaxhighlight> And here is a similar implementation using C99's ''Auto [[Variable length array|VLA]]'' feature:{{efn|Code of <code>print_array</code> (not shown) slightly differs,{{why|date=November 2023}} too.}} <syntaxhighlight lang="c"> int func(int N, int M) { // Caution: checks should be made to ensure N*M*sizeof(float) does NOT exceed limitations for auto VLAs and is within available size of stack. float p[N] [M]; // auto VLA is held on the stack, and sized when the function is invoked for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) p[i] [j] = i + j; print_array(N, M, p); // no need to free(p) since it will disappear when the function exits, along with the rest of the stack frame return 1; } </syntaxhighlight>
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)