constness of key in ngx_hash_find_

Arvind Jayaprakash work at anomalizer.net
Mon Oct 20 16:26:24 MSD 2008


Igor Sysoev wrote:
> Do you mean
> 
> -void *ngx_hash_find(ngx_hash_t *hash, ..., u_char *name,
> +void *ngx_hash_find(const ngx_hash_t *hash, ..., const u_char *name,
> 
> But what will this improve ?

This is more of an API design/style question. When passing pointers, if 
the function signature declares the argument to be "const type *ptr", it 
tells me that the function will not modify the contents of the variable 
being passed. For eg: A hash lookup function should never need to modify 
either the hash or the key being searched for.

If a function violates this contract, then it results in a compile time 
error. If someone tries to forcibly discard the constness using a 
typecast, it will result in a warning.

Having the const declarations thus gives people the assurance that the 
values being passed will not be modified inside the function. Without 
that, I am not sure if the value will get modified unless I read the 
implementation.





More information about the nginx mailing list