sizeof(unsigned int) returns 4 instead of 2

All design tool related questions: compiler, assembler, linker. Embedded programming questions: assembler, C code.

Moderator: nferre

M14
Posts: 20
Joined: Thu Mar 27, 2014 10:51 am

sizeof(unsigned int) returns 4 instead of 2

Wed Jul 09, 2014 10:53 am

I am using Atmel Studio 6.2 for development for a SAM D20. I'm using the sizeof() function to determine the size of a variable, which is declared as an unsigned int. I should expect a size of 2, but sizeof() returns 4.

I just updated to 6.2, before I was using 6.2. I can't remember this was an issue with 6.1. I can imagine something has changed with the compiler but I can't find anything what could be the cause. Has anyone of you an idea how to fix this? :)
M14
Posts: 20
Joined: Thu Mar 27, 2014 10:51 am

Re: sizeof(unsigned int) returns 4 instead of 2

Wed Jul 09, 2014 5:09 pm

Because an unsigned int has the size of 2 bytes. Not 4.
_ref_hannu
Posts: 8
Joined: Sun Apr 06, 2014 3:35 pm

Re: sizeof(unsigned int) returns 4 instead of 2

Tue Jul 15, 2014 9:28 am

If you expect it to be 2, then use uint16_t.
_ref_hannu
Posts: 8
Joined: Sun Apr 06, 2014 3:35 pm

Re: sizeof(unsigned int) returns 4 instead of 2

Tue Jul 15, 2014 10:09 am

Indeed, but if M14:s problem is that the variable should be 2 bytes wide and unsigned, then he should use uint16_t instead of the implementation-specific unsigned int, which he expects to be 16 bits even though it's not in this case.
M14
Posts: 20
Joined: Thu Mar 27, 2014 10:51 am

Re: sizeof(unsigned int) returns 4 instead of 2

Tue Jul 15, 2014 11:14 am

I assumed that an integer always had the size of 2 bytes because this was the case with other systems which I used before. Anyway, Awneil, you were right ;). This document explains a lot :).

http://infocenter.arm.com/help/topic/co ... _aapcs.pdf

See paragraph 4.1 and then 7.1.

Return to “Development Tools”

Who is online

Users browsing this forum: No registered users and 2 guests