Reusing Strings

The Reuse Strings setting controls how the compiler stores string literals.

If you enable this setting, the compiler stores each string literal separately. Otherwise, the compiler stores only one copy of identical string literals. This means if you change one of the strings, you change them all. For example, take this code snippet:

char *str1="Hello";
char *str2="Hello"; // two identical strings
*str2 = 'Y';

This setting helps you save memory if your program contains identical string literals which you do not modify.

If you enable the Reuse Strings setting, the strings are stored separately. After changing the first character, str1 is still "Hello", but str2 is "Yello".

If you disable the Reuse Strings setting, the two strings are stored in one memory location because they are identical. After changing the first character, both str1 and str2 are "Yello", which is counterintuitive and can create bugs that are difficult to locate.

The Reuse Strings setting corresponds to the pragma dont_reuse_strings. To check this setting, use __option (dont_reuse_strings). By default, this setting is enabled, so strings are not reused.

See also dont_reuse_strings. and Checking Settings.