PDA

View Full Version : How to get rid of this warning?: dereferencing type-punned pointer will break strict-aliasing rules


Jan Panteltje
11-10-2005, 01:38 AM
I ma cleaning up so,me program code using -Wall in gcc, but I
do not understand this error message (things work OK), how to
get rid of the error message, or what is wrong here?

void *video_routine()
{
......
}

Using this:
pthread_create( (pthread_t *)&play_thread, NULL , &video_routine, NULL);


gives me the following warning with -Wall
warning: dereferencing type-punned pointer will break strict-aliasing rules

Bernd Strieder
11-10-2005, 02:42 AM
Hello,

Jan Panteltje wrote:

> warning: dereferencing type-punned pointer will break strict-aliasing
> rules

Try -fno-strict-aliasing as option to gcc. However, this might defeat
some optimization.

Use google with the error message as search key to get more on the
background.

Bernd Strieder

HASM
11-10-2005, 04:42 AM
Jan Panteltje <pNaonStpealmtje@yahoo.com> writes:

> void *video_routine()
> {
> .....
> }
>
> Using this:
> pthread_create( (pthread_t *)&play_thread, NULL , &video_routine, NULL);
>
> gives me the following warning with -Wall
> warning: dereferencing type-punned pointer will break strict-aliasing rules

My gcc:
Thread model: posix
gcc version 4.0.1 20050727 (Red Hat 4.0.1-5)
doesn't complain, with or without -Wall, if I feed it this :

#include <pthread.h>
void *video_routine()
{
return NULL;
}
int main (int n, char*l[])
{
pthread_t play_thread;
pthread_create( (pthread_t *)&play_thread, NULL , &video_routine, NULL);
return 0;
}

Have you tried changing video_routine to be:

void *video_routine (void* ignore)

-- HASM

Jan Panteltje
11-10-2005, 07:20 AM
On a sunny day (Mon, 10 Oct 2005 14:42:12 +0200) it happened Bernd Strieder
<strieder@informatik.uni-kl.de> wrote in <didnj4$gh$1@news.uni-kl.de>:

>Hello,
>
>Jan Panteltje wrote:
>
>> warning: dereferencing type-punned pointer will break strict-aliasing
>> rules
>
>Try -fno-strict-aliasing as option to gcc. However, this might defeat
>some optimization.
>
>Use google with the error message as search key to get more on the
>background.
>
>Bernd Strieder
>
That works, clean compile!
More re-assuring to users when the compiles are clean :-)
Many thanks.

Jan Panteltje
11-10-2005, 07:30 AM
On a sunny day (Mon, 10 Oct 2005 07:42:55 -0700) it happened HASM
<not_really@comcast.net> wrote in <m3br1xbfgw.fsf@127.0.0.1>:

>Jan Panteltje <pNaonStpealmtje@yahoo.com> writes:
>
>> void *video_routine()
>> {
>> .....
>> }
>>
>> Using this:
>> pthread_create( (pthread_t *)&play_thread, NULL , &video_routine, NULL);
>>
>> gives me the following warning with -Wall
>> warning: dereferencing type-punned pointer will break strict-aliasing rules
>
>My gcc:
> Thread model: posix
> gcc version 4.0.1 20050727 (Red Hat 4.0.1-5)
>doesn't complain, with or without -Wall, if I feed it this :
>
> #include <pthread.h>
> void *video_routine()
> {
> return NULL;
> }
> int main (int n, char*l[])
> {
> pthread_t play_thread;
> pthread_create( (pthread_t *)&play_thread, NULL , &video_routine, NULL);
> return 0;
> }

I tried gcc-2.95.3 and gcc-3.3.3, the error seems to come from
(pthread_t *)&play_thread
So:
(something*)&

I fixed it by using -fno-strict-aliasing as somebody else suggested.
Thank you anyway for looking into it.

=?iso-8859-1?q?M=E5ns_Rullg=E5rd?=
11-10-2005, 08:20 AM
Jan Panteltje <pNaonStpealmtje@yahoo.com> writes:

> On a sunny day (Mon, 10 Oct 2005 07:42:55 -0700) it happened HASM
> <not_really@comcast.net> wrote in <m3br1xbfgw.fsf@127.0.0.1>:
>
>>Jan Panteltje <pNaonStpealmtje@yahoo.com> writes:
>>
>>> void *video_routine()
>>> {
>>> .....
>>> }
>>>
>>> Using this:
>>> pthread_create( (pthread_t *)&play_thread, NULL , &video_routine, NULL);
>>>
>>> gives me the following warning with -Wall
>>> warning: dereferencing type-punned pointer will break strict-aliasing rules
>>
>>My gcc:
>> Thread model: posix
>> gcc version 4.0.1 20050727 (Red Hat 4.0.1-5)
>>doesn't complain, with or without -Wall, if I feed it this :
>>
>> #include <pthread.h>
>> void *video_routine()
>> {
>> return NULL;
>> }
>> int main (int n, char*l[])
>> {
>> pthread_t play_thread;
>> pthread_create( (pthread_t *)&play_thread, NULL , &video_routine, NULL);
>> return 0;
>> }
>
> I tried gcc-2.95.3 and gcc-3.3.3, the error seems to come from
> (pthread_t *)&play_thread

