public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "redi at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/53225] static operator new in multiple inheritance carries incorrect type information for the class
Date: Fri, 04 May 2012 21:30:00 -0000	[thread overview]
Message-ID: <bug-53225-4-hAF1flKd0R@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-53225-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #20 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-05-04 21:29:43 UTC ---
(In reply to comment #18)
> This code compiles:
> 
> #include <cstddef>
> #include <stdlib.h>
> typedef unsigned int uint;
> 
> class C{ // just here to be faithful to the original code
>   int y;
> };
> 
> class A{
> public:
>   typedef A this_type;
> 
>   void* operator new(size_t enfacia_size, uint count){
>       size_t total_size 
>     = enfacia_size
>     + sizeof(int) * count; // the 'tail'
>     ;
>       this_type *new_pt = (this_type *)malloc(total_size);
>       new_pt->count = count;

The bug is here.

>       return new_pt;
>   };
>   uint count;
> };
> 
> class B : public C, public A{
> public:
>     int i;
> };
> 
> int main(){
>   B *b_pt = new(5) B;  
>   uint j=0;
>   j++;
> };
> 
> And this is the debugger output:
> 
> (gdb) r
> Starting program try_offsets 
> Breakpoint 1, main () at try_offsets.cc:32
> (gdb) n
> (gdb) p &(b_pt->count)
> $1 = (uint *) 0x804a00c
> (gdb) x/10 b_pt
> 0x804a008:  5   0   0   0
> 0x804a018:  0   0   0   0
> 0x804a028:  0   135129
> (gdb) p b_pt
> $2 = (B *) 0x804a008
> (gdb) 
> 
> Compare this to the prior code and debugger output of the same form, and you
> will notice that operator new, from a point of view of type, behaves
> differently than other method.  Though it was inherited, its understanding of
> this_type has not changed, as it does in the case for other methods.

No it doesn't!

That never happens for any type or any member function!


#include <iostream>

class C{ // just here to be faithful to the original code
  int y;
};

class A{
public:
  void method(void* p){
    std::cout << "this=" << this << " p=" << p << '\n';
  }
  int count;
};

class B : public C, public A{
};

int main(){

  A a;
  a.method(&a);

  B b;
  b.method(&b);

}

this=0x7fffe08b1640 p=0x7fffe08b1640
this=0x7fffe08b1634 p=0x7fffe08b1630

Note that when calling b.method(&b) the "this" pointer is not the same as &b

The member count is always at the same address within the A object, but the
address of that A object might not be the same as the address of the B object.

A B object looks like this in memory:

-----   <------ C*      <---- B*
| C |
|---|   <------ A*
| A |
-----

When you call A::f() the 'this' pointer is adjusted to point to the A
sub-object, which is not at the same address as the complete B object that
contains it.


> In the least, in a strongly typed system I would expect there to be a warning
> about inheriting operator new when the type was not going to be updated as it
> is for other opertors and methods, though I suspect it is a small matter for
> the compiler to give it the right type info.

The type is never "updated" when a function is inherited!  The base class
function is just visible and accessible in the derived class.  It's not copied
or update, the same, original function is used unaltered.

Please, go and learn C++ somewhere else.  I don't know what language your
mental model corresponds to, but it's not C++.


  parent reply	other threads:[~2012-05-04 21:30 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-05-04  3:30 [Bug c++/53225] New: " dimitrisdad at gmail dot com
2012-05-04  3:35 ` [Bug c++/53225] " dimitrisdad at gmail dot com
2012-05-04  3:41 ` pinskia at gcc dot gnu.org
2012-05-04  5:21 ` dimitrisdad at gmail dot com
2012-05-04  8:55 ` redi at gcc dot gnu.org
2012-05-04  9:44 ` redi at gcc dot gnu.org
2012-05-04 18:12 ` dimitrisdad at gmail dot com
2012-05-04 18:32 ` redi at gcc dot gnu.org
2012-05-04 18:57 ` dimitrisdad at gmail dot com
2012-05-04 19:25 ` dimitrisdad at gmail dot com
2012-05-04 19:45 ` redi at gcc dot gnu.org
2012-05-04 19:59 ` dimitrisdad at gmail dot com
2012-05-04 20:02 ` dimitrisdad at gmail dot com
2012-05-04 20:10 ` daniel.kruegler at googlemail dot com
2012-05-04 20:18 ` dimitrisdad at gmail dot com
2012-05-04 20:35 ` redi at gcc dot gnu.org
2012-05-04 20:56 ` dimitrisdad at gmail dot com
2012-05-04 20:59 ` redi at gcc dot gnu.org
2012-05-04 21:07 ` redi at gcc dot gnu.org
2012-05-04 21:16 ` dimitrisdad at gmail dot com
2012-05-04 21:27 ` dimitrisdad at gmail dot com
2012-05-04 21:30 ` redi at gcc dot gnu.org [this message]
2012-05-04 21:41 ` dimitrisdad at gmail dot com
2012-05-04 21:52 ` redi at gcc dot gnu.org
2012-05-04 21:56 ` redi at gcc dot gnu.org
2012-05-04 22:01 ` dimitrisdad at gmail dot com
2012-05-04 22:05 ` daniel.kruegler at googlemail dot com
2012-05-04 22:07 ` pinskia at gcc dot gnu.org
2012-05-04 22:09 ` redi at gcc dot gnu.org
2012-05-04 22:31 ` dimitrisdad at gmail dot com
2012-05-04 22:55 ` dimitrisdad at gmail dot com
2012-05-04 23:21 ` redi at gcc dot gnu.org
2012-05-04 23:37 ` dimitrisdad at gmail dot com
2012-05-04 23:37 ` dimitrisdad at gmail dot com
2012-05-05  0:04 ` pinskia at gcc dot gnu.org
2012-05-05  0:15 ` redi at gcc dot gnu.org
2012-05-05  0:49 ` dimitrisdad at gmail dot com

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-53225-4-hAF1flKd0R@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).