1

In C++11, char pointers cannot be initialized to string literals directly.
In earlier versions of C++, I could do this with no issue.

If the code below is allowed:

char arr[] = "Hello";
char *p_str1 = arr;  //allowed

Then why is the code below not allowed?

char *p_str3 = "Hello"; //Not allowed

Note: I am aware adding const would fix. But I need to know the reasons.

anastaciu
  • 22,293
  • 7
  • 26
  • 44
Sumaiya A
  • 117
  • 13
  • IMHO The reason is clear from the error message. The second one is like the first one but `const char arr[] = "Hello";`. – 273K Apr 06 '21 at 22:53
  • @S.M. Need to know how the first one is different from second – Sumaiya A Apr 06 '21 at 22:54
  • 1
    Related C question: [C: differences between char pointer and array](https://stackoverflow.com/questions/1335786/) – Brian Apr 06 '21 at 22:56
  • 2
    @CEPB I'm not aware of any C++ implementation that would perform a heap allocation for either case. – Brian Apr 06 '21 at 22:57
  • 2
    @CEPB The edits to your comment are making it more wrong, not less. This has nothing to do with the ability to track dynamic memory allocations. – Brian Apr 06 '21 at 23:00

2 Answers2

3

char arr[] = "Hello" stores a modifiable copy of the string literal "Hello" in the char array arr. p_str1 is a pointer to that array, and the data is modifiable, so the pointer does not need to be const.

char *p_str3 = "Hello" is a pointer directly to a string literal that is read-only. The pointer does not own the string literal, more often than not these are stored in some read-only section of memory, either way you can access the data, but you can't modify it, so making the const pointer obligatory avoids undesired problems at runtime.

The C++ standard does not allow for non-const pointers to unmodifiable data. And that's fortunate because it avoids undefined behavior by way of attempting to modify it, as often happens in C where this rule doesn't exist.

It was still legal to use non-const char pointer in C++03 (perhaps for compatibility reasons), when it was deprecated, after C++11 it was disallowed, but as far as I can tell attempting to modify these string literals was always undefined behavior.

anastaciu
  • 22,293
  • 7
  • 26
  • 44
  • 1
    Ok. Just a question though. So before C++11, this section was not read-only? – Sumaiya A Apr 06 '21 at 23:07
  • 2
    No, it was probably still read only. Allowing non-const pointers to string literals was a common source of unforced errors, so the Standard writers decided to bite the bullet and make it formally illegal. – user4581301 Apr 06 '21 at 23:09
  • 1
    @SumaiyaA, I missunderstood your question, it was never modifiable, the `const` rule was implemented to avoid problems created by attemps to modify it. – anastaciu Apr 06 '21 at 23:14
  • 1
    @user4581301 Yea, This was my major point of confusion. Thanks for clearing that the section was always read-only but modifying it caused error. So now has been restricted rightly. – Sumaiya A Apr 06 '21 at 23:19
  • 1
    I refrain from saying "always read-only" I don't believe the standard requires it to be read only to this day, only that it *may* be in read only memory. I'm not good at Standard-parsing and Language Law, but In general the C++ Standard doesn't like to specify details like this. It's only in C++20 that 2s Compliment became a requirement after decades of being the defacto standard. I believe that there will be some oddball systems out there where the literals are modifiable, but I doubt you'll see one outside of a lab. I never have seen one. – user4581301 Apr 06 '21 at 23:35
2

It's because C++ understands that the string constant will be stored in non-modifiable memory, so it must be flagged as const. In many compilers this is stored in a read-only data segment.

Any attempt to modify that string could result in a segmentation fault.

In the first case you're actually making a copy to a local array. This is modifiable.

tadman
  • 200,744
  • 21
  • 223
  • 248
  • How is the first approach different from approach 2. Need clarity. string constant will not be stored in non-modifiable memory then? – Sumaiya A Apr 06 '21 at 22:57
  • In the first case you're saying "please create a local array that has a copy of the following contents" and on most compilers that's stored on the stack. – tadman Apr 06 '21 at 22:59
  • 3
    *Any attempt to modify that string could result in a segmentation fault.* or worse. I remember the nefarious and mysterious "Bus Error". – user4581301 Apr 06 '21 at 23:07