Why do you have that cast there? If play_thread isn't a pthread_t,
your program is terribly broken, so there should be no need for a
cast.

--
Måns Rullgård
mru@inprovide.com

Jan Panteltje
11-10-2005, 09:00 AM
On a sunny day (Mon, 10 Oct 2005 19:20:04 +0100) it happened
=?iso-8859-1?q?M=E5ns_Rullg=E5rd?= <mru@inprovide.com> wrote in
<yw1xirw5w7xn.fsf@ford.inprovide.com>:
>>
>> I tried gcc-2.95.3 and gcc-3.3.3, the error seems to come from
>> (pthread_t *)&play_thread
>
>Why do you have that cast there? If play_thread isn't a pthread_t,
>your program is terribly broken, so there should be no need for a
>cast.

Because of this:
pthread_t *play_thread;
pthread_create( &play_thread, NULL , &video_routine, NULL);

make
video.c: In function `video_init':
video.c:234: warning: passing arg 1 of `pthread_create' from incompatible pointer type

But you mean:
pthread_t play_thread;
pthread_create( &play_thread, NULL , &video_routine, NULL);

Did I forget to allocate memory for pthread?

Roger Leigh
11-10-2005, 09:30 AM
Jan Panteltje <pNaonStpealmtje@yahoo.com> writes:

> Because of this:
> pthread_t *play_thread;
> pthread_create( &play_thread, NULL , &video_routine, NULL);
>
> make
> video.c: In function `video_init':
> video.c:234: warning: passing arg 1 of `pthread_create' from incompatible pointer type
>
> But you mean:
> pthread_t play_thread;
> pthread_create( &play_thread, NULL , &video_routine, NULL);
>
> Did I forget to allocate memory for pthread?

Yes; you had a pointer to nowhere.


--
Roger Leigh
Printing on GNU/Linux? http://gimp-print.sourceforge.net/
Debian GNU/Linux http://www.debian.org/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.

=?iso-8859-1?q?M=E5ns_Rullg=E5rd?=
11-10-2005, 09:50 AM
Jan Panteltje <pNaonStpealmtje@yahoo.com> writes:

> On a sunny day (Mon, 10 Oct 2005 19:20:04 +0100) it happened
> =?iso-8859-1?q?M=E5ns_Rullg=E5rd?= <mru@inprovide.com> wrote in
> <yw1xirw5w7xn.fsf@ford.inprovide.com>:
>>>
>>> I tried gcc-2.95.3 and gcc-3.3.3, the error seems to come from
>>> (pthread_t *)&play_thread
>>
>>Why do you have that cast there? If play_thread isn't a pthread_t,
>>your program is terribly broken, so there should be no need for a
>>cast.
>
> Because of this:
> pthread_t *play_thread;
> pthread_create( &play_thread, NULL , &video_routine, NULL);
>
> make
> video.c: In function `video_init':
> video.c:234: warning: passing arg 1 of `pthread_create' from incompatible pointer type
>
> But you mean:
> pthread_t play_thread;
> pthread_create( &play_thread, NULL , &video_routine, NULL);
>
> Did I forget to allocate memory for pthread?

Yes, you did. The usual way is to simply declare it as a non-pointer.

--
Måns Rullgård
mru@inprovide.com

Erik Max Francis
11-10-2005, 11:33 AM
Roger Leigh wrote:

> Jan Panteltje <pNaonStpealmtje@yahoo.com> writes:
>
>> Because of this:
>> pthread_t *play_thread;
>> pthread_create( &play_thread, NULL , &video_routine, NULL);
>>
>> make
>> video.c: In function `video_init':
>> video.c:234: warning: passing arg 1 of `pthread_create' from incompatible pointer type
>>
>> But you mean:
>> pthread_t play_thread;
>> pthread_create( &play_thread, NULL , &video_routine, NULL);
>>
>> Did I forget to allocate memory for pthread?
>
> Yes; you had a pointer to nowhere.

This is, by the way, an excellent example of why simply tweaking
compiler options to eliminate warnings from a build is usually not a
good idea. There was a serious problem lurking here that just disabling
the warning would not have fixed.

