clc_wprealloc [英] clc_wprealloc

查看:61
本文介绍了clc_wprealloc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算为clc库提出这个功能


void * clc_wprealloc(void * buff,size_t size)

{

void * answer;


answer = realloc(buff,size);

if(!answer)

免费(buff);

返回答案;

}


wp代表防水。你可能会问,这有什么意义呢?点数

是10次中的9次,如果系统不允许你调整数组的大小,那么你工作的是
没有选择,只能释放它,经常终止程序,

或者中止函数并返回更高级别。


但是,因为realloc()不会释放buff它无法调整大小,你需要一个暂时的尴尬舞蹈来避免内存泄漏 - 这可能不会发生在现实中,因为你在问在一个有4GB交换空间的系统上用几个字节来保存

字符,但你仍然希望

程序是正确的。


clc_wprealloc()必须是广泛使用的库的一部分,如clc将

成为。将其添加到用户代码中会产生依赖关系并且会混淆

人们为什么没有调用标准的realloc()。

I am going to propose this function for the clc library

void *clc_wprealloc(void *buff, size_t size)
{
void *answer;

answer = realloc(buff, size);
if(!answer)
free(buff);
return answer;
}

wp stands for waterproof. What''s the point of this, you might ask. The point
is that, 9 times out of 10, if system won''t let you resize the array you are
working on there is no option but to free it, often terminate the program,
or else abort the function and return to a higher level.

However, since realloc() won''t free the buff it fails to resize, you need an
awkward dance with a temporary to avoid a memory leak - which probably can''t
happen in reality anyway because you are asking for a few bytes to hold
characters on a system with 4GB of swap space, but you still want the
program to be correct.

clc_wprealloc() has to be part of a widely-used library like clc will
become. Adding it into user code would create dependencies and confuse
people as to why standard realloc() wasn''t being called.

推荐答案

2003年7月15日星期二20:15:20 +0100,Malcolm

< ma ***** @ 55bank.freeserve.co.uk>写道:
On Tue, 15 Jul 2003 20:15:20 +0100, "Malcolm"
<ma*****@55bank.freeserve.co.uk> wrote:
我打算为clc库提出这个函数

void * clc_wprealloc(void * buff,size_t size)
{
void * answer;

answer = realloc(buff,size);
if(!answer)
free(buff);
返回答案;
}

wp代表防水。
??好的功能名称??


clc_realloc_or_perish()似乎更好。

你可能会问,这有什么意义。要点是,10次中有9次,如果系统不允许你调整阵列的大小,那么你正在工作,没有选择,只能释放它,经常终止程序,
或者中止函数并返回更高级别。


YMMV,但我通常会发现除了释放

缓冲区以外我想要做的事情并扔掉我已有的数据。
但是,因为realloc()不会释放buff它无法调整大小,你需要一个暂时的尴尬舞蹈以避免内存泄漏 - 这可能不会在现实中发生,因为无论如何你要求几个字节来保存具有4GB交换空间的系统上的字符,但你仍然希望
程序是正确的。

clc_wprealloc()必须成为广泛使用的图书馆的一部分,如clc将成为。将它添加到用户代码中会产生依赖关系并使人们混淆为什么没有调用标准的realloc()。
I am going to propose this function for the clc library

void *clc_wprealloc(void *buff, size_t size)
{
void *answer;

answer = realloc(buff, size);
if(!answer)
free(buff);
return answer;
}

wp stands for waterproof. ?? Good functional name ??

clc_realloc_or_perish() would seem better.
What''s the point of this, you might ask. The point
is that, 9 times out of 10, if system won''t let you resize the array you are
working on there is no option but to free it, often terminate the program,
or else abort the function and return to a higher level.
YMMV, but I usually find I want to do something other than free the
buffer and throw away the data I already have.
However, since realloc() won''t free the buff it fails to resize, you need an
awkward dance with a temporary to avoid a memory leak - which probably can''t
happen in reality anyway because you are asking for a few bytes to hold
characters on a system with 4GB of swap space, but you still want the
program to be correct.

