1.20. ARRAYS
This mechanism is probably the same in all compilers. Here is what GCC does:
Listing 1.243: Optimizing GCC 4.9 x64
.LC1:
.string "month.c"
.LC2:
.string "month<12"
get_month1_checked:
cmp edi, 11
jg .L6
movsx rdi, edi
mov rax, QWORD PTR month1[0+rdi*8]
ret
.L6:
push rax
mov ecx, OFFSET FLAT:__PRETTY_FUNCTION.2423
mov edx, 29
mov esi, OFFSET FLAT:.LC1
mov edi, OFFSET FLAT:.LC2
call assert_fail
__PRETTY_FUNCTION__.2423:
.string "get_month1_checked"
So the macro in GCC also passes the function name for convenience.
Nothing is really free, and this is true for the sanitizing checks as well.
They make your program slower, especially if the assert() macros used in small time-critical functions.
So MSVC, for example, leaves the checks in debug builds, but in release builds they all disappear.
MicrosoftWindows NTkernels come in “checked” and “free” builds^142.
The first has validation checks (hence, “checked”), the second one doesn’t (hence, “free” of checks).
Of course, “checked” kernel works slower because of all these checks, so it is usually used only in debug
sessions.
Accessing specific character
An array of pointers to strings can be accessed like this:
#include <stdio.h>
const char* month[]=
{
"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"
};
int main()
{
// 4th month, 5th character:
printf ("%c\n", month[3][4]);
};
...sincemonth[3]expressionhasaconstchar*type. Andthen, 5thcharacteristakenfromthatexpression
by adding 4 bytes to its address.
By the way, arguments list passed tomain()function has the same data type:
#include <stdio.h>
int main(int argc, char *argv[])
{
printf ("3rd argument, 2nd character: %c\n", argv[3][1]);
(^142) msdn.microsoft.com/en-us/library/windows/hardware/ff543450(v=vs.85).aspx