From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 8201 invoked by alias); 18 Dec 2013 12:37:01 -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 8190 invoked by uid 89); 18 Dec 2013 12:37:01 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.7 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-ie0-f170.google.com Received: from mail-ie0-f170.google.com (HELO mail-ie0-f170.google.com) (209.85.223.170) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Wed, 18 Dec 2013 12:37:00 +0000 Received: by mail-ie0-f170.google.com with SMTP id qd12so10132683ieb.15 for ; Wed, 18 Dec 2013 04:36:58 -0800 (PST) MIME-Version: 1.0 X-Received: by 10.50.17.100 with SMTP id n4mr7777142igd.11.1387370218502; Wed, 18 Dec 2013 04:36:58 -0800 (PST) Received: by 10.64.98.34 with HTTP; Wed, 18 Dec 2013 04:36:58 -0800 (PST) In-Reply-To: <1387369514104-996603.post@n5.nabble.com> References: <1387369514104-996603.post@n5.nabble.com> Date: Wed, 18 Dec 2013 12:37:00 -0000 Message-ID: Subject: Re: function cast leads to gcc abort command From: Jonathan Wakely To: buzush Cc: gcc-help Content-Type: text/plain; charset=ISO-8859-1 X-IsSubscribed: yes X-SW-Source: 2013-12/txt/msg00124.txt.bz2 On 18 December 2013 12:25, buzush wrote: > hi, > > in the following code func.c : > #include > > int Myfunc1(int i, int z) > { > return i; > } > > int main() > { > int ans; > > /* casting the function into an 'int (int)' function */ > ans = ((int(*)(int))(Myfunc1))(5); > > printf("ans: %d\n\n", ans); > > return 0; > } > > i tried to cast an int(int,int) function into an int(int) function an got > the gcc warning and note: > > func.c:13:32: warning: function called through a non-compatible type > [enabled by default] > func.c:13:32: note: if this code is reached, the program will abort > > and when trying to run i get: > > Illegal instruction (core dumped) > > (but if i compile this file with a .cpp ending with the gcc compiler it > works OK.) When the file has a .cpp extension the C++ compiler is used, which has different diagnostics. > can anyone explain the problem of the compiler in the .c case? Your program has undefined behaviour in both cases, so any behaviour is allowed, including (but not limited to) aborting or executing without error.