这是一个好习惯吗? [英] Is this a good habit?

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

问题描述




返回指向静态数组的指针似乎很常见,因为

例如:


char * capitalize(char * name)

{

static char buf [MAX_STRING_LENGTH];


sprintf(buf, "%s",name);

if(* buf> =''a''&& * buf< =''z'')

* buf + =''A'' - '''';


返回buf;

}


但是当我做这样的事情时,我喜欢保留一个静态的

指针并在第一次使用malloc,以节省任何银行的空间

static内存存储在,因为我有这个模糊的想法,如果太多

静态内存中使用了很多空间会发生什么坏事?所以我改为

这个:


char * capitalize(char * name)

{

static char * buf;


if(!buf)

{

buf = malloc(MAX_STRING_LENGTH);

if(!buf)返回名称;

}


sprintf(buf,"%s",name);

if(* buf> =''a''&& * buf< =''z'')

* buf + =''A'' - ''a';


返回buf;

}


以下是我将两种方法分析为效率:


*第二版在精致的静态存储器中占用更少的空间。 (不确定

那叫什么)

*第一次打电话第一次快得多

*第一次稍快一点其他电话(假设第一次,

malloc工作)

*第1次不假设操作系统会在退出时进行适当的清理,

作为第二个确实

*第一个更快打字


我想知道哪种方法真的最好。我想这取决于MAX_STRING_LENGTH的大小是多少。我假设有一个人不想

随便申报数百KB的静态数组?


感谢您的帮助=)

Hi,

It seems pretty common to return pointers to a static array, for
example:

char *capitalize( char *name )
{
static char buf[MAX_STRING_LENGTH];

sprintf( buf, "%s", name );
if ( *buf >= ''a'' && *buf <= ''z'' )
*buf += ''A'' - ''a'';

return buf;
}

But when I do something like this, I like to instead keep a static
pointer and malloc it the first time, to save space in whatever bank
static memory is stored in, since I have this vague idea that if too
much space is used in static memory bad things will happen? So I do
this instead:

char *capitalize( char *name )
{
static char *buf;

if ( !buf )
{
buf = malloc( MAX_STRING_LENGTH );
if ( !buf ) return name;
}

sprintf( buf, "%s", name );
if ( *buf >= ''a'' && *buf <= ''z'' )
*buf += ''A'' - ''a'';

return buf;
}

Here is how I analyze both methods as far as efficiency:

* the 2nd version uses less space in delicate "static memory" (not sure
what that''s called)
* the 1st is much faster on the first call
* the 1st is slightly faster the other calls (assuming the first time,
malloc worked)
* the 1st does not assume the OS will do appropriate cleanup on exit,
as the 2nd does
* the 1st is quicker to type

I wonder which method is really best. I suppose it depends on how big
MAX_STRING_LENGTH is. I assume for instance one wouldn''t want to
casually declare multi-hundred-kilobyte static arrays?

Thanks for any help =)

推荐答案



Snis Pilbor写道:

Snis Pilbor wrote:




返回指向静态数组的指针似乎很常见,因为

例如:


char * capitalize(char * name)

{

static char buf [MAX_STRING_LENGTH];


sprintf(buf,"% s",name);

if(* buf> =''a''&& * buf< =''z'')

* buf + =''A'' - '''';


返回buf;

}
Hi,

It seems pretty common to return pointers to a static array, for
example:

char *capitalize( char *name )
{
static char buf[MAX_STRING_LENGTH];

sprintf( buf, "%s", name );
if ( *buf >= ''a'' && *buf <= ''z'' )
*buf += ''A'' - ''a'';

return buf;
}



恕我直言,在函数内部使用静态变量并不是一个好主意。你的
最终会得到非线程安全的代码。如果多个并发运行的线程正在使用静态变量调用

函数,该函数将会发生错误行为。


-

VisualEther协议分析器1.0 -

http :: //www.EventHelix.com/VisualEther

