[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]


Reusing Strings

The Don't Reuse Strings option in the C/C++ Language Panel affects how the compiler stores string literals.

If this option is on, the compiler stores each string literal separately.

If this option is off, the compiler stores only one copy of identical string literals. This option helps you save memory if your program contains identical string literals which you do not modify.

If this option is off (meaning that string storage is reused for identical strings) and 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';

If the Don't Reuse Strings option is on. the strings are stored separately. After changing the first character, str1 is still "Hello" but str2 is "Yello".

If the Don't Reuse Strings option is off, the two strings are stored in one memory location (i.e. the same memory location is reused), because they are both identical. After changing the first character, both str1 and str2 are "Yello". This is counter-intuitive, and can lead to difficult-to locate bugs.

The Don't Reuse Strings option corresponds to the pragma dont_reuse_strings. To check whether this option is on, use __option (dont_reuse_strings). By default, this option is on. (Strings are not reused.)

See also "dont_reuse_strings." and "Checking Options."


[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]

Visit the Metrowerks website at: http://www.metrowerks.com
For assistance contact Metrowerks Technical Support at: cw_support@metrowerks.com
Copyright © 2000, Metrowerks Corp. All rights reserved.

Last updated: August 17, 2000