3 ms·
In this case though the length of data array is supposed to be less than n, not terminated by the first unsorted pair. Reading past the first unsorted pair is v
by Too 9y ago
In this case though the length of data array is supposed to be less than n, not terminated by the first unsorted pair. Reading past the first unsorted pair is valid as long as you don't read past n.
Is there a way to tell the compiler this? I've been trying with std::vector and std::array instead of raw pointer but without any luck. std::array also constrains you to static length.
- Too 9y agoAgain the lack of a built in array type with both data+length shows. Leaving this out of C must be the most expensive mistake in progranning history, after null. Imagine all the security holes stemming from that and now also it shows that it is preventing optimizers. At least now it's finally available with cppcoreguidelines.
- CodeArtisan 9y agohttps://port70.net/~nsz/c/c11/n1570.html#6.7.6.3p7 https://port70.net/~nsz/c/c11/n1570.html#6.7.6.3p7 "A declaration of a parameter as ''array of type'' shall be adjusted to ''qualified pointer to type'', where the type qualifiers (if any) are those specified within the [ and ] of the array type derivation. If the keyword static also appears within the [ and ] of the array type derivation, then for each call to the function, the value of the corresponding actual argument shall provide access to the first element of an array with at least as many elements as specified by the size expression." According to this, the following syntax could be used for optimization void foo(size_t n, int array[static n]) {...} pointers to array could be used too void foo(size_t n, int (*array)[n]) { printf("array %zu, *array %zu\n", sizeof(array), sizeof(*array)); } foo( 10, null); // array 8, *array 40 foo(100, null); // array 8, *array 400