What is the best way to reallocate memory for char array?

kay nginx-forum at nginx.us
Mon Nov 17 14:35:18 UTC 2014


Thanks.

Is it necessary to use ngx_pfree(str) at the end of my function?

Maxim Dounin Wrote:
-------------------------------------------------------
> Hello!
> 
> On Mon, Nov 17, 2014 at 09:09:34AM -0500, kay wrote:
> 
> > For example I've allocated memory for char *str:
> > 
> > str = ngx_pnalloc(r->pool, len1);
> > 
> > Then I've added some data to "str":
> > 
> > ngx_sprintf(str, "abc");
> > 
> > How should I reallocate this memory? Should I use:
> > 
> > str = ngx_pnalloc(r->pool, len2);
> > 
> > ? And I would like to keep the str's "abc" data as well.
> 
> Best strategy is to allocate len1 + len2 from the start.  E.g., 
> you may try looking into autoindex module: it calculates size of a 
> buffer it will need, and then allocates the buffer.
> 
> -- 
> Maxim Dounin
> http://nginx.org/
> 
> _______________________________________________
> nginx mailing list
> nginx at nginx.org
> http://mailman.nginx.org/mailman/listinfo/nginx

Posted at Nginx Forum: http://forum.nginx.org/read.php?2,254851,254854#msg-254854



More information about the nginx mailing list