Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
209 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...