clc_wprealloc() has to be part of a widely-used library like clc will
become. Adding it into user code would create dependencies and confuse
people as to why standard realloc() wasn''t being called.




-

Al Balmer

Balmer Consulting
re ************************ @ att.net


Malcolm写道:
Malcolm wrote:
我将为clc库提出这个函数

void * clc_wprealloc(void * buff,size_t大小)
{
无效*答案;

答案= realloc(buff,size);
if(!answer)
free(buff);
返回答案;
}
wp代表防水。你可能会问,这有什么意义呢?
点是,10次中的9次,如果系统不允许你调整阵列的大小
你正在努力,除了释放它之外别无选择,经常终止
程序,或者中止函数并返回到更高级别。
I am going to propose this function for the clc library

void *clc_wprealloc(void *buff, size_t size)
{
void *answer;

answer = realloc(buff, size);
if(!answer)
free(buff);
return answer;
}

wp stands for waterproof. What''s the point of this, you might ask. The
point is that, 9 times out of 10, if system won''t let you resize the array
you are working on there is no option but to free it, often terminate the
program, or else abort the function and return to a higher level.




当内存失败时该怎么办(从< cough>中剔除一个简短的摘要,调用

我对文献的贡献):


1)终止程序。适用于学生代码。


2)将内存需求分解为更小的块。


3)使用更少的内存。


4)使用固定长度的缓冲区。


5)预先分配紧急储备。


6 )使用虚拟磁盘空间(可能在网络上的另一台机器上)。


确实有很多选择。你不一定要放弃,因为

一次分配失败。


< snip>


-

Richard Heathfield: bi****@eton.powernet.co.uk

Usenet是一个奇怪的地方。 - Dennis M Ritchie,1999年7月29日。

C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

K& R答案,C书等:< a rel =nofollowhref =http://users.powernet.co.uk/etontarget =_ blank> http://users.powernet.co.uk/eton



What to do when memory fails (a short summary culled from <cough> well, call
it my contribution to "the literature"):

1) Terminate the program. Fine for student code.

2) Break down the memory requirement into smaller chunks.

3) Use less memory.

4) Use a fixed length buffer.

5) Pre-allocate an emergency reserve.

6) Use virtual disk space (perhaps on another machine over a network).

Lots of options really. You don''t necessarily have to give up just because
one allocation failed.

<snip>

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton


在文章< bf ********** @ news8.svr.pol.co.uk>,

Malcolm< ma *****@55bank.freeserve.co.uk>写道:
In article <bf**********@news8.svr.pol.co.uk>,
Malcolm <ma*****@55bank.freeserve.co.uk> wrote:
我将为clc库提出这个函数

void * clc_wprealloc(void * buff,size_t size)
{
void *回答;

回答= realloc(buff,size);
如果(!回答)
免费(buff);
返回答案;
}

wp代表防水。你可能会问,这有什么意义呢?要点是,10次中有9次,如果系统不允许你调整阵列的大小,那么你正在工作,没有选择,只能释放它,经常终止程序,
或者中止该函数并返回更高级别。
I am going to propose this function for the clc library

void *clc_wprealloc(void *buff, size_t size)
{
void *answer;

answer = realloc(buff, size);
if(!answer)
free(buff);
return answer;
}

wp stands for waterproof. What''s the point of this, you might ask. The point
is that, 9 times out of 10, if system won''t let you resize the array you are
working on there is no option but to free it, often terminate the program,
or else abort the function and return to a higher level.




这很好,但有一点需要注意:在realloc()失败和释放后( )

被调用,errno的值可能是未定义的。因此,clc_wprealloc()的调用者不应该尝试使用

perror()来打印失败消息,就像他在直接调用realloc()之后所做的那样。 br />

-

Rouben Rostamian< ro ******* @ umbc.edu>



That''s good, but there is one caveat: after realloc() fails and free()
is called, the value of errno may be undefined. Therefore the caller
of clc_wprealloc() should not attempt to print a failure message with
perror() as he would have done after a direct call to realloc().

--
Rouben Rostamian <ro*******@umbc.edu>


这篇关于clc_wprealloc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