字符串字面量 [英] String Literals

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

问题描述

我对c ++中的字符串文字很少怀疑。

  char * strPtr =Hello 
char strArray [] =Hello;

现在strPtr和strArray被认为是字符串文字。



根据我的理解,字符串文字存储在只读存储器中,因此我们不能修改它们的值。



我们不能做

  strPtr [2] ='a'; 
和strArray [2] ='a';

上述语句都应该是非法的。
编译器在这两种情况下都应该抛出错误。



编译器将字符串文字保留在只读内存中,因此如果我们尝试修改它们, >

此外,const数据也被视为只读。



是否同时处理字符串文字和const数据?
我可以使用const_cast从字符串字面量中删除常量,可以更改其值吗?



字符串文本在哪里存储? (在程序的数据部分)

解决方案


现在strPtr和strArray被认为是字符串文字。


不,他们不是。字符串文字是你在代码中看到的东西。例如,Hello strPtr 是一个指针到文字(现在编译在可执行文件中)。注意它应该是 const char * ;您不能合法删除 const 每个C标准,并期望定义的行为时使用它。 strArray 是一个包含文本副本(在execuable中编译)的数组。


上面的语句都应该是非法的。编译器应该在这两种情况下都抛出错误。


不,它不应该。这两个声明是完全合法的。由于情况,第一个是未定义的。如果它们是指向 const char 的指针,那将是一个错误。



据我所知,字符串文字的定义方式与其他文字和常量的定义方式相同。但是,有以下区别:

  //这些在运行时从ROM复制到RAM:
char myString [ ] =hello;
const int myInt = 42;
float myFloats [] = {3.1,4.1,5.9};

//在运行时复制一个指向ROM中某些数据的指针:
const char * myString2 =hello;
const float * myFloats2 = {3.1,4.1,5.9};

char * myString3 =hello; // Legal,but ...
myString3 [0] ='j'; //未定义的行为! (最可能的segfaults。)

我使用的ROM和RAM这里是一般的。如果平台只是RAM(例如大多数Nintendo DS程序),则const数据可以在RAM中。写入仍然未定义,但。 const数据的位置对于正常的C ++程序员来说不重要。


I have few doubts about string literals in c++.

char *strPtr ="Hello" ;
char strArray[] ="Hello";

Now strPtr and strArray are considered to be string literals.

As per my understanding string literals are stored in read only memory so we cannot modify their values.

We cannot do

strPtr[2] ='a';
and strArray[2]='a';

Both the above statements should be illegal. compiler should throw errors in both cases.

Compiler keeps string literals in read only memory , so if we try to modify them compiler throws errors.

Also const data is also considered as readonly.

Is it that both string literals and const data are treated same way ? Can I remove constantness using const_cast from string literal can change its value?

Where exactly do string literals are stored ? (in data section of program)

解决方案

Now strPtr and strArray are considered to be string literals.

No, they aren't. String literals are the things you see in your code. For example, the "Hello". strPtr is a pointer to the literal (which is now compiled in the executable). Note that it should be const char *; you cannot legally remove the const per the C standard and expect defined behavior when using it. strArray is an array containing a copy of the literal (compiled in the execuable).

Both the above statements should be illegal. compiler should throw errors in both cases.

No, it shouldn't. The two statements are completely legal. Due to circumstance, the first one is undefined. It would be an error if they were pointers to const chars, though.

As far as I know, string literals may be defined the same way as other literals and constants. However, there are differences:

// These copy from ROM to RAM at run-time:
char myString[] = "hello";
const int myInt = 42;
float myFloats[] = { 3.1, 4.1, 5.9 };

// These copy a pointer to some data in ROM at run-time:
const char *myString2 = "hello";
const float *myFloats2 = { 3.1, 4.1, 5.9 };

char *myString3 = "hello";  // Legal, but...
myString3[0] = 'j';         // Undefined behavior!  (Most likely segfaults.)

My use of ROM and RAM here are general. If the platform is only RAM (e.g. most Nintendo DS programs) then const data may be in RAM. Writes are still undefined, though. The location of const data shouldn't matter for a normal C++ programmer.

这篇关于字符串字面量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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