修改静态数据好吗? [英] Modify Static Data Okay?

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

问题描述

修改静态数据是否合法,如下面的代码?


#include< stdlib.h>

#include< stdio.h> ;


struct tbl {

int i;

char * s;

};


struct tbl t [] = {

{10," hello" },

{12," yoyoyoyoyoyoyoyoyoyo" }

};


int

main(无效)

{

t [0] .s =" something else" ;; / *修改静态表格* /


printf("%d%s \ n",t [0] .i,t [0] .s);


返回EXIT_SUCCESS;

}


Mike

Is it legit to modify static data like the following code?

#include <stdlib.h>
#include <stdio.h>

struct tbl {
int i;
char *s;
};

struct tbl t[] = {
{ 10, "hello" },
{ 12, "yoyoyoyoyoyoyoyoyoyo" }
};

int
main(void)
{
t[0].s = "something else"; /* modify the static table in place */

printf("%d %s\n", t[0].i, t[0].s);

return EXIT_SUCCESS;
}

Mike

推荐答案

在文章< pa ********************************* @ ioplex.com> ; ,

Michael B Allen< mb ***** @ ioplex.com>写道:
In article <pa*********************************@ioplex.com> ,
Michael B Allen <mb*****@ioplex.com> wrote:
修改静态数据是否合法,如下面的代码?


< snippage>

[文件范围] struct tbl {
int i;
char * s;
结构tbl t [] = {
{10," hello" },
{12," yoyoyoyoyoyoyoyoyoyo" }
};
[函数内部] t [0] .s =" something else" ;; / *修改静态表* /
Is it legit to modify static data like the following code?
<snippage>
[file scope]struct tbl {
int i;
char *s;
};

struct tbl t[] = {
{ 10, "hello" },
{ 12, "yoyoyoyoyoyoyoyoyoyo" }
}; [inside a function] t[0].s = "something else"; /* modify the static table in place */



是的。除非它是const限定的或字符串文字,否则静态数据

总是可写的。


注意你初始化t []的指针.s with指向字符串

literals,所以你必须指出它们之前可写的东西

你可以通过它们来写。

dave


-

Dave Vandervies dj ****** @ csclub.uwaterloo.ca

我当然授予我们的任务,如果可以的话,我们的任务会更简单

订购理想组件库存。

- 威廉迈耶在comp.lang.c


Yes. Unless it''s const-qualified or a string literal, static data
is always writeable.

Note that the pointers you initialize t[].s with are pointing at string
literals, so you''d have to point them at something writeable before
you''re allowed to write through them.
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
I grant, of course, that our tasks would be much simpler if we could
order up a stock of ideal components.
--William Meyer in comp.lang.c


周五,2004年11月12日17:33 :30 -0500,Dave Vandervies写道:
On Fri, 12 Nov 2004 17:33:30 -0500, Dave Vandervies wrote:
struct tbl t [] = {
{10," hello" },
{12," yoyoyoyoyoyoyoyoyoyo" }
}; [函数内部]
struct tbl t[] = {
{ 10, "hello" },
{ 12, "yoyoyoyoyoyoyoyoyoyo" }
}; [inside a function]
t [0] .s =" something else" ;; / *修改静态表* /
t[0].s = "something else"; /* modify the static table in place */



是的。除非它是const限定的或字符串文字,否则静态数据总是可写的。


Yes. Unless it''s const-qualified or a string literal, static data is
always writeable.




好​​的。谢谢。

请注意,你初始化t [] .s的指针指向字符串
文字,因此你必须指出它们之前可写的东西
你''允许通过它们写。



Okay. Thanks.
Note that the pointers you initialize t[].s with are pointing at string
literals, so you''d have to point them at something writeable before
you''re allowed to write through them.




当然。我只是在谈论修改指针。实际上实际上是

我只是在每个元素的第一个int成员中添加一个常量。


谢谢,

Mike



Sure. I''m only talking about modifying the pointer. Actually in truth
I''m just adding a constant to the first int member of each element.

Thanks,
Mike


2004年11月12日星期五18:26:55 -0500,Michael B Allen

< mb ***** @ ioplex.com>写道:
On Fri, 12 Nov 2004 18:26:55 -0500, Michael B Allen
<mb*****@ioplex.com> wrote:
修改静态数据是否合法,如下面的代码?


您的代码不会修改静态数据。

#include< stdlib.h>
#include< stdio.h>

struct tbl {
int i;
char * s;
};

struct tbl t [] = {
{ 10,你好 },
{12," yoyoyoyoyoyoyoyoyoyo" }
};

int
main(无效)
{
t [0] .s ="别的东西" ;; / *修改静态表* /


这不会修改hello。它只修改t [0] .s,以便它b / b
指向内存中的不同位置。


如果你使用了strcpy,例如,

strcpy(t [0] .s,其他东西);

那么你会试图修改你好。这将是
调用未定义的行为,不是因为任何东西都是静态的,而是因为

试图修改字符串文字,具体描述为

调用未定义的行为。

printf("%d%s \ n",t [0] .i,t [0] .s);

返回EXIT_SUCCESS;
}
Is it legit to modify static data like the following code?
Your code does not modify static data.

#include <stdlib.h>
#include <stdio.h>

struct tbl {
int i;
char *s;
};

struct tbl t[] = {
{ 10, "hello" },
{ 12, "yoyoyoyoyoyoyoyoyoyo" }
};

int
main(void)
{
t[0].s = "something else"; /* modify the static table in place */
This does not modify the "hello". It only modifies t[0].s so that it
points to a different location in memory.

If you had used strcpy, e.g.,
strcpy(t[0].s,"something else");
then you would have attempted to modify the "hello". This would
invoke undefined behavior, not because anything is static but because
attempting to modify a string literal is specifically described as
invoking undefined behavior.

printf("%d %s\n", t[0].i, t[0].s);

return EXIT_SUCCESS;
}




static关键字描述了对象的生命周期,而不是它是否可以修改为b $ b。这两个属性是无关的。修改静态

对象不是问题。修改const对象是违规行为。如上所述,修改某些非常量对象(例如字符串

文字)也是违规行为。

<<删除电子邮件的del>>



The static keyword describes the life of an object, not whether it is
modifiable. The two attributes are unrelated. Modifying a static
object is not a problem. Modifying a const object is a violation. As
noted above, modifying certain non-const objects (such as string
literals) is also a violation.
<<Remove the del for email>>


这篇关于修改静态数据好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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