public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* function cast leads to gcc abort command
@ 2013-12-18 12:25 buzush
  2013-12-18 12:37 ` Jonathan Wakely
  0 siblings, 1 reply; 2+ messages in thread
From: buzush @ 2013-12-18 12:25 UTC (permalink / raw)
  To: gcc-help

hi,

in the following code func.c :
    #include <stdio.h>
    
    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.)
can anyone explain the problem of the compiler in the .c case?




--
View this message in context: http://gcc.1065356.n5.nabble.com/function-cast-leads-to-gcc-abort-command-tp996603.html
Sent from the gcc - Help mailing list archive at Nabble.com.

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

* Re: function cast leads to gcc abort command
  2013-12-18 12:25 function cast leads to gcc abort command buzush
@ 2013-12-18 12:37 ` Jonathan Wakely
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Wakely @ 2013-12-18 12:37 UTC (permalink / raw)
  To: buzush; +Cc: gcc-help

On 18 December 2013 12:25, buzush wrote:
> hi,
>
> in the following code func.c :
>     #include <stdio.h>
>
>     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.

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

end of thread, other threads:[~2013-12-18 12:37 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-12-18 12:25 function cast leads to gcc abort command buzush
2013-12-18 12:37 ` Jonathan Wakely

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).