public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/60201] New: Issue with CRTP generation under 4.8.1
@ 2014-02-14 17:46 andrew.stern at itg dot com
  2014-02-14 19:20 ` [Bug c++/60201] " pinskia at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: andrew.stern at itg dot com @ 2014-02-14 17:46 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 60201
           Summary: Issue with CRTP generation under 4.8.1
           Product: gcc
           Version: 4.8.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: andrew.stern at itg dot com

I created a CRTP (Curiously recurring template pattern) and added non-static
member variables to my base class and that works without issue.  But when I add
non-static variables to the subclass instance the initialization and values for
the variables in this class don't get initialized properly and also don't
consistently keep the same values.  I checked the size-of the object and it
does seem to contain the correct size that is the Base member variables plus
the Subclasses member variables and the this pointer is consistently the same
value.  It's just that when dumping the values to the screen the ones in the
subclass multiple instances of my object are dumping the same values regardless
of the object. I'm guessing that that the computation of the offset in the
object for these variables is being calculated incorrectly.  Note that 472
should have been printed and not 0 in the example below.


gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-2/root/usr/libexec/gcc/x86_64-redhat-linux/4.8.1/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/opt/rh/devtoolset-2/root/usr
--mandir=/opt/rh/devtoolset-2/root/usr/share/man
--infodir=/opt/rh/devtoolset-2/root/usr/share/info
--with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap
--enable-shared --enable-threads=posix --enable-checking=release
--with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions
--enable-gnu-unique-object --enable-linker-build-id
--enable-languages=c,c++,fortran,lto --enable-plugin
--with-linker-hash-style=gnu --enable-initfini-array --disable-libgcj
--with-isl=/builddir/build/BUILD/gcc-4.8.1-20130715/obj-x86_64-redhat-linux/isl-install
--with-cloog=/builddir/build/BUILD/gcc-4.8.1-20130715/obj-x86_64-redhat-linux/cloog-install
--with-mpc=/builddir/build/BUILD/gcc-4.8.1-20130715/obj-x86_64-redhat-linux/mpc-install
--with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.8.1 20130715 (Red Hat 4.8.1-4) (GCC) 

Compile flags:
-g;-Wall;-std=c++11;-O0

To execute: 
~/crtpbug/crtpbug/Debug/crtpbug

Output of application:
The this pointer is 7fff2124ec70 with a size of 8 and values of 304 and 0

Source code:
#include <stdio.h>

struct ParamOne {
    double val {0.0};
};

struct ParamTwo {
    int val {0};
};

template<typename P, typename Data, typename Other>
class Baseclass
{
public:
    using subclass_type = P;
    using data_type = Data;
    using other_type = Other;

    bool Method( const Data &data);

public:
    int m_BaseClassValue { 304 };
};

template<typename P, typename Data, typename Other> using pdata_type = typename
P::data_type;
template<typename P, typename Data, typename Other> using pother_type =
typename P::other_type;

template<typename P, typename Data, typename Other>
bool Baseclass<P, Data, Other>::Method( const Data &data )
{
    P& Subclass = static_cast<P&>( *this );
    pother_type<P, Data, Other> other;
    other.val = 11;

    return Subclass.SubclassMethod( data, other );
}

template<typename Data, typename Other>
class Subclass : public Baseclass<Subclass<Data, Other>, Data, Other>
{
public:
    using data_type = Data;
    using other_type = Other;

    bool SubclassMethod( const Data &data, Other &other );

public:
    int m_SubClassValue { 472 };
};

template<typename Data, typename Other>
bool Subclass<Data, Other>::SubclassMethod( const Data &data, Other &other )
{
    return true;
}

template<>
bool Subclass<ParamOne, ParamTwo>::SubclassMethod( const ParamOne &data,
ParamTwo &other )
{
    printf( "The this pointer is %lx with a size of %ld and values of %d and
%d\n", (long)this, sizeof(*this), m_BaseClassValue, m_SubClassValue );
    return true;
}

int main(int argc, char **argv)
{
    ParamOne one;
    one.val = 5.0;

    Baseclass<Subclass<ParamOne, ParamTwo>, ParamOne, ParamTwo> test;

    test.Method(one);
    return 0;
}


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c++/60201] Issue with CRTP generation under 4.8.1
  2014-02-14 17:46 [Bug c++/60201] New: Issue with CRTP generation under 4.8.1 andrew.stern at itg dot com
@ 2014-02-14 19:20 ` pinskia at gcc dot gnu.org
  2014-02-14 19:30 ` andrew.stern at itg dot com
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2014-02-14 19:20 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
>    P& Subclass = static_cast<P&>( *this );

Is this valid as *this at this point is just a Baseclass type (and does not
have a Subclass type.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c++/60201] Issue with CRTP generation under 4.8.1
  2014-02-14 17:46 [Bug c++/60201] New: Issue with CRTP generation under 4.8.1 andrew.stern at itg dot com
  2014-02-14 19:20 ` [Bug c++/60201] " pinskia at gcc dot gnu.org
