Re: Does casting lvalue lead to Undefined Behaviour ?
- From: Richard Heathfield <rjh@xxxxxxxxxxxxxxx>
- Date: Fri, 29 Dec 2006 06:37:33 +0000
p_cricket_guy@xxxxxxxxxxx said:
Please see the code below
-- start listing is_it_ub.c --
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
unsigned char buff[20];
unsigned int i;
i = 0xaabbccddUL;
*((int *)buff) = i; /* Is this UB ? */
Yes. You're evaluating buff (which is a pointer to char), and then
converting that value to a pointer to int, but there is no guarantee that
it will be properly aligned. You then dereference the possibly-invalid
pointer thus obtained. On systems where this works, it's harmless. On
systems where it doesn't, we're talking potential bus errors, which are
definite showstoppers.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
.
- References:
- Does casting lvalue lead to Undefined Behaviour ?
- From: p_cricket_guy
- Does casting lvalue lead to Undefined Behaviour ?
- Prev by Date: Re: c / c++ : is it end of era ?
- Next by Date: Re: Does casting lvalue lead to Undefined Behaviour ?
- Previous by thread: Re: Does casting lvalue lead to Undefined Behaviour ?
- Next by thread: Re: Does casting lvalue lead to Undefined Behaviour ?
- Index(es):
Relevant Pages
|