这是允许的吗? [英] is this allowed in c ?

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

问题描述

char * getdata(char * str)

{

char buffer [200];

sprintf(缓冲区,我们是这里%s",str);

返回(str);

}


可以调用此函数并使用缓冲区返回或者这是非法的吗?


问候,

-chip

char *getdata(char *str)
{
char buffer[200];
sprintf( buffer, "We are here %s", str );
return (str);
}

Can one call this function and use the buffer returned or is this illegal ?

Regards,
-chip

推荐答案

chip写道:
chip wrote:
可以调用这个函数并使用返回的缓冲区还是这个
非法?
Can one call this function and use the buffer returned or is this
illegal ?




非常非常非法。这是你可以用C / C ++吹掉你的腿的众多方法之一




-

gabriel



Very, very illegal. This is one of the many ways you can blow your leg off
in C/C++.

--
gabriel




" chip" <无**** @ hotmail.com>在留言中写道

news:yQ ***************** @ news2.e.nsc.no ...

"chip" <no****@hotmail.com> wrote in message
news:yQ*****************@news2.e.nsc.no...
char * getdata(char * str)
{char buffer [200];
sprintf(缓冲区,我们在这里%s,str);
return(str );
}

可以调用此函数并使用返回的缓冲区或者这是非法的
char *getdata(char *str)
{
char buffer[200];
sprintf( buffer, "We are here %s", str );
return (str);
}

Can one call this function and use the buffer returned or is this illegal






这存储在缓冲区中。你的代码也是非常不安全的。你可以

使用


char * getdata(const char * str)

{

static char buffer [200];


memset(缓冲区,0,sizeof(缓冲区));

strncpy(缓冲区,我们在这里, sizeof(缓冲区)-1);

strncat(缓冲区,str,sizeof(缓冲区)-1);


返回缓冲区;

}


除非这不是线程安全的[例如调用它的两个线程将munge

函数返回值。

Tom


?

This stores in "buffer" and you''re code is horribly insecure too. You could
use

char *getdata(const char *str)
{
static char buffer[200];

memset(buffer, 0, sizeof(buffer));
strncpy(buffer, "We are here ", sizeof(buffer)-1);
strncat(buffer, str, sizeof(buffer)-1);

return buffer;
}

Except this is not thread safe [e.g. two threads that call this will munge
the function return value.

Tom


Tom St Denis写道:
Tom St Denis wrote:
" chip" <无**** @ hotmail.com>在消息中写道
新闻:yQ ***************** @ news2.e.nsc.no ...
"chip" <no****@hotmail.com> wrote in message
news:yQ*****************@news2.e.nsc.no...
char * getdata(char * str)
{char buffer [200];
sprintf(缓冲区,我们在这里%s,str);
return(str );
}

可以调用此函数并使用返回的缓冲区或这是非法的吗?

这存储在缓冲区中你的代码也是非常不安全的。你可以使用

char * getdata(const char * str)
{
静态字符缓冲区[200];

memset( buffer,0,sizeof(buffer));
char *getdata(char *str)
{
char buffer[200];
sprintf( buffer, "We are here %s", str );
return (str);
}

Can one call this function and use the buffer returned or is this illegal
?

This stores in "buffer" and you''re code is horribly insecure too. You could
use

char *getdata(const char *str)
{
static char buffer[200];

memset(buffer, 0, sizeof(buffer));



为什么?

strncpy(缓冲区,我们在这里,sizeof(缓冲区)-1 );
为什么不直接使用strcpy()? strncat(buffer,str,sizeof(buffer)-1);


错误。如果strlen(str)> sizeof(缓冲区) - 1 - strlen(我们在这里)?
返回缓冲区;


除非这不是线程安全的[例如调用它的两个线程将munge
函数返回值。


Why ?
strncpy(buffer, "We are here ", sizeof(buffer)-1); Why not just use strcpy() ? strncat(buffer, str, sizeof(buffer)-1);
Bug. What if strlen(str) > sizeof(buffer) - 1 - strlen("We are here ") ?
return buffer;
}

Except this is not thread safe [e.g. two threads that call this will munge
the function return value.




Bj?rn


PS:原始版本是线程安全的;-)

-

世界上最快的网络服务器现在可用

at http://highlander.metasystems.no:2000 。享受!



Bj?rn

PS: The original version was thread safe ;-)
--
The worlds fastest web server is now available
at http://highlander.metasystems.no:2000. Enjoy!


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

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