On Tue, Jul 13, 2004 at 17:17:41 +0530, amith wrote: > hi all, > > main() > { > void **blkPtr; > char *Buffer; > char *ptr; > Buffer = (char *) malloc(10000); > > if( ((unsigned long)Buffer % 4) == 0) > printf(" Buffer = %u is divisible by 4 \n",Buffer); > > // Buffer++; /* if this line is commented ,the code works as > expected */ > blkPtr = (void **)Buffer; > > > *blkPtr = ((void *)0x11223344); > ptr = Buffer; > printf(" value = %X address = %X\n",*ptr,ptr); > ptr++; > printf(" value = %X address = %X\n",*ptr,ptr); > ptr++; > printf(" value = %X address = %X\n",*ptr,ptr); > ptr++; > printf(" value = %X address = %X\n",*ptr,ptr); > } > [...] > could someone bail me out of this ? I fear that the code is invalid. It violates the aliasing rules. The compiler is allowed to expect, that a char * never ever points to the same location as void **. It's not said anywhere that something that is valid pointer to character is also a valid pointer to pointer. It seems to be the case that on ARM it really isn't, because there is alignment requirement for pointers. When there is alignment requirement, the compiler makes sure to never violate it -- and emmits instructions that only work when it's satisfied. When you deliberately break the rules, you have to keep the pieces. ------------------------------------------------------------------------------- Jan 'Bulb' Hudec <bulb@ucw.cz>
Attachment:
signature.asc
Description: Digital signature