UNION problem?

New to FreeBASIC? Post your questions here.
Post Reply
Fabrizio_00000
Posts: 22
Joined: Mar 31, 2011 17:30
Location: Rome, Italy

UNION problem?

Post by Fabrizio_00000 »

Have a look at this code fragment:

Code: Select all

type vtype
	vtype as ushort
	namelen as ubyte
	name as string * 15
	union
		i8 as byte
		i16 as short
		i32 as long
		flt as double
		spt as any ptr
	end union
end type

dim shared as vtype vars(128) = { type(&h0004, 1, "x", 1.52), _
				  type(&h0001, 5, "pippo", 51) }
It seems that gcc ha some problems with it:

Code: Select all

l.c:27:36: warning: missing braces around initializer [-Wmissing-braces]
   27 | static struct $5VTYPE VARS$[129] = { { (uint16)4u, (uint8)1u, "x", (int8)2 }, ...
                                           ^
This happenss with fbc64 only (version 1.10.1). However it doesn't end here. If I try to give a name to the UNION, fbc (32 and 64) throws an error:

error 67: Too many expressions in 'dim shared as vtype vars(128) = { type(&h0004, 1, "x", 1.52), _'

What am I doing wrong?

Thanks to anyone who responds!


fab
fxm
Moderator
Posts: 12528
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: UNION problem?

Post by fxm »

No warning with gas (32-bit) and gas64 (64-bit).
Warning with only gcc (32-bit and 64 bit).
fxm
Moderator
Posts: 12528
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: UNION problem?

Post by fxm »

Initialize implicitly an union field structure is not always safe.
Generally, the first declared data of the union structure is initialized, but this is a bite daring.

If one defines specific constructors, no more problem.
For example to initialize the first declared data of the union structure:

Code: Select all

type vtype
	vtype as ushort
	namelen as ubyte
	name as string * 15
	union
		i8 as byte
		i16 as short
		i32 as long
		flt as double
		spt as any ptr
	end union
    declare constructor()
    declare constructor(byval _vtype as ushort, byval _namelen as ubyte, byref _name as string, byval _i8 as byte)
end type

constructor vtype()
end constructor

constructor vtype(byval _vtype as ushort, byval _namelen as ubyte, byref _name as string, byval _i8 as byte)
    vtype = _vtype
    namelen = _namelen
    name = _name
    i8 = _i8
end constructor

dim shared as vtype vars(128) = { type(&h0004, 1, "x", 1.52), _
				  type(&h0001, 5, "pippo", 51) }
fxm
Moderator
Posts: 12528
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: UNION problem?

Post by fxm »

Example of initializing for any field of Union:

Code: Select all

type vtype
	vtype as ushort
	namelen as ubyte
	name as string * 15
	union
		i8 as byte
		i16 as short
		i32 as long
		flt as double
		spt as any ptr
	end union
    declare constructor()
    declare constructor(byval _vtype as ushort, byval _namelen as ubyte, byref _name as string, byval _i8 as byte)
    declare constructor(byval _vtype as ushort, byval _namelen as ubyte, byref _name as string, byval _i16 as short)
    declare constructor(byval _vtype as ushort, byval _namelen as ubyte, byref _name as string, byval _i32 as long)
    declare constructor(byval _vtype as ushort, byval _namelen as ubyte, byref _name as string, byval _flt as double)
    declare constructor(byval _vtype as ushort, byval _namelen as ubyte, byref _name as string, byval _spt as any ptr)
end type

constructor vtype()
end constructor

constructor vtype(byval _vtype as ushort, byval _namelen as ubyte, byref _name as string, byval _i8 as byte)
    print "call constructor for 'byte' field initialization in union"
    vtype = _vtype
    namelen = _namelen
    name = _name
    i8 = _i8
end constructor

constructor vtype(byval _vtype as ushort, byval _namelen as ubyte, byref _name as string, byval _i16 as short)
    print "call constructor for 'short' field initialization in union"
    vtype = _vtype
    namelen = _namelen
    name = _name
    i16 = _i16
end constructor

constructor vtype(byval _vtype as ushort, byval _namelen as ubyte, byref _name as string, byval _i32 as long)
    print "call constructor for 'long' field initialization in union"
    vtype = _vtype
    namelen = _namelen
    name = _name
    i32 = _i32
end constructor

constructor vtype(byval _vtype as ushort, byval _namelen as ubyte, byref _name as string, byval _flt as double)
    print "call constructor for 'double' field initialization in union"
    vtype = _vtype
    namelen = _namelen
    name = _name
    flt = _flt
end constructor

constructor vtype(byval _vtype as ushort, byval _namelen as ubyte, byref _name as string, byval _spt as any ptr)
    print "call constructor for 'any ptr' field initialization in union"
    vtype = _vtype
    namelen = _namelen
    name = _name
    spt = _spt
end constructor

dim shared as vtype vars(1 to 128) = { type(1, 1, "1", Cast(byte, 1)), _
                                     type(2, 2, "2", Cast(short, 2)), _
                                     type(3, 3, "3", Cast(long, 3)), _
                                     type(4, 4, "4", Cast(double, 4)), _
                                     type(5, 5, "5", @"5") }

sleep
'Cast' is used to override the implicit conversion of Union argument when there are multiple constructor candidates.
Post Reply