Uploaded image for project: 'C++ Standard Library'
  1. C++ Standard Library
  2. STDCXX-556

[MSVC 7.1] Bug in implementation of the empty base optimization

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: External
    • Labels:
      None
    • Environment:

      MSVC 7.1 with Service Pack 1

      Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.6030 for 80x86
      Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.

    • Severity:
      Incorrect Behavior

      Description

      The test below exit with exitcode 1 due to bug in MSVC 7.1 implementation of the empty base optimization.

      -----------------------------
      #include <cstring>
      #include <cstdio>

      #include <new>

      class empty_base { };

      class base
      {
      public:
      base (int val) : __val (val) { }

      volatile int __val;
      };

      class derived1 : public base, public empty_base
      {
      public:
      derived1 (int val) : base (val), empty_base () { }
      };

      class derived2 : public base, public empty_base
      {
      public:
      derived2 (int val) : base (val) { }
      };

      template <class T>
      int foo (const int val, const char* name)
      {
      const char fill = '\xdc';

      char buf [sizeof (T) + 1];
      std::memset (buf, fill, sizeof (buf));

      T* t = new (buf) T (val);

      int ret = 0;

      if (fill != buf [sizeof (T)])

      { ++ret; std::printf ("buf [sizeof (%s)] expected %d, got %d\n", name, int (fill), int (buf [sizeof (T)])); }

      if (val != t->__val)

      { ++ret; std::printf ("%s::__val expected %d, got %d\n", name, val, t->__val); }

      t->~T ();

      return ret;
      }

      int main(int, char**)
      {
      const int init = 0xcdcdcdcd;

      return foo <derived1> (init, "derived1")
      + foo <derived2> (init, "derived2");
      }
      -----------------------------

      The test output:
      -----------------------------
      buf [sizeof (derived1)] expected -36, got 0
      -----------------------------

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                farid Farid Zaripov
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated: