C nginx!如果将一个元素推入数组并且旧数组已满,如何释放旧数组内存?
请看下面的源代码C nginx!如果将一个元素推入数组并且旧数组已满,如何释放旧数组内存?,c,nginx,C,Nginx,请看下面的源代码 void *ngx_array_push(ngx_array_t *a) { void *elt, *new; size_t size; ngx_pool_t *p; if (a->nelts == a->nalloc) { /* the array is full */ size = a->size * a->nalloc; p = a-&
void *ngx_array_push(ngx_array_t *a)
{
void *elt, *new;
size_t size;
ngx_pool_t *p;
if (a->nelts == a->nalloc) {
/* the array is full */
size = a->size * a->nalloc;
p = a->pool;
if ((u_char *) a->elts + size == p->d.last
&& p->d.last + a->size <= p->d.end)
{
/*
* the array allocation is the last in the pool
* and there is space for new allocation
*/
p->d.last += a->size;
a->nalloc++;
} else {
/* allocate a new array */
new = ngx_palloc(p, 2 * size);
if (new == NULL) {
return NULL;
}
ngx_memcpy(new, a->elts, size);
a->elts = new; //This place
a->nalloc *= 2;
}
}
elt = (u_char *) a->elts + a->size * a->nelts;
a->nelts++;
return elt;
}
void*ngx\u array\u push(ngx\u array\u t*a)
{
无效*elt,*新;
大小;
ngx_pool_t*p;
如果(a->nelts==a->nalloc){
/*数组已满*/
尺寸=a->尺寸*a->nalloc;
p=a->pool;
如果((u_char*)a->elts+size==p->d.last
&&p->d.last+a->尺寸d.end)
{
/*
*阵列分配是池中的最后一个
*还有新的分配空间
*/
p->d.last+=a->尺寸;
a->nalloc++;
}否则{
/*分配一个新数组*/
新=ngx_palloc(p,2*尺寸);
if(new==NULL){
返回NULL;
}
ngx_memcpy(新,a->elts,尺寸);
a->elts=new;//这个地方
a->nalloc*=2;
}
}
elt=(u_char*)a->elts+a->size*a->nelts;
a->nelts++;
返回英语教学;
}
变量“new”是一个新的内存!a->elts指向旧内存。根据“a->elts=new;”,a->elts指向新内存。但是,旧内存不会释放。如果不考虑旧内存发布?不应该调用NGXFLY(A->ELTS)之类的东西;在memcpy之后?