结构vs字符串文字?只读还是读写? [英] Struct vs string literals? Read only vs read-write?

查看:148
本文介绍了结构vs字符串文字?只读还是读写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C99标准是否允许写入复合文字(结构)?似乎它不提供对文字字符串的写入.我问这个问题是因为它在 C编程:现代方法,第二版第406页.

Does the C99 standard permit writing to compound literals (structs)? It seems it doesn't provide writing to literal strings. I ask about this because it says in C Programming: A Modern Approach, 2nd Edition on Page 406.

问允许指向复合文字的指针似乎使修改文字成为可能.是这样吗?

Q. Allowing a pointer to a compound literal would seem to make it possible to modify the literal. Is that the case?

A.是的.复合文字是可以修改的左值.

A. Yes. Compound literals are lvalues that can be modified.

但是,我不太了解它是如何工作的,以及它如何与您肯定无法修改的字符串文字一起工作.

But, I don't quite get how that works, and how that works with string literals which you certainly can't modify.

char *foo = "foo bar";
struct bar { char *a; int g; };
struct bar *baz = &(struct bar){.a = "foo bar", .g = 5};

int main () {
  // Segfaults
  // (baz->a)[0] = 'X';
  // printf( "%s", baz->a );

  // Segfaults
  // foo[0] = 'a';
  // printf("%s", foo);

  baz->g = 9;
  printf("%d", baz->g);

  return 0;
}

您可以在我的段错误列表中看到,写入baz->a会导致段错误.但是,写入baz->g不会.为什么其中一个会引起段错误而不是另一个?结构文字与字符串文字有何不同?为什么不将struct-literal放入内存的只读部分,并且为这两种行为(标准问题)都定义还是未定义行为?

You can see on my list of things that segfault, writing to baz->a causes a segfault. But, writing to baz->g does not. Why is that one of them would cause a segfault and not the other one? How are struct-literals different from string-literals? Why would struct-literals not also be put into read-only section of memory and is the behavior defined or undefined for both of these (standards question)?

推荐答案

第一件事:您的struct文字有一个初始化为字符串文字的指针成员.结构本身的成员是可写的,包括指针成员.只是字符串文字的内容是不可写的.

First thing first: your struct literal has a pointer member initialized to a string literal. The members of the struct itself are writeable, including the pointer member. It is only the content of the string literal that is not writeable.

字符串文字一直是该语言的一部分,而从C99开始,结构文字(正式称为 compoundliteral )是相对较新的添加.到那时,存在许多将字符串文字放置在只读存储器中的实现,尤其是在具有少量RAM的嵌入式系统上.到那时,该标准的设计者可以选择将字符串文字移至可写位置,允许结构文字为只读,或保持原样.三种解决方案都不是理想的,因此看起来它们走在阻力最小的道路上,并保持了一切原样.

String literals were part of the language since the beginning, while struct literals (officially known as compound literals) are a relatively recent addition, as of C99. By that time many implementations existed that placed string literals in read-only memory, especially on embedded systems with tiny amounts of RAM. By then designers of the standard had a choice of requiring string literals to be moved to a writeable location, allowing struct literals to be read-only, or leaving things as-is. None of the three solutions was ideal, so it looks like they went on the path of least resistance, and left everything the way it is.

C99标准是否允许写入复合文字(结构)?

Does the C99 standard permit writing to compound literals (structs)?

C99标准未明确禁止写入用复合文字初始化的数据对象.这与字符串文字不同,字符串文字的修改被标准视为未定义的行为.

C99 standard does not explicitly prohibit writing to data objects initialized with compound literals. This is different from string literals, whose modification is considered undefined behavior by the standard.

这篇关于结构vs字符串文字?只读还是读写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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