Re: long pointer issue with newer gcc compilers

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux