This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Pointer typecasts may write to incorrect addresses on ARM?
- From: Subramanyam Badri <badri_subbu at hotmail dot com>
- To: <gcc-help at gcc dot gnu dot org>
- Date: Wed, 23 Sep 2009 15:20:37 -0700
- Subject: Pointer typecasts may write to incorrect addresses on ARM?
Hi,
I am using a GCC compiler for ARM and see an alignment problem with pointer typecasting. We're porting code to ARM and found some writes to incorrect addresses. Can you help? For example:
char * pChar = (char *)0x100000; /* hardcoded for example purposes */
pChar[0] = 0x0;
*(int *)&pChar[1] = 0x11223344;
/* We really want pChar[0] to equal 0x00, but it equals 0x44 on ARM!!!! */
printf ("pChar[0] = 0x%02x\n", pChar[0]);
My understanding is that non-aligned memory access is allowed on ARM, so it's not clear why typecasting and writing data adjusts the destination address.
I'm facing this issue while porting network code to ARM, and I'm not sure what strategy to take to ensure we catch/fix all problems like this during the porting effort. Any advice?
Please find the sample program tested on the same platform and find the same results.
I tested the same sample code in x86 PC with MinGW compiler and there are no issues.
G:\>a.exe
main: pMem[0] = 0
main: pMem[0] = 0
Thanks in advance.
Regards,
Badri
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
char * pMem;
pMem = calloc (1, 1024);
if (pMem == NULL)
{
printf ("ERROR\n");
return -1;
}
printf ("%s: pMem[0] = %x\n", __FUNCTION__, pMem[0]);
*((int *)&pMem[1]) = 0x11223344;
printf ("%s: pMem[0] = %x\n", __FUNCTION__, pMem[0]);
if (pMem[0] != 0)
{
printf ("%s: FAILURE\n", __FUNCTION__);
return -1;
}
free (pMem);
return 0;
}
ARM Results:
main: pMem[0] = 0
main: pMem[0] = 44
main: FAILURE
_________________________________________________________________
Lauren found her dream laptop. Find the PC that’s right for you.
http://www.microsoft.com/windows/choosepc/?ocid=ftp_val_wl_290