From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 19499 invoked by alias); 3 Feb 2014 17:53:14 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 19450 invoked by uid 89); 3 Feb 2014 17:53:14 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.6 required=5.0 tests=BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-we0-f171.google.com Received: from mail-we0-f171.google.com (HELO mail-we0-f171.google.com) (74.125.82.171) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Mon, 03 Feb 2014 17:53:13 +0000 Received: by mail-we0-f171.google.com with SMTP id u56so2737093wes.30 for ; Mon, 03 Feb 2014 09:53:10 -0800 (PST) X-Received: by 10.180.11.36 with SMTP id n4mr9633174wib.4.1391449990142; Mon, 03 Feb 2014 09:53:10 -0800 (PST) Received: from [192.168.1.27] (115.Red-88-21-228.staticIP.rima-tde.net. [88.21.228.115]) by mx.google.com with ESMTPSA id ju6sm45711172wjc.1.2014.02.03.09.53.08 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 03 Feb 2014 09:53:09 -0800 (PST) Message-ID: <52EFD783.3030207@gmail.com> Date: Mon, 03 Feb 2014 17:53:00 -0000 From: =?UTF-8?B?w4FuZ2VsIEdvbnrDoWxleg==?= User-Agent: Thunderbird MIME-Version: 1.0 To: Graziano Servizi CC: gcc-help@gcc.gnu.org Subject: Re: question References: <52EFD3C2.6030209@bo.infn.it> In-Reply-To: <52EFD3C2.6030209@bo.infn.it> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-IsSubscribed: yes X-SW-Source: 2014-02/txt/msg00019.txt.bz2 On 03/02/14 18:37, Graziano Servizi wrote: > How could this (included) very short code work? > It isn't a NOT SAFE conversion that made inside void fun? > > How can the A pointer know about the B's member f() ? > > I'm using gcc 4.8.2 on a Linux Fedora 19 system. > > Thanks for your help. > > G. Servizi The a pointer knows nothing about B's member f(). The compiler sees that you are calling B::f(), and calls it (expecting a B object) with a A object. As B::f() doesn't use anything of B, you are “lucky” and it happens to run fine. If you were to call a B variable, it would be accessing random meemory. Equally, if it had to access the vtable for accesing f(), it would fail, too. you can mark f() as “virtual” in order to force that and get at runtime the Segmentation fault you seem to be expecting. That this invalid code happens to «work» on this compiler version doesn't make it more legal nor should it be taken as promoting its (mis)use, etc. etc.