2 ms·
This is the reasoning behind the "const all the things" in C and similar languages, which doesn't have distinct keywords but where you can use "const" to make t
by unwind 4y ago
This is the reasoning behind the "const all the things" in C and similar languages, which doesn't have distinct keywords but where you can use "const" to make things immutable.
If a variable is const-declared, I react in exactly the same way and know that I don't have to look for further uses, which I find very valuable.
I even do stuff like this, which might be extreme but I find it coherent and nice:
void * allocate_and_fill(size_t size, unsigned char x)
{
void * const p = malloc(size);
if (p != NULL)
memset(p, x, size);
return p;
}
The above const-declares the local pointer p, so that it can never be intentionally clobbered inside the function, which would leak the memory. Since it's the pointer itself that is constant, not the data being pointed at, there is no problem in modifying that memory, and the return value is not const-declared in any way.