constness of key in ngx_hash_find_

Igor Sysoev is at rambler-co.ru
Tue Oct 21 16:32:45 MSD 2008


On Mon, Oct 20, 2008 at 05:56:24PM +0530, Arvind Jayaprakash wrote:

> 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.

OK, I will change this eventually.


-- 
Igor Sysoev
http://sysoev.ru/en/





More information about the nginx mailing list