Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > Re: type-punnin...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 16 Topic 26091 of 26955
Post > Topic >>

Re: type-punning?

by Andrey Tarasevich <andreytarasevich@[EMAIL PROTECTED] > May 6, 2008 at 11:05 AM

j.j.fishbat@[EMAIL PROTECTED]
 wrote:
> Hi all
> 
> I have a program which, with inessential details removed,
> looks like this:
> 
> --
> #include <stdlib.h>
> #include <stdio.h>
> 
> static int punme(void** dat,size_t newsize)
> {
>   void *newdat = realloc(*dat,newsize);
> 
>   if (! newdat) return 1;
> 
>   *dat = newdat;
> 
>   return 0;
> }
> 
> int main (void)
> {
>   char *dat = malloc(30);
> 
>   int ret = punme((void**)&dat,40);
> 
>   printf("punme returns %i\n",ret);
> 
>   return 0;
> }
> --
> 
> My compiler (gcc -Wall -O3) tells me that "typepun.c:19:
> warning: dereferencing type-punned pointer will break
> strict-aliasing rules".  The code works as expected, ie,
> prints that punme returns 0.
> 
> Who is the idiot? Me or the compiler?
> ...

You perform memory reinterpretation in your code. You have an lvalue 
'dat' of type 'char*', which is reinterpreted as an lvalue of type 
'void*' by using a conversion followed by a dereference '*(void**) 
&dat'. In general, accessing the lvalue obtained by such 
reinterpretation causes undefined behavior in C, unless the types are 
"similar enough". In strict-aliasing mode (implied by -O3) GCC assumes 
that such reinterpretations are not performed in the code. This is why 
it issues the warning.

-- 
Best regards,
Andrey Tarasevich
 




 16 Posts in Topic:
type-punning?
j.j.fishbat@[EMAIL PROTEC  2008-05-06 08:46:00 
Re: type-punning?
vippstar@[EMAIL PROTECTED  2008-05-06 09:43:53 
Re: type-punning?
Harald van =?UTF-8?b?RMSz  2008-05-06 19:35:00 
Re: type-punning?
Andrey Tarasevich <and  2008-05-06 11:05:25 
Re: type-punning?
lawrence.jones@[EMAIL PRO  2008-05-06 14:39:47 
Re: type-punning?
j.j.fishbat@[EMAIL PROTEC  2008-05-06 11:17:38 
Re: type-punning?
Andrey Tarasevich <and  2008-05-06 11:24:55 
Re: type-punning?
Harald van =?UTF-8?b?RMSz  2008-05-06 20:32:01 
Re: type-punning?
Kenneth Brody <kenbrod  2008-05-06 14:34:57 
Re: type-punning?
Harald van =?UTF-8?b?RMSz  2008-05-06 20:50:08 
Re: type-punning?
j.j.fishbat@[EMAIL PROTEC  2008-05-06 11:56:36 
Re: type-punning?
Kenneth Brody <kenbrod  2008-05-06 16:26:19 
Re: type-punning?
Kaz Kylheku <kkylheku@  2008-05-06 14:02:24 
Re: type-punning?
Harald van =?UTF-8?b?RMSz  2008-05-06 23:11:51 
Re: type-punning?
"Default User"   2008-05-06 22:49:07 
Re: type-punning?
Ben Bacarisse <ben.use  2008-05-06 22:17:42 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Jul 24 2:28:00 CDT 2008.