- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Using Nios-II with eclipse. While accessing integer(32 bit) form non word align location getting incorrect value as described in below example.
- Tested with eclipse version 13 and 16, both are giving same result. - Test case : -------------------------------------------------------------------------------- char c[10] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}; printf("int = 0x%08x", *(int*)(&c[2])); -------------------------------------------------------------------------------- above code should print "int = 0x66554433" but getting "int = 0x44332211"Link Copied
4 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
This is not due to Eclipse, it is a limitation of the Nios II architecture that does not support unaligned accesses.
But anyway, please note that using an unaligned pointer accesses is "undefined behaviour" in the C standard, so your code could return anything and still be 100% correct according to the C standard. This kind of construct is not portable because it can work on some architectures and not on others. It could even work on some compilers and not others.- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Daixiwen,
Thanks for you feedback. But we are porting LUFA USB host/device stack on nios-II. In it there is requirement of unaligned memory access from stack itself. Also we have checked many compilers for different platforms like NXP LPC microcontrollers, AVR (from Atmel) microcontrollers, windows, linux etc. But with all we are getting expected result. Thanks, Vasu.- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I'm afraid you will have to go through the LUFA code and change all the places where unaligned pointers are used. To help you, you can use the -Wcast-align option in gcc and it will generate a warning each time it thinks an unaligned transfer can happen.
IIRC unaligned transfers trigger a bus error on some ARM architectures also so it's really not good practise when making portable code. GCC for ARM has a -mno-unaligned-access option that will automatically convert unaligned accesses into multiple byte accesses, but unfortunately it doesn't seem that this option is available for the Nios II architecture.- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi Vasu,
Are you using "little Indian" or "big Indian "? please send a sample of the wrong and expected results to investigate.

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page