这是否有效 [英] whether this is valid
问题描述
您好
char * a =" str";
strcpy(a," abc");
>
我已经了解到,因为a是一个字符串文字,它可能在某些实现上分配在
只读内存上。
使用这种方式修改字符串文字是否正确。
Madhur Ahuja
印度
madhur< ma *** *@sdf.com>潦草地写道:Hello
char * a =" str";
strcpy(a," abc");
我已经了解到,因为a是一个字符串文字,它可能在某些实现上的只读内存上分配。
使用这种方式修改字符串文字是否正确。
不,这不对。它可能起作用或工作。在一些实施上
但这是由于特殊的补贴或纯粹的运气。
以上代码导致未定义的行为,因此永远不应该依赖
on。
-
/ - Joona Palaste(pa*****@cc.helsinki.fi)-------------芬兰-------- \
\ - http://www.helsinki.fi/~palaste ---------------------规则! -------- /
众所周知,PC的硬件很棒,但软件很糟糕。
- Petro Tyschtschenko
在文章< 2k ************* @ uni-berlin.de>," madhur" < ma **** @ sdf.com>
写道:
你好
char * a =" str" ;;
strcpy(a," abc");
我已经了解到,因为a是一个字符串文字,它可能在某些实现上的只读内存上分配。
使用这种方式修改字符串文字是否正确。
如果有人告诉你,那就开枪吧。如果你因为专业工作而被抓住了,那么你会立即被解雇。
现在请告诉我们:如果合法,为什么你会这样做?
" Christian Bau" < CH *********** @ cbau.freeserve.co.uk>在消息中写道char * a =" str";
strcpy(a," abc");
现在请告诉我们:如果它是合法的,为什么你要做>它?
char * filename =" foo" ;;
/ *许多代码使用" filename" * /
更改为规格
char * filename =" foo";
if(filenotavailable())
strcpy(filename," bar");
/ *许多代码未修改以前的版本* /
Hello
char *a="str";
strcpy(a,"abc");
I have learnt that since "a" is a string literal, it might be allocated on
read only memory on some implementations.
Is it correct to modify a string literal using this way.
Madhur Ahuja
India
madhur <ma****@sdf.com> scribbled the following:Hello char *a="str";
strcpy(a,"abc"); I have learnt that since "a" is a string literal, it might be allocated on
read only memory on some implementations.
Is it correct to modify a string literal using this way.
Not, it''s not correct. It might work or "work" on some implementations
but that''s due to either special allowances or sheer dumb luck. The
above code causes undefined behaviour and thus should never be relied
on.
--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"As we all know, the hardware for the PC is great, but the software sucks."
- Petro Tyschtschenko
In article <2k*************@uni-berlin.de>, "madhur" <ma****@sdf.com>
wrote:
Hello
char *a="str";
strcpy(a,"abc");
I have learnt that since "a" is a string literal, it might be allocated on
read only memory on some implementations.
Is it correct to modify a string literal using this way.
If anyone tells you that it is, shoot him. If you are ever caught doing
it on purpose in a professional job, you will be instantly fired.
Now please tell us: If it were legal, why on earth would you do it?
"Christian Bau" <ch***********@cbau.freeserve.co.uk> wrote in messagechar *a="str";
strcpy(a,"abc");
Now please tell us: If it were legal, why on earth would you do > it?
char *filename = "foo";
/* lots of code using "filename" */
Change to specs
char *filename = "foo";
if( filenotavailable() )
strcpy(filename, "bar");
/* lots of code unmodified from previous version */
这篇关于这是否有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!