--
Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
We'll have to make our own luck from now on.
-- Louis Wu

Jan Panteltje
11-10-2005, 12:32 PM
On a sunny day (Mon, 10 Oct 2005 20:50:18 +0100) it happened
=?iso-8859-1?q?M=E5ns_Rullg=E5rd?= <mru@inprovide.com> wrote in
<yw1x64s5w3r9.fsf@ford.inprovide.com>:

>Jan Panteltje <pNaonStpealmtje@yahoo.com> writes:
>
>> On a sunny day (Mon, 10 Oct 2005 19:20:04 +0100) it happened
>> =?iso-8859-1?q?M=E5ns_Rullg=E5rd?= <mru@inprovide.com> wrote in
>> <yw1xirw5w7xn.fsf@ford.inprovide.com>:
>>>>
>>>> I tried gcc-2.95.3 and gcc-3.3.3, the error seems to come from
>>>> (pthread_t *)&play_thread
>>>
>>>Why do you have that cast there? If play_thread isn't a pthread_t,
>>>your program is terribly broken, so there should be no need for a
>>>cast.
>>
>> Because of this:
>> pthread_t *play_thread;
>> pthread_create( &play_thread, NULL , &video_routine, NULL);
>>
>> make
>> video.c: In function `video_init':
>> video.c:234: warning: passing arg 1 of `pthread_create' from incompatible pointer type
>>
>> But you mean:
>> pthread_t play_thread;
>> pthread_create( &play_thread, NULL , &video_routine, NULL);
>>
>> Did I forget to allocate memory for pthread?
>
>Yes, you did. The usual way is to simply declare it as a non-pointer.
OK I have to thank you, and have brought out a new version.
What is rather strange that the program works with the cast, likely
some memory area is overwritten...

=?iso-8859-1?q?M=E5ns_Rullg=E5rd?=
11-10-2005, 02:22 PM
Jan Panteltje <pNaonStpealmtje@yahoo.com> writes:

> On a sunny day (Mon, 10 Oct 2005 20:50:18 +0100) it happened
> =?iso-8859-1?q?M=E5ns_Rullg=E5rd?= <mru@inprovide.com> wrote in
> <yw1x64s5w3r9.fsf@ford.inprovide.com>:
>
>>Jan Panteltje <pNaonStpealmtje@yahoo.com> writes:
>>
>>> On a sunny day (Mon, 10 Oct 2005 19:20:04 +0100) it happened
>>> =?iso-8859-1?q?M=E5ns_Rullg=E5rd?= <mru@inprovide.com> wrote in
>>> <yw1xirw5w7xn.fsf@ford.inprovide.com>:
>>>>>
>>>>> I tried gcc-2.95.3 and gcc-3.3.3, the error seems to come from
>>>>> (pthread_t *)&play_thread
>>>>
>>>>Why do you have that cast there? If play_thread isn't a pthread_t,
>>>>your program is terribly broken, so there should be no need for a
>>>>cast.
>>>
>>> Because of this:
>>> pthread_t *play_thread;
>>> pthread_create( &play_thread, NULL , &video_routine, NULL);
>>>
>>> make
>>> video.c: In function `video_init':
>>> video.c:234: warning: passing arg 1 of `pthread_create' from incompatible pointer type
>>>
>>> But you mean:
>>> pthread_t play_thread;
>>> pthread_create( &play_thread, NULL , &video_routine, NULL);
>>>
>>> Did I forget to allocate memory for pthread?
>>
>>Yes, you did. The usual way is to simply declare it as a non-pointer.
> OK I have to thank you, and have brought out a new version.
> What is rather strange that the program works with the cast, likely
> some memory area is overwritten...

It just happens that a pthread_t is the same size as a pointer, so
nothing was damaged in your case.

--
Måns Rullgård
mru@inprovide.com

=?ISO-8859-15?Q?=22Nils_O=2E_Sel=E5sdal=22?=
11-10-2005, 07:44 PM
Jan Panteltje wrote:
> I ma cleaning up so,me program code using -Wall in gcc, but I
> do not understand this error message (things work OK), how to
> get rid of the error message, or what is wrong here?
>
> void *video_routine()
> {
> .....
> }
>
> Using this:
> pthread_create( (pthread_t *)&play_thread, NULL , &video_routine, NULL);
>
>
> gives me the following warning with -Wall
> warning: dereferencing type-punned pointer will break strict-aliasing rules
Why the cast ?
Why not make play_thread a pthread_t like it is supposed to be, and
not cast it.
void *video_routine(); should be a void *video_routine(void*); btw.