0

I use visual studio 2017

int main() { std::string a = "hello"; std::cout << &a; a[1] = 'r'; std::cout << &a; gives same address as above std::cin.get(); } 

when i hover the mouse pointer over "hello".

It shows const char[6]. So "hello" is stored in read only memory.

then how come it is possible to change the value in it.

screen shot

5
  • a isn't a string literal. It's a std::string that was constructed using a string literal, but it has its own copy of the data, possibly on the heap. Try "hello"[1] = 'r'; and you'll almost certainly get a crash. Commented Mar 4, 2019 at 13:22
  • 2
    Where do you change the value in "hello"? I can only see you changing a. Commented Mar 4, 2019 at 13:22
  • 1
    42 is an integer constant. int a = 42; std::cin >> a; modifies the variable a; that does not mean it is possible to change the value of integer constants. Commented Mar 4, 2019 at 13:25
  • then a stores the address of "hello"(0x0053fcf0 "hello"). I looked into 0x0053fcf0. It has "hello". After changing the value ,l looked into that same memory.But with "hrllo" Commented Mar 4, 2019 at 13:26
  • This is the reason why we have std::string_view (to avoid the copy that std::string does) Commented Mar 4, 2019 at 13:27

3 Answers 3

4

where string literals will be stored

String literals have static storage duration.

then how come it is possible to change the value in it.

You didn't change the string literal (which is something that cannot be done in C++).

You've created an object of type std::string. std::string contains a (potentially) dynamically allocated buffer. You've copied the string literal into that dynamic buffer, and you're modifying the copy of the string literal.

But with "hrllo".It should allocate new memory for "hrllo" right? and make a to point to new location?

No. Modifying characters of a std::string will not cause reallocation. Inserting characters however may potentially cause reallocation.

Sign up to request clarification or add additional context in comments.

8 Comments

then a stores the address of "hello"(0x0053fcf0 "hello"). I looked into 0x0053fcf0. It has "hello". After changing the value ,l looked into that same memory.But with "hrllo".It should allocate new memory for "hrllo" right? and make a to point to new location?
then it may possible that "hello" still may be present somewhere?
please explain the overflow of what is happening
@bubeshp The string literal is still present. I don't understand what you refer to by "overflow."
@bubeshp that screen shot shows the type of the string literal. That type has nothing to do with what is inside std::string.
|
1

The std::string makes a copy of "hello" and you didn't make the std::string itself const.

1 Comment

then a stores the address of "hello"(0x0053fcf0 "hello"). I looked into 0x0053fcf0. It has "hello". After changing the value ,l looked into that same memory.But with "hrllo".It should allocate new memory for "hrllo" right? and make a to point to new location?
1

The literal "hello" sits in read-only memory of the executable, which is there even before the program runs. The variable ais constructed from the literal through:

string::string(const char*) 

(actually std::basic_string)

This constructor copies the characters from the literal to the newly created object. If the string is short, then the characters may be copied directly to the memory of the object. Otherwise, a new memory area is allocated, the characters are copied there, and the object points to the new block.

Never does the new object store a pointer to the original literal.

edit

The lines

std::cout << &a; 

Takes the address of the object (named a) of type std::basic_string<char>. This object sits on the stack, and is created and destroyed inside int main(). The object does not move, so the pointer is constant and does not change when the string changes. This is the same as the this pointer inside the methods of the std::basic_string<char> of this object.

7 Comments

then a stores the address of "hello"(0x0053fcf0 "hello"). I looked into 0x0053fcf0. It has "hello". After changing the value ,l looked into that same memory.But with "hrllo".It should allocate new memory for "hrllo" right? and make a to point to new location?
@bubeshp a internally stores the address of a copy of "hello". It does not just point to a constant.
@bubeshp No, a copies the characters pointed by a. It may store it in-place (short string optimization) or allocate a new memory block, copy the string there, and put the new pointer into a
then it may possible that "hello" still may be present somewhere?
please explain me the overflow of whats happening of what is happening
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.