@ 2014-02-14 19:30 ` andrew.stern at itg dot com
  2014-02-14 19:44 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: andrew.stern at itg dot com @ 2014-02-14 19:30 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Stern <andrew.stern at itg dot com> ---
>>Is this valid as *this at this point is just a Baseclass type (and does not have a Subclass type.

I believe the code to be valid since the template generates both the Baseclass
and Subclass.  The Subclass object is really just the same object as the
Baseclass and the sizeof operator seems to give the correct values.  Note that
the code compiles and runs and the correct member functions get called.  If you
think this is the issue what is the correct method to get the Subclass type.

Note the line also is consistent with the code located at:
http://eli.thegreenplace.net/2011/05/17/the-curiously-recurring-template-pattern-in-c/

Specifically:    const Derived& d1 = static_cast<const Derived&>(o1);


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c++/60201] Issue with CRTP generation under 4.8.1
  2014-02-14 17:46 [Bug c++/60201] New: Issue with CRTP generation under 4.8.1 andrew.stern at itg dot com
  2014-02-14 19:20 ` [Bug c++/60201] " pinskia at gcc dot gnu.org
  2014-02-14 19:30 ` andrew.stern at itg dot com
@ 2014-02-14 19:44 ` pinskia at gcc dot gnu.org
  2014-02-14 20:00 ` andrew.stern at itg dot com
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2014-02-14 19:44 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Look at the original object:
    Baseclass<Subclass<ParamOne, ParamTwo>, ParamOne, ParamTwo> test;

test is of class Baseclass.  You call Method on it.
Method then has a cast from Baseclass to Subclass for *this but *this is only
of type Baseclass and not Subclass.

So when you call SubclassMethod on the object you just casted, it would really
be only a Baseclass and not a Subclass so m_SubClassValue never was initialized
and is in fact outside of the object.

If we run using valgrind we get:
==10910== Use of uninitialised value of size 8
==10910==    at 0x56167AB: _itoa_word (_itoa.c:195)
==10910==    by 0x5619347: vfprintf (vfprintf.c:1616)
==10910==    by 0x5621A59: printf (printf.c:35)
==10910==    by 0x400740: Subclass<ParamOne, ParamTwo>::SubclassMethod(ParamOne
const&, ParamTwo&) (t.cc:60)
==10910==    by 0x4007D6: Baseclass<Subclass<ParamOne, ParamTwo>, ParamOne,
ParamTwo>::Method(ParamOne const&) (t.cc:35)
==10910==    by 0x400787: main (t.cc:71)

If we run using address sanitizer we get:
================================================================
==11676== ERROR: AddressSanitizer: stack-buffer-overflow on address
0x7fff3594a8d4 at pc 0x400a6f bp 0x7fff3594a7c0 sp 0x7fff3594a7b8
READ of size 4 at 0x7fff3594a8d4 thread T0
    #0 0x400a6e (/home/apinski/a.out+0x400a6e)
    #1 0x400cdd (/home/apinski/a.out+0x400cdd)
    #2 0x400bc8 (/home/apinski/a.out+0x400bc8)
    #3 0x7fe47a7b9c8c (/lib/libc-2.11.3.so+0x1ec8c)
    #4 0x4008d8 (/home/apinski/a.out+0x4008d8)
Address 0x7fff3594a8d4 is located at offset 36 in frame <main> of T0's stack:
  This frame has 2 object(s):
    [32, 36) 'test'
    [96, 104) 'one'


See how test ends at 36 but we are accessing location 36.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c++/60201] Issue with CRTP generation under 4.8.1
  2014-02-14 17:46 [Bug c++/60201] New: Issue with CRTP generation under 4.8.1 andrew.stern at itg dot com
                   ` (2 preceding siblings ...)
  2014-02-14 19:44 ` pinskia at gcc dot gnu.org
@ 2014-02-14 20:00 ` andrew.stern at itg dot com
  2014-02-14 20:07 ` andrew.stern at itg dot com
  2014-02-14 20:08 ` andrew.stern at itg dot com
  5 siblings, 0 replies; 7+ messages in thread
From: andrew.stern at itg dot com @ 2014-02-14 20:00 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Stern <andrew.stern at itg dot com> ---
Since the examples that I found for CRTP seem to indicate a line very much like
this could you perhaps suggest the correct method to get and call the Subclass
object from the Baseclass object?


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c++/60201] Issue with CRTP generation under 4.8.1
  2014-02-14 17:46 [Bug c++/60201] New: Issue with CRTP generation under 4.8.1 andrew.stern at itg dot com
                   ` (3 preceding siblings ...)
  2014-02-14 20:00 ` andrew.stern at itg dot com
@ 2014-02-14 20:07 ` andrew.stern at itg dot com
  2014-02-14 20:08 ` andrew.stern at itg dot com
  5 siblings, 0 replies; 7+ messages in thread
From: andrew.stern at itg dot com @ 2014-02-14 20:07 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Andrew Stern <andrew.stern at itg dot com> ---
It seems that if I change my code
From:
    Baseclass<Subclass<ParamOne, ParamTwo>, ParamOne, ParamTwo> test;

To:
    Subclass<ParamOne, ParamTwo> test;

Then it seems to work.

This produces the following output:
   The this pointer is 7fffddafc360 with a size of 8 and values of 304 and 472


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c++/60201] Issue with CRTP generation under 4.8.1
  2014-02-14 17:46 [Bug c++/60201] New: Issue with CRTP generation under 4.8.1 andrew.stern at itg dot com
                   ` (4 preceding siblings ...)
  2014-02-14 20:07 ` andrew.stern at itg dot com
@ 2014-02-14 20:08 ` andrew.stern at itg dot com
  5 siblings, 0 replies; 7+ messages in thread
From: andrew.stern at itg dot com @ 2014-02-14 20:08 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Andrew Stern <andrew.stern at itg dot com> ---
Thank you for your help.


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2014-02-14 20:08 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-02-14 17:46 [Bug c++/60201] New: Issue with CRTP generation under 4.8.1 andrew.stern at itg dot com
2014-02-14 19:20 ` [Bug c++/60201] " pinskia at gcc dot gnu.org
2014-02-14 19:30 ` andrew.stern at itg dot com
2014-02-14 19:44 ` pinskia at gcc dot gnu.org
2014-02-14 20:00 ` andrew.stern at itg dot com
2014-02-14 20:07 ` andrew.stern at itg dot com
2014-02-14 20:08 ` andrew.stern at itg dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).