Controlling where string literals exist in C++ code

In z/OS® XL C/C++, the string literals exist in the code part by default, and are not modifiable if the code is reentrant. In a large number of programs, string literals may be constant. In this case, every user does not need a separate copy of these strings.

By using the #pragma strings(writable) directive, you can ensure that the string literals for that compilation unit will exist in the writable static area and be modifiable. Figure 1, which shows sample program CCNGRE2, illustrates how to make the string literals modifiable.

Figure 1. How to Make String Literals Modifiable
/* this example demonstrates how to make string literals modifiable */

#pragma strings(writable)
#include <iostream.h>
int main(void)
{
  char * s;
  s = "wall\n";      // point to string literal
  *(s+3) = 'k';      // modify string literal
  cout << s;         // output "walk\n"
}

In this example, the string "wall\n" will exist in the writable static area because #pragma strings(writable) is specified. This modifies the fourth character.