This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/32241] New: Compiler segmentation fault when trying to call x.~X(); (X &x = f();) in a template


/*

$ cat /proc/version
Linux version 2.6.20-15-generic (root@yellow) (gcc version 4.1.2 (Ubuntu
4.1.2-0ubuntu4)) #2 SMP Sun Apr 15 06:17:24 UTC 2007

$ gcc --version
gcc (GCC) 4.1.2 (Ubuntu 4.1.2-0ubuntu4)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


$ g++ simplestack.cpp -o simplestack -g
simplestack.cpp: In member function ?void SimpleStack<T, N>::pop() [with T =
int, long unsigned int N = 4ul]?:
simplestack.cpp:52:   instantiated from here
simplestack.cpp:32: internal compiler error: Segmentation fault
Please submit a full bug report,
with preprocessed source if appropriate.

preprocessed source: http://cpp.sourceforge.net/?show=36942

 */


#include <stdexcept>
#include <memory>

template <typename T, std::size_t N>
class SimpleStack
{
        public:
                SimpleStack()
                        : ptr(reinterpret_cast<T*>(&buffer[0]))
                {
                }

                void push(const T& element)
                {
                        if(num >= N) throw std::out_of_range("stack full");
                        new (ptr + num) T(element);
                        ++num;
                }

                bool empty() const
                {
                        return num <= 0;
                }

                T& top()
                {
                        return *(ptr + num - 1);
                }

                void pop()
                {
                        top.~T(); // this is the line causing the error!
                        --num;
                }

        private:
                SimpleStack(const SimpleStack&);
                SimpleStack& operator=(const SimpleStack&);

                char buffer[N * sizeof(T)];
                T *ptr;
                std::size_t num;
};

#include <iostream>

int main()
{
        SimpleStack<int, 4> stack;
        stack.push(3);
        std::cout << stack.top() << std::endl;
        stack.pop();
}


-- 
           Summary: Compiler segmentation fault when trying to call x.~X();
                    (X &x = f();) in a template
           Product: gcc
           Version: 4.1.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: rsalmin2 at cc dot hut dot fi
 GCC build triplet: x86_64-linux-gnu
  GCC host triplet: x86_64-linux-gnu
GCC target triplet: x86_64-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32241


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]