-4

In the below code -

#include<iostream>
#include<string>
using namespace std;
class my_string
{
    string s;
    public :
    my_string(const string& s):s(s){ cout << s;}
};
int main()
{
    my_string sa{"amruth"};
}

What is the address "amruth" string that is passed into the constructor? Where does the address locate in stack data or code data?

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Amruth A
  • 66
  • 5
  • 17
  • 2
    C++ doesn't have a concept of stack, heap or code data. All we known is it is an array with static storage duration. You'd have to check your implementation documentation to determine what actually happens. – NathanOliver Jul 17 '19 at 13:44
  • 1
    Another fun thing is C++ doesn't even specify if the same string literal is unique or not. `char foo[] = "foo"; char foo2[] = "foo";` may or may not produce two static arrays (or none in the as-if rule case). – NathanOliver Jul 17 '19 at 13:47
  • C-string is different than `std::string`. so `s` passed in constructor is a temporary std::string built from C-string `"amruth"`. – Jarod42 Jul 17 '19 at 13:47
  • @NathanOliver That is not correct. What if I modify characters of `foo` and not `foo2`? Surely there has to be 2 different static arrays. – Aykhan Hagverdili Jul 17 '19 at 13:50
  • @nathanoliver for the example you gave there would be two character arrays so they would definitely have distinct addresses. – Mark Ransom Jul 17 '19 at 13:52
  • @Ayxan `foo` and `foo2` are copies of `"foo"` and have their own storage. If change one of them it doesn't change the other, and it doesn't change the string literal either. When I said static arrays, I mean `"foo"` itself is an array, which may or may not be duplicated for every literal `"foo"` you have in your code. – NathanOliver Jul 17 '19 at 13:52
  • @MarkRansom Yes, `foo` and `foo2` are distinct, but the `"foo"`'s they were initialized with may or may not be. – NathanOliver Jul 17 '19 at 13:53
  • @NathanOliver you said "may or may not produce two static arrays" but it *has to* produce two static arrays, `foo` and `foo2`. – Aykhan Hagverdili Jul 17 '19 at 13:54
  • `foo` and `foo2` are not static unless they are global or declared `static`. I was using them in the context of being in a function so they are not static. – NathanOliver Jul 17 '19 at 13:55
  • In general, a string literal is placed into read-only section of your program. It could be in ROM, or inline with the code. – Thomas Matthews Jul 17 '19 at 13:56

1 Answers1

2

What is the address of "amruth"

The address is &"amruth".

Where does the address located

C++ standard does not specify locations of variables or other objects. Those are freely chosen by the implementation. What the language does specify is storage classes. String literals have static storage.

Note that the address of the literal is different from the address of the temporary std::string object that is initialised from the string literal and bound to the reference argument.

eerorika
  • 232,697
  • 12
  • 197
  • 326