This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: long pointer issue with newer gcc compilers
- From: Andrew Haley <aph at redhat dot com>
- To: wcrotty <bill dot crotty at dialogic dot com>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Fri, 14 Nov 2008 16:28:35 +0000
- Subject: Re: long pointer issue with newer gcc compilers
- References: <20502954.post@talk.nabble.com>
wcrotty wrote:
> I'm building an older codebase on RE5 and the casting of a long pointer is
> somehow not working. It works when i compile on RE4.
>
> RE5 compiler is gcc (GCC) 4.1.2 20070626 (Red Hat 4.1.2-14)
>
> RE4 compiler is gcc (GCC) 3.4.3 20050227 (Red Hat 3.4.3-22.1)
>
> Here is an example of the code.
>
> char *hdr ;
> char *p;
> unsigned hl;
>
> hdr = malloc(256 );
> p=hdr;
> hl = 32;
> *((long *)(p)) = *((long *)(&hl));
> hl = 0;
> *((long *)(&hl)) = *((long *)(p));
> printf("hl = %d p = %x\n",hl,p);
> p+=4 ;
>
>
>
> Now if i change the long * to int * the RE5 compiler works fine.
>
> This isn't a compile error its a run time unexpected value error.
>
> Any help would be appreciated.
It's not legal C: you can't cast a pointer to an object of one type
to a pointer of an incompatible type.
If you really must do this, either use -fno-strict-aliasing or
use the gcc union extension:
union
{
unsigned uvalue;
long lvalue;
} u;
u.uvalue = 32;
printf( ... u.lvalue
Andrew.