Loosing space memory dsPIC33F 16 bits

vscastro wrote on Thursday, January 07, 2010:

Example of diagram of memory

|Byte | Nothing | Int | => As is today, loss memory.

| B | N | I | I |

8bits 8bits 8bits 8bits

|Byte | Int | Free | => As should be, gain memory.

| B | I | I | F |

8bits 8bits 8bits 8bits

Hi, I’m using dsPIC33F 16 bits with MPLab, I’m with problem with loss of memory, I’m mounting structs containing bytes and ints, but when I use sizeof(), the value returned not is the expected, the value is always higher. Has some setup to do on MPLab or have no way? In both examples, I have one byte and one int, but in the first I loss one byte of memory, adding one byte on the final result, and always I make this exact structure, it adds one byte after int.

Thank’s a lot!!!

Best regards,

Víctor Castro

edwards3 wrote on Thursday, January 07, 2010:

I think their compilers are GCC based so you can use __attribute__((packed)) on individual structure variables or -fpack-struct compiler switch to pack all structures.

Not a FreeRTOS question, by the way.

richard_damon wrote on Thursday, January 07, 2010:

One comment, on the dsPic Processor, like many processors today, multi-byte numbers may not be placed at arbitrary memory locations, but must be aligned to some power of 2 for addresses. 16 bit numbers on the dsPic must be stored on even addresses or the processor will not fetch them correctly. If you try to access a 16 bit word at an odd address, the processor will raise an address error execption.

edwards3 wrote on Thursday, January 07, 2010:

One comment, on the dsPic Processor, like many processors today, multi-byte numbers may not be placed at arbitrary memory locations, but must be aligned to some power of 2 for addresses. 16 bit numbers on the dsPic must be stored on even addresses or the processor will not fetch them correctly. If you try to access a 16 bit word at an odd address, the processor will raise an address error execption.

So the order in which you list the members inside the structure will also make a difference.  Dont have a char followed by an int followed by a char, but instead a char followed by a char followed by an int.

richard_damon wrote on Thursday, January 07, 2010:

Int followed by char followed by another char won’t lose space. A structure of an int followed by a char, and that is all, will still take 4 bytes as is required to make array work.

vscastro wrote on Friday, January 08, 2010:

Hi edwards3, the tip you said worked very well “__attribute__((packed))”.

My structures are in this syntax:
typedef struct
{
  char a;
  int b;
  char c;
} Tes1;

How I put the attribute in this structure above?

Because I got it in this syntax below:
struct Test2
{
  char a;
  int b;
  char c;
} __attribute__((__packed__));

typedef struct Test2 Test_2;

Result:
sizeof(Tes1) = 6
sizeof(Test_2) = 4