May 05, 2024, 04:16:50 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Converting Unions

Started by Zen, January 09, 2006, 11:24:15 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Zen

Hello. While we dont have unions at the moment is it possible to still use some API stuff that have unions in them?

I am converting some of the windows headers i need at the moment and came accross a structure with the UNION {...} part in it. Ive never come accross them before so i dont know what they do or what they are. Some code i saw though seemed to access the elements just like structure variables though. So is it possible just create a structure with the union elements instead?

Hope this makes sense. ;)

Lewis

sapero

Hello
In Alpha version I have used same trick as in Pro: int d[0]
It works, but only for "normal" variables, *pointer adds 4 bytes:
struct myunion1
{
ÂÃ,  ÂÃ, int type;ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ, // 4
ÂÃ,  ÂÃ, //alpha union
ÂÃ,  ÂÃ, int hbitbap[0];// 0
ÂÃ,  ÂÃ, int hicon[0];ÂÃ,  // 0
ÂÃ,  ÂÃ, int blah1[0];ÂÃ,  // 0
ÂÃ,  ÂÃ, int blah2;ÂÃ,  ÂÃ,  ÂÃ, // 4
}

struct myunion2
{
ÂÃ,  ÂÃ, int type;ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ, // 4
ÂÃ,  ÂÃ, pointer x[0];ÂÃ,  ÂÃ, // 0
ÂÃ,  ÂÃ, intÂÃ,  ÂÃ,  *pInt[0];// 4, bug
ÂÃ,  ÂÃ, int blah1[0];
ÂÃ,  ÂÃ, int blah2;ÂÃ,  ÂÃ,  ÂÃ,  // 4
}

import int system(...);
import int printf(...);

global sub main
{
printf("size of myunion1 = %d\n", len(myunion1));
printf("size of myunion2 = %d\n", len(myunion2));
system("pause");
}

The UNION keyword is (i think) in progress :)

Zen

Yes i believe Paul mentioned unions but as i did not know what they were i simply ignored it.

Thanks a lot sapero.

Lewis

Parker

Paul said unions were planned for Alpha 3, so we can look forward to seeing them in the near future.

I didn't know type* added bytes to the structure, oh well, since it's my own union it'll work fine when the union keyword is implemented.

Parker

Also just for reference, a union is a place where each element is stored in the same space in all the other elements. It's useful if you only need to use one field at a time. For example,
union Storage
{
    string *str;
    int integer;
    double dbl;
}


That union takes up exactly 8 bytes, or 64 bits, because that is the size of the largest member (double). If you assign something to the integer field, it just treats it as if you were using an int. Same for the string pointer and double.

Zen

so instead of using unions then, why not just pick the type you want instead? I managed to get the code working by just using the type in the code that i wanted.

Lewis

Parker

Mostly for using less memory. Consider a union with 100 members. If that was a structure or just a list of variables, it would take up a lot of space in memory. But if you only use one at a time, you can use a union and only take up say 8 bytes instead of 420. You can do this with casting in C too, but unions look cleaner.