Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String literals that contain '\0' - why aren't they the same?

Tags:

c++

string

So I did the following test:

char* a = "test";
char* b = "test";
char* c = "test\0";

And now the questions:

1) Is it guaranteed that a==b? I know I'm comparing addresses. This is not meant to compare the strings, but whether identical string literals are stored in a single memory location

2) Why doesn't a==c? Shouldn't the compiler be able to see that they're referring to the same string?

3) Is an extra \0 appended at the end of c, even though it already contains one?

I didn't want to ask 3 different questions for this because they seem somehow related, sorry 'bout that.

Note: The tag is correct, I'm interested in C++. (although please specify if the behavior is different for C)

like image 696
AMCoder Avatar asked May 24 '12 17:05

AMCoder


People also ask

Is 0 A string literal?

It's the "end" of a string. A null character. In memory, it's actually a Zero.

Are there any differences in string literals in Python?

Python strings can be created with single quotes, double quotes, or triple quotes. When we use triple quotes, strings can span several lines without using the escape character. In our example we assign three string literals to a , b , and c variables. And we print them to the console.

What is special about a string literal?

A "string literal" is a sequence of characters from the source character set enclosed in double quotation marks (" "). String literals are used to represent a sequence of characters which, taken together, form a null-terminated string. You must always prefix wide-string literals with the letter L.

What is the difference between string and literal?

Definition. String literal in Java is a set of characters that is created by enclosing them inside a pair of double quotes. In contrast, String Object is a Java is a set of characters that is created using the new() operator. Thus, this explains the main difference between string literal and string object.


1 Answers

Is it guaranteed that a==b?

No. But it is allowed by §2.14.5/12:

Whether all string literals are distinct (that is, are stored in nonoverlapping objects) is implementation-defined. The effect of attempting to modify a string literal is undefined.

And as you can see from that last sentence using char* instead of char const* is a recipe for trouble (and your compiler should be rejecting it; make sure you have warnings enabled and high conformance levels selected).

Why doesn't a==c? Shouldn't the compiler be able to see that they're referring to the same string?

No, they're not required to be referring to same array of characters. One has five elements, the other six. An implementation could store the two in overlapping storage, but that's not required.

Is an extra \0 appended at the end of c, even though it already contains one?

Yes.

like image 139
R. Martinho Fernandes Avatar answered Sep 17 '22 16:09

R. Martinho Fernandes