realloc返回值

编程入门 行业动态 更新时间:2024-10-27 23:30:02
本文介绍了realloc返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我从K& K中获取了以下原型R. void * realloc(void * p,size_t size); 假设p先前由malloc分配。假设我调用realloc ,大小值更大。 如果realloc成功,返回指针是否与p相同或 将它是不同的。 K& R第2版说realloc返回指向新空间的指针。 为什么我要问的是,如果它们不同,那么旧的指针值 $ b $ p的b应该被释放。 谢谢

I have taken the following prototype from K & R. void *realloc(void *p, size_t size); Suppose p was earlier allocated by malloc. Suppose I am calling realloc with larger size value. If realloc is successful, will the return pointer be the same as p or will it be different. K & R 2nd edition says "realloc returns a pointer to the new space". Why I am asking is that if they are different, the older pointer value of p should be freed. Thanks

推荐答案

subramanianaécrit: subramanian a écrit : 我从K& K中获取了以下原型R. void * realloc(void * p,size_t size); 假设p先前由malloc分配。假设我调用realloc ,大小值更大。 如果realloc成功,返回指针是否与p相同或 将它是不同的。 K& R第2版说realloc返回指向新空间的指针。 为什么我要问的是,如果它们不同,那么旧的指针值 $ b $ p的b应该被释放。 谢谢 I have taken the following prototype from K & R. void *realloc(void *p, size_t size); Suppose p was earlier allocated by malloc. Suppose I am calling realloc with larger size value. If realloc is successful, will the return pointer be the same as p or will it be different. K & R 2nd edition says "realloc returns a pointer to the new space". Why I am asking is that if they are different, the older pointer value of p should be freed. Thanks

你不必释放旧值。 realloc将为你做这件事

You do not have to free the old value. realloc will do it for you

subramanian说: subramanian said: 我从K& K中获取了以下原型R. void * realloc(void * p,size_t size); 假设p先前由malloc分配。假设我调用realloc ,大小值更大。 如果realloc成功,返回指针是否与p相同或 将它是不同的。 I have taken the following prototype from K & R. void *realloc(void *p, size_t size); Suppose p was earlier allocated by malloc. Suppose I am calling realloc with larger size value. If realloc is successful, will the return pointer be the same as p or will it be different.

* *可能*不同。这取决于realloc是否重新定位块 (以及realloc是否成功)。如果它确实重新定位了块,那么p'的值现在是不确定的,不应该使用。如果失败, realloc将返回NULL。见下文。

It *may* be different. That depends on whether realloc relocated the block (and also on whether realloc succeeded). If it did relocate the block, p''s value is now indeterminate and should not be used. And if it failed, realloc will return NULL. See below.

K& R第2版说realloc返回指向新空间的指针。 为什么我要问的是,如果它们不同,那么旧的指针值 $ b $ p的b应该被释放。 K & R 2nd edition says "realloc returns a pointer to the new space". Why I am asking is that if they are different, the older pointer value of p should be freed.

不,如果有必要,realloc将为您处理。 请注意,realloc可能*失败* ,在这种情况下,它将返回NULL。所以不要这样做:这个: p = realloc(p,new_size); /* 坏狗!没有BISCUIT! * / 相反,这样做: tmp = realloc(p,new_size); if( tmp!= NULL) { p = tmp; tmp = NULL; } 其他 { 重新分配失败,但至少p仍然指向 旧的,未触及的内存块,所以你没有*丢失*任何记忆 } - Richard Heathfield Usenet是一个奇怪的地方 - dmr 29/7/1999 www.cpax.uk 电子邮件:rjh在上述域名中, - www。

No, realloc will take care of that for you if it is necessary. Note that realloc may *fail*, in which case it will return NULL. So don''t do this: p = realloc(p, new_size); /* BAD DOG! NO BISCUIT! */ Instead, do this: tmp = realloc(p, new_size); if(tmp != NULL) { p = tmp; tmp = NULL; } else { the reallocation failed, but at least p still points to the old, untouched memory block, so you haven''t *lost* any memory } -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 www.cpax.uk email: rjh at the above domain, - www.

subramanian写道: subramanian wrote: 我从K& K中获取了以下原型R. void * realloc(void * p,size_t size); 假设p先前由malloc分配。假设我调用realloc ,大小值更大。 如果realloc成功,返回指针是否与p相同或 将它是不同的。 I have taken the following prototype from K & R. void *realloc(void *p, size_t size); Suppose p was earlier allocated by malloc. Suppose I am calling realloc with larger size value. If realloc is successful, will the return pointer be the same as p or will it be different.

我们不知道,我们也不需要关心。如果它不是空的, 重新分配工作(并且可以想象通过增加 已分配的内存,AFAIK),旧数据已经(如有必要) 复制到新位置。如果旧的基地址和新的基地 地址不同,之前分配的空间将被释放。

We don''t know, and we don''t need to care. If it''s not null, the reallocation worked (and that could conceivably be by growing the allocated memory in place, AFAIK), the old data has been (if necessary) copied into the new location. If the old base address and the new base address are different, the previously allocated space will have been freed.

K& R第2版说realloc返回指向新空间的指针。 K & R 2nd edition says "realloc returns a pointer to the new space".

为什么我要问的是,如果它们不同,应该释放p的旧指针值 。 Why I am asking is that if they are different, the older pointer value of p should be freed.

但不是你。这是realloc()的工作。

But not by you. That''s realloc()''s job.

更多推荐

realloc返回值

本文发布于:2023-10-26 14:41:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1530440.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:返回值   realloc

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!