unions?

Beta Testing discussion on mikroBasic PRO for dsPIC30/33 and PIC24.
Post Reply
Author
Message
peterverkaik
Posts: 174
Joined: 31 Aug 2009 22:44

unions?

#1 Post by peterverkaik » 16 Dec 2009 15:09

I tried out the new MikroC PRO for dsPIC
and there we can declare unions (same area of memory occupied
by different datatypes).

I could not find 'union' for MikroE Basic PRO for dsPIC, only structure.

Are unions supported, or will they, or is there a workaround
to implement unions?

regards peter

User avatar
anikolic
mikroElektronika team
Posts: 1775
Joined: 17 Aug 2009 16:51
Location: Belgrade
Contact:

#2 Post by anikolic » 17 Dec 2009 11:17

Hi,
As far as I know there are no unions in Basic language standard, only structures. Unions are C-specific and this is one of the major reasons why some mikroC compiled libraries (.MCL) that use unions cannot be used in mikroBasic.

Best regards,
Aleksandar
Web Department Manager

extrapilot
Posts: 130
Joined: 28 Nov 2006 17:59

#3 Post by extrapilot » 19 Dec 2009 22:10

Peter
As I recall, you can emulate unions in MB by using absolute addressing for a series of variables or structures (such that they overlap).
Regards,
Rob

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

#4 Post by janni » 20 Dec 2009 01:08

Actually, it's no longer necessary to use the directive absolute. One may use the keyword at to emulate a union in mB.
Here's an example:

Code: Select all

typedef Tunion as byte[4]

dim union as Tunion

dim union_x as float at union
dim union_w as word at union
dim union_b as byte at union

main:
  union_b=1
  union_w=256
  union_x=1.0
All three variables will operate on the same space in RAM (though obviously, the byte variable will need just one byte, the word variable - 2, and the float one will use all space - 4 bytes).

Naturally, one may also declare a pointer to such 'union'

Code: Select all

dim Uptr as ^Tunion

  Uptr=@union

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

#5 Post by janni » 20 Dec 2009 01:26

A thought - if the (overworked :( ) developers at mE had some time, compiler could properly interpret following declaration

Code: Select all

structure my_union
  dim x as float
  dim w as word at x
  dim b as byte at x
end structure
and we would have unions in mB without naming them as such :wink: .

Skyline
Posts: 267
Joined: 10 Jan 2006 09:35

#6 Post by Skyline » 20 Dec 2009 11:06

Hi Janni,

Thank you much for your elaboration of the at keyword to emulate unions in MB instead of absolute, your pointers are really nice.

Thanks again for the help you have been providing.

peterverkaik
Posts: 174
Joined: 31 Aug 2009 22:44

#7 Post by peterverkaik » 20 Dec 2009 14:48

My workaround for now is to declare structures of equal size
but different members. Then use a pointer, casted to particular
structure pointer, to access members. That works well.

Since C PRO for dsPIC already implements unions including bitfields,
I see no reason why Basic PRO should not have unions, even if it
is a non-standard basic extension. In fact, it could increase sales
but if you now want unions you could use the free C30 instead
of mE C PRO. Having unions in Basic could make people to select
mE Basic PRO.

regards peter

Post Reply

Return to “mikroBasic PRO for dsPIC30/33 and PIC24 Beta Testing”