Is putting new items in C++ `std::map` during a `std::for_each` that traverses that same map a potential segfault (if realocation happens)?
09:34 12 May 2026

So, in my AEC-to-WebAssembly compiler, written in C++11, I have this piece of code:

    if (!staticPropertiesInitialized) { // https://atheistforums.org/thread-63150-post-2055509.html#pid2055509
      basicDataTypeSizes["Integer32"] = 4;
      basicDataTypeSizes["Character"] = 1;
      basicDataTypeSizes["Decimal32"] = 4;
      basicDataTypeSizes["Integer64"] = 8;
      basicDataTypeSizes["Decimal64"] = 8;
      basicDataTypeSizes["Integer16"] = 2;
      std::for_each(basicDataTypeSizes.begin(), basicDataTypeSizes.end(),
                    [&](std::pair type) {
                      if (not(isPointerType(type.first)))
                        basicDataTypeSizes[type.first + "Pointer"] =
                            4; // JavaScript (WebAssembly) virtual machine is
                               // 32-bit (pointers being 32 bits or 4 bytes
                               // long), unless somebody switches to the 64-bit
                               // mode (which is almost never done).
                    });
      basicDataTypeSizes["Nothing"] = 0;

Now, obviously, if basicDataTypeSizes were a std::vector, such a code (modifying the size of the container while iterating it with a std::for_each) would be a potential segfault, in the unlikely case the realocation happens during the loop. But basicDataTypeSizes is, of course, a std::map. std::map is essentially a Red-Black Tree, and its nodes are not supposed to be realocated while the std::map is being modified... Or are they?

No compiler I've tried this piece of code in emits any warnings for this code.

c++ c++11