Ethereal的可视化协议分析和序列图生成




EventHelix.com写道:

EventHelix.com wrote:

Snis Pilbor写道:
Snis Pilbor wrote:




返回指向静态数组的指针似乎很常见,因为

例如:


char * capitalize(char * name)

{

static char buf [MAX_STRING_LENGTH];


sprintf(buf,"%s",name);

if(* buf> =''a''&& * buf< = ''z'')

* buf + =''A'' - '''';


返回buf;

}
Hi,

It seems pretty common to return pointers to a static array, for
example:

char *capitalize( char *name )
{
static char buf[MAX_STRING_LENGTH];

sprintf( buf, "%s", name );
if ( *buf >= ''a'' && *buf <= ''z'' )
*buf += ''A'' - ''a'';

return buf;
}



恕我直言,在函数内部使用静态变量并不是一个好主意。你的
最终会得到非线程安全的代码。如果多个并发运行的线程正在使用静态变量调用

函数,该函数将会错误地运行


IMHO, Use of static variables inside functions is not a good idea. You
will end up with code that is not thread safe. The function will
misbehave if multiple concurrently running threads are invoking the
function with static variables.



是当然,在任何一种情况下都必须行使

谨慎。

恕我直言,关于C最美丽的事情之一就是它可以让你

这样的灵活性。它并不认为你是一个婴儿并持有你的b $ b手。

如果我想要一种语言,各种各样的人告诉我哦,你不能这样做/>
那个,它太危险了,我会编程Java =)无论如何,谢谢你提出

的警告。

我会的在时间敏感的情况下继续返回指向静态数组的指针

函数因为我是,而那些在我之下工作的人,足够聪明

知道我们在做什么。 =)

Yes, of course, it goes without saying in either case one must exercise
caution.
IMHO one of the most beautiful things about C is that it allows you
such flexibility. It does not assume you are a baby and hold your
hand.
If I wanted a language where various people told me "Oh you can''t do
that, it''s too dangerous", I would program Java =) Thanks for the word
of warning anyway, though.
I will continue to return pointers to static arrays in time sensitive
functions because I am, and those who work under me are, smart enough
to know what we''re doing. =)


Snis Pilbor< sn ******** @ yahoo.comwrote:
Snis Pilbor <sn********@yahoo.comwrote:

恕我直言,关于C最美丽的事情之一就是它可以让你获得这样的灵活性。它并不认为你是一个婴儿,并持有你的b $ b手。
IMHO one of the most beautiful things about C is that it allows you
such flexibility. It does not assume you are a baby and hold your
hand.



好​​吧,有些人会不同意你的看法,但有些人不好意思C

程序员:-)

Well, some would disagree with you, but then again, some are bad C
programmers :-)


我将继续返回时间敏感的指向静态数组的指针

函数,因为我是,而那些在我之下工作的人,足够聪明

知道我们在做什么。 =)
I will continue to return pointers to static arrays in time sensitive
functions because I am, and those who work under me are, smart enough
to know what we''re doing. =)



我没有嵌入式平台的经验(听起来你好像是b
$ b),但是来自其他发布到这个群组似乎是动态内存而不是静态内存最为昂贵。如果你b $ b bb的自动存储空间有限,并且你经常做这类事情,你可能会想到使用全球联盟而不是

几个静态数组;这可以为你节省更多空间。


-

C. Benson Manica |我*应该*知道我在说什么 - 如果我

cbmanica(at)gmail.com |不,我需要知道。火焰欢迎。

I don''t have experience on embedded platforms (it sounds like you are
working on one), but from other posts to this group it seems that
dynamic memory rather than static memory is most at a premium. If you
have limited automatic storage, and you do this sort of thing
frequently, you might think about using a global union rather than
several static arrays; that could save you some more space.

--
C. Benson Manica | I *should* know what I''m talking about - if I
cbmanica(at)gmail.com | don''t, I need to know. Flames welcome.


这篇关于这是一个好习惯吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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