Daniel Lemire's blog

, 8 min read

Are your strings immutable?

11 thoughts on “Are your strings immutable?”

  1. Leonid Boytsov says:

    Not immutable, but at least they can’t be copy-on-write anymore: https://stackoverflow.com/questions/12199710/legality-of-cow-stdstring-implementation-in-c11

  2. Guy Tremblay says:

    Concerning: “There is no programming language where you can redefine the value “3” to be equal to “5”.”

    Well, there was such a programming language where you might have been able to do so: Fortran-77.

    I don’t know if it is still like this (I wrote a program with this bug in 1985), but here is a “bug” I once had, written in pseudo-FORTRAN code:

    DEF INC( X )
    X = X + 1
    END

    INC( 0 )
    PRINT *, 0

    This program prints “1” on stdout (*)!

    Why? Because in Fortran-77, all arguments are passed by reference. When 0 is used as argument in the call to INC, what is passed as real argument is a reference to the location in the *constant table* where 0 is stored. The location’s content is then increased by 1 in INC, an so 0 becomes 1.

    1. Andrew Dalke says:

      As I understand it, that was non-conformant compiler behavior, not part of the Fortran specification. See http://computer-programming-forum.com/49-fortran/c1e8b7d194d9f46a.htm for elaboration, where one commentor quotes from the FORTRAN 66 specification then adds “All Fortran standards have required that actual arguments be
      definable if they are associated with dummy arguments that
      change during the execution of the procedure. It is not, however,
      a violation that’s required to be detected or reported by compliant
      implementations.”.

  3. llogiq says:

    Regarding the “redefining the value of 3 to be 5”, in Java and C#, you can use an Aspect Weaver to do this. In addition, in Java, you can mess with the Integer constant pool (using reflection + trickery) to set the third value to 5, which means Integer.valueOf(3) will return an Integer instance with a value of 5. Finally, observe the following forth code:

    :3 5;

    Regarding String immutability, in Rust, Strings are mutable, but one often uses a mutable &str slice instead of the raw value.

    1. Are strings mutable or immutable in Rust?

      1. dmitry_vk says:

        There are different types of strings in Rust: some of them are mutable.
        std::string::String is like C++’s std::string – an owned mutable buffer.
        &str is a immutable view of a (sub-)string (Rust’s type system also guarantees that noone can change the string while you inspect it through &str).

  4. Travis Downs says:

    Keep in mind that casting-away constness of values in C and C++ is undefined behavior if the value was originally defined as const. This means that, for example, casting away constness inside a function where you don’t really know the origin of all the arguments may be unsafe.

    This isn’t just a language-ism that has no practical consequence: modern compilers are putting objects (including const char* values and string objects) inside the “read only” section (e.g., .rodata) of the executable, which are mapped read-only into the process, and so actually cannot be written. This applies also to string literals in both languages.

    So as a practical matter, your program will probably crash if you try to write a string literal or a string declared as const.

  5. nicol biden says:

    Why are Python strings immutable? Which means a string value cannot be updated . Immutability is a clean and efficient solution to concurrent access. Having immutable variables means that no matter how many times the method is called with the same variable/value, the output will always be the same. Having mutable variables means that calling the same method with the same variables may not guarantee the same output, because the variable can be mutated at any time by another method or perhaps, another thread, and that is where you start to go crazy debugging.

    1. Oleh says:

      Variable is called so because its value can vary. Have you ever heard of visibility scopes?

  6. markfilan says:

    Everything in Python is an object . You have to understand that Python represents all its data as objects. An object’s mutability is determined by its type. Some of these objects like lists and dictionaries are mutable , meaning you can change their content without changing their identity. Other objects like integers, floats, strings and tuples are objects that can not be changed.

    Strings are Immutable

    Strings are immutable in Python, which means you cannot change an existing string. The best you can do is create a new string that is a variation on the original.

    1. grid says:

      You could use the StringIO Module which is pretty much the same as the Java StringBuffer Class. Such objects are mutable.