structure issue

General discussion on mikroBasic PRO for PIC32.
Post Reply
Author
Message
marcus
Posts: 101
Joined: 12 Sep 2006 08:58

structure issue

#1 Post by marcus » 26 Sep 2017 08:48

I find the Mikrobasic compiler is padding structures with useless bytes, making the structure larger than it is. For example:

Code: Select all

structure str
  dim a as word '2
  dim b as word '2
  dim c as byte '1
  dim d as byte '1
  dim e as longword  '4
end structure

dim arr as str[3]

for i = 0 to 3
    arr[i].a = 0xAAAA
    arr[i].b = 0xBBBB
    arr[i].c = 0xCC
    arr[i].d = 0xDD
    arr[i].e = 0xEEEEEEEE
next i

'send arr out of serial port - result:
'AA AA BB BB CC DD 00 00 EE EE EE EE 
'AA AA BB BB CC DD 00 00 EE EE EE EE 
'AA AA BB BB CC DD 00 00 EE EE EE EE

What are the "00 00" in the middle of the struct? Is this a bug or feature? If it's a feature, can you please tell me what to expect.

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: structure issue

#2 Post by filip » 27 Sep 2017 16:41

Hi,

Please, can you tell me which MCU and compiler version are you using ?

Regards,
Filip.

marcus
Posts: 101
Joined: 12 Sep 2006 08:58

Re: structure issue

#3 Post by marcus » 28 Sep 2017 07:53

Hi Filip, the MCU is PIC32MZ2048EFM064 and the compiler is v4.0

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: structure issue

#4 Post by filip » 28 Sep 2017 12:23

Hi,

Can you please send the UART part of the code ?

Regards,
Filip.

marcus
Posts: 101
Joined: 12 Sep 2006 08:58

Re: structure issue

#5 Post by marcus » 29 Sep 2017 10:11

Code: Select all

sub procedure SerOutN(dim p as ^byte, dim l as word)
    dim w as word
    for w = 1 to l
        UART3_write(p^)
        p = p + 1
    next w
end sub

SerOutN(@arr,36)

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: structure issue

#6 Post by filip » 03 Oct 2017 14:11

Hi,

Because the structure members are of different type, the compiler, when it gets to the "d" member, will allocate it on the odd address and then add zeros at next two consecutive addresses,
in order to allocate the last member (longword) at the address dividable by 4 (alignment 4, please look in the PIC32 Specifics chapter of help file).

The solution for your situation would be to print the structure not address by address, but as a structure member (field) instead.

Regards,
Filip.

Post Reply

Return to “mikroBasic PRO for PIC